可以将多个xps文档合并到WPF中的一个xps文档中并显示在DocumentViewer
中吗?
一个应用程序有4个小的xps文档,每个文档单独显示,但在其中一个地方,所有4个文档应显示为一个文档。我该怎么做呢?
答案 0 :(得分:3)
此处,targetDocument
是新文件的目标路径,list
是要合并的所有文档的列表。
public void CreateXPSStreamPages(string targetDocument, List<string> list)
{
Package container = Package.Open(targetDocument, FileMode.Create);
XpsDocument xpsDoc = new XpsDocument(container);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
vxpsd.BeginBatchWrite();
foreach (string sourceDocument in list)
{
AddXPSDocument(sourceDocument, vxpsd);
}
vxpsd.EndBatchWrite();
container.Close();
}
public void AddXPSDocument(string sourceDocument, SerializerWriterCollator vxpsd)
{
XpsDocument xpsOld = new XpsDocument(sourceDocument, FileAccess.Read);
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
foreach (DocumentReference r in seqOld.References)
{
FixedDocument d = r.GetDocument(false);
foreach (PageContent pc in d.Pages)
{
FixedPage fixedPage = pc.GetPageRoot(false);
double width = fixedPage.Width;
double height = fixedPage.Height;
Size sz = new Size(width, height);
fixedPage.Width = width;
fixedPage.Height = height;
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
//fixedPage.UpdateLayout();
ContainerVisual newPage = new ContainerVisual();
newPage.Children.Add(fixedPage);
//test: add Watermark from Feng Yuan sample
//newPage.Children.Add(CreateWatermark(width, height, "Watermark"));
vxpsd.Write(newPage);
}
}
xpsOld.Close();
}
答案 1 :(得分:1)
如果有人对VB代码感兴趣:
Public Sub CreateXPSStream(targetDocument As String, ListToMerge As List(Of String))
If (File.Exists(targetDocument)) Then
File.Delete(targetDocument)
End If
Dim container As Package = Package.Open(targetDocument, FileMode.Create)
Dim xpsDoc = New System.Windows.Xps.Packaging.XpsDocument(container)
Dim seqNew As FixedDocumentSequence = New FixedDocumentSequence()
For Each sourceDocument As String In ListToMerge
AddXPSDocuments(sourceDocument, seqNew)
Next
Dim xpsWriter As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc)
xpsWriter.Write(seqNew)
xpsDoc.Close()
container.Close()
End Sub
Public Sub AddXPSDocuments(ByVal sourceDocument As String, ByRef seqNew As FixedDocumentSequence)
Try
Dim xpsOld As XpsDocument = New XpsDocument(sourceDocument, FileAccess.Read)
Dim seqOld As FixedDocumentSequence = xpsOld.GetFixedDocumentSequence()
For Each r As DocumentReference In seqOld.References
Dim newRef As DocumentReference = New DocumentReference()
CType(newRef, IUriContext).BaseUri = CType(r, IUriContext).BaseUri
newRef.Source = r.Source
seqNew.References.Add(newRef)
Next
Catch ex As Exception
myStatusAdd("Error with " & sourceDocument)
End Try
End Sub
感谢您的初始代码
这也需要在您的项目中提供相当多的参考资料:
PresentationCore
PresentationFramework
ReachFramwork
System.Core
System.Windows.Presentation
System.Xaml
System.Printing
WindowsBase
我认为就是这样。