c# – 如何在XPSDocument中使用WriteAsync?

我的代码有问题,

XpsDocument doc = new XpsDocument(_filePath, FileAccess.Read);
XpsDocument = doc.GetFixedDocumentSequence();
...
var writer = PrintQueue.CreateXpsDocumentWriter(pq);
var paginator = XpsDocument.DocumentPaginator;
writer.WritingCancelled += WriterOnWritingCancelled;
writer.WritingCompleted += WriterOnWritingCompleted;
writer.WritingPrintTicketRequired += WriterOnWritingPrintTicketRequired;
writer.WriteAsync(paginator);

如果我用Write(paginator)替换最后一个Ligne,一切都还可以,但如果我使用write async,我会得到一个,

FixedPage cannot contain another FixedPage.

WriterOnWritingCompleted中的错误;

我发现article.但我真的不知道如何处理那个超载.

解决方法:

你可以尝试这个code

private void WriteUsingAsync (
            string XPSDocFileName, object FixedDocFromApplication) 
    {
        // create a new XPS Document for output
        //  note that AsyncXpsDoc is a property defined in the
        //   class containing this method.
        AsyncXpsDoc = new XpsDocument(XPSDocFileName,
            FileAccess.ReadWrite);

    // create a doc writer object based on the open XPS Document
    XpsDocumentWriter XpsDocWrtr =
        XpsDocument.CreateXpsDocumentWriter(AsyncXpsDoc);

    // register the writing completed event handler
    XpsDocWrtr.WritingCompleted +=
        new WritingCompletedEventHandler(AsyncPrintCompleted);
    XpsDocWrtr.WriteAsync((FixedDocument)FixedDocFromApplication);

    return;
}

// Completion Event Handler
private void AsyncPrintCompleted(
    object sender, WritingCompletedEventArgs e)
{
    if ((e.Cancelled) || (e.Error != null))
        //document not written
    else
        // Asynchronous operation Completed

    AsyncXpsDoc.Close();
    AsyncXpsDoc = null;
}

并检查您是否以正确的方式创建了fixedDocument,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;
    }
上一篇:远程控制屏幕向日葵和teamviewer


下一篇:c# – 如何在将XPS转换为PDF时获取GhostPDL进度通知?