我有一个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文件?
答案 0 :(得分:1)
使用方法来合并我的XPS文档存在问题,我将其更改为此处的代码: Can multiple xps documents be merged to one in WPF?
现在我可以将所有页面转换为PDF格式。