You can try this code :
private void WriteUsingAsync (
string XPSDocFileName, object FixedDocFromApplication)
{
AsyncXpsDoc = new XpsDocument(XPSDocFileName,
FileAccess.ReadWrite);
XpsDocumentWriter XpsDocWrtr =
XpsDocument.CreateXpsDocumentWriter(AsyncXpsDoc);
XpsDocWrtr.WritingCompleted +=
new WritingCompletedEventHandler(AsyncPrintCompleted);
XpsDocWrtr.WriteAsync((FixedDocument)FixedDocFromApplication);
return;
}
private void AsyncPrintCompleted(
object sender, WritingCompletedEventArgs e)
{
if ((e.Cancelled) || (e.Error != null))
else
AsyncXpsDoc.Close();
AsyncXpsDoc = null;
}
And make sure you create fixedDocument correctly, like :
var fd = new FixedDocument();
fd.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth,fd.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = fd.DocumentPaginator.PageSize.Width;
page1.Height = fd.DocumentPaginator.PageSize.Height;
UIElement page1Text = pages();
page1.Children.Add(page1Text);
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
fd.Pages.Add(page1Content);
private UIElement pages()
{
Canvas pcan = new Canvas();
TextBlock page1Text = new TextBlock();
page1Text.Text = "This is a test";
page1Text.FontSize = 40;
page1Text.Margin = new Thickness(96);
pcan.Children.Add(page1Text);
return pcan;
}
source
share