【问题标题】:WPF convert XPS with multiple pages to PDFWPF 将具有多页的 XPS 转换为 PDF
【发布时间】:2016-04-03 07:04:51
【问题描述】:

我有一个 WPF 应用程序,我在其中创建了一个 XPS 文档(从 Excel),然后我能够将 XPS 文档转换为 PDF 文件:

PdfSharp.Xps.XpsConverter.Convert(sourceXpsFile, destPdfFile, 0);

但是当我的 XPS 文档包含多个页面时,只有第一页会转换为 PDF。

看来我的 XPS 文件有误,我创建了多个单个 XPS 文件,然后将它们合并:

public void MergeXpsDocument(string newFile, List<XpsDocument> sourceDocuments)
    {
        Thread th = new Thread(() =>
        {    
            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }

            XpsDocument xpsDocument = new XpsDocument(newFile, System.IO.FileAccess.ReadWrite);
            XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            FixedDocumentSequence fixedDocumentSequence = new FixedDocumentSequence();

            foreach (XpsDocument doc in sourceDocuments)
            {
                FixedDocumentSequence sourceSequence = doc.GetFixedDocumentSequence();
                foreach (DocumentReference dr in sourceSequence.References)
                {
                    DocumentReference newDocumentReference = new DocumentReference();
                    newDocumentReference.Source = dr.Source;
                    (newDocumentReference as IUriContext).BaseUri = (dr as IUriContext).BaseUri;
                    FixedDocument fd = newDocumentReference.GetDocument(true);
                    newDocumentReference.SetDocument(fd);
                    fixedDocumentSequence.References.Add(newDocumentReference);

                }
                doc.Close();
            }
            xpsDocumentWriter.Write(fixedDocumentSequence);
            xpsDocument.Close();
        });
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
    }

也许我应该使用其他方式来合并我的 XPS 文件?

【问题讨论】:

  • 还有...?错误?警告?
  • 没有错误或警告,PDF 被创建,但只有第一页。

标签: wpf pdf pdfsharp xps


【解决方案1】:

合并我的 XPS 文档的方法存在问题,我将其更改为此处的代码: Can multiple xps documents be merged to one in WPF?

现在我可以将所有页面转换为 PDF。

【讨论】:

  • 这是一种将我的 N xaml 页面转换为 pdf 的解决方法。谢谢
猜你喜欢
  • 2012-11-30
  • 2011-06-25
  • 2011-01-18
  • 2011-09-17
  • 2011-11-25
  • 2012-05-16
  • 2011-04-22
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多