3

複数のXPSドキュメントを1つにマージするときに問題が発生します。それらをマージすると、結果のxpsには、複製された最後のXPSドキュメントが含まれます。これがマージする私の関数です(この質問の修正バージョン):

    public XpsDocument CreateXPSStream(List<XpsDocument> ListToMerge)
    {
        var memoryStream = new MemoryStream();
        Package container = Package.Open(memoryStream, FileMode.Create);
        string pack = "pack://temp.xps"; 
        PackageStore.RemovePackage(new Uri(pack));
        PackageStore.AddPackage(new Uri(pack), container);

        XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.SuperFast, "pack://temp.xps");
        FixedDocumentSequence seqNew = new FixedDocumentSequence();
        foreach (var sourceDocument in ListToMerge)
        {
            FixedDocumentSequence seqOld = sourceDocument.GetFixedDocumentSequence();
            foreach (DocumentReference r in seqOld.References)
            {
                DocumentReference newRef = new DocumentReference();
                ((IUriContext)newRef).BaseUri = ((IUriContext)r).BaseUri;
                newRef.Source = r.Source;
                seqNew.References.Add(newRef);
            }
        }
        XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
        xpsWriter.Write(seqNew);
        //xpsDoc.Close();
        //container.Close();
        return xpsDoc;
    }

結果はDocumentViewerに送られ、ユーザーに表示されます。

4

1 に答える 1

9

私は次の関数を作成しました、そしてそれは私のために働きます。

    public void MergeXpsDocument(string newFile, List<XpsDocument> sourceDocuments)
    {
        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);
            }
        }
        xpsDocumentWriter.Write(fixedDocumentSequence);
        xpsDocument.Close();
    }
于 2012-07-11T14:53:19.957 に答える