Can multiple xps documents be merged to one in WPF?

有些话、适合烂在心里 提交于 2019-11-30 06:02:43

问题


Can multiple xps documents be merged to one xps document in WPF and shown in DocumentViewer?
An application has 4 small xps documents each displayed seperately, but in one of the places all 4 documents should be shown as one document. How do I go about it?


回答1:


Here, targetDocument is the target path of the new file and list is a list of all documents to be merged.

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();
    }



回答2:


In case someone is interested in the VB code:

 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

thanks for the initial code.
This requires also a fair amount of references in your projects:

PresentationCore
PresentationFramework
ReachFramwork
System.Core
System.Windows.Presentation
System.Xaml
System.Printing
WindowsBase

I think that's all.



来源:https://stackoverflow.com/questions/1660748/can-multiple-xps-documents-be-merged-to-one-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!