WPF 将具有多个页面的 XPS 转换为 PDF
WPF convert XPS with multiple pages to PDF
我有一个 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 文件?
合并我的 XPS 文档的方法有问题,我将其更改为此处找到的代码:
Can multiple xps documents be merged to one in WPF?
现在我可以将所有页面转换为 PDF。
我有一个 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 文件?
合并我的 XPS 文档的方法有问题,我将其更改为此处找到的代码: Can multiple xps documents be merged to one in WPF?
现在我可以将所有页面转换为 PDF。