3

是否可以将多个 xps 文档合并到 WPF 中的一个 xps 文档并显示在DocumentViewer
一个应用程序有 4 个小的 xps 文档,每个文档单独显示,但在其中一个地方,所有 4 个文档都应显示为一个文档。我该怎么做?

4

2 回答 2

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();
    }
于 2009-11-02T13:55:01.830 回答
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

我想就是这样。

于 2011-11-18T18:30:28.363 回答