Hello,
I have written a Custom Command Descriptor based on the Telerik demos and documentation that exports the currently loaded document to a local file. Everything appears to be working 100% correct with no errors. However, when I open the file all the pages are blank. The page count is correct, so it appears to be reading the PDFViewer document correctly into the stream, just not writing all of the bytes properly. Here is my code behind for the page that contains the Document Viewer control.
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows.Documents.Fixed.FormatProviders;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model;
using WeldTrackingWPF.ViewModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.Commands.Descriptors;
using Telerik.Windows.Documents.Commands;
namespace WeldTrackingWPF.View
{
/// <
summary
>
/// Interaction logic for ViewQualificationDoc.xaml
/// </
summary
>
public partial class ViewDoc : Page
{
public ViewDoc(byte[] fileBytes)
{
InitializeComponent();
/// <
summary
>
/// Read bytes into memory stream
/// Pass stream in RadPdfViewer for viewing
/// </
summary
>
this.pdfViewer.CommandDescriptors = new CustomCommandDescriptors(this.pdfViewer);
Stream stream = new MemoryStream(fileBytes);
FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.AllAtOnce);
PdfFormatProvider provider = new PdfFormatProvider(stream, settings);
RadFixedDocument doc = provider.Import();
this.pdfViewer.Document = doc;
IconSources.ChangeIconsSet(IconsSet.Modern);
}
private void Page_Initialized(object sender, EventArgs e)
{
var vm = new ViewDocViewModel();
DataContext = vm;
}
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
}
private void tbCurrentPage_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
}
}
public class SaveCommand : FixedDocumentViewerCommandBase
{
public SaveCommand(FixedDocumentViewerBase fixedDocumentViewerBase)
: base(fixedDocumentViewerBase)
{
}
public override void Execute(object parameter)
{
Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "PDF files (*.pdf)|*.pdf";
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = File.OpenWrite(dialog.FileName))
{
PdfFormatProvider provider = new PdfFormatProvider();
provider.Export(this.Viewer.Document, stream);
}
}
}
}
public class CustomCommandDescriptors : DefaultCommandDescriptors
{
private readonly CommandDescriptor saveCommandDescriptor;
public CommandDescriptor SaveCommandDescriptor
{
get
{
return this.saveCommandDescriptor;
}
}
public CustomCommandDescriptors(FixedDocumentViewerBase fixedDocumentViewer) : base(fixedDocumentViewer)
{
this.saveCommandDescriptor = new CommandDescriptor(new SaveCommand(fixedDocumentViewer));
}
}
}
Any insight as to why these documents are being exported sans page content would be appreciated.
Thanks!