0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WPF-XPSドキュメントをマージする方法

Last updated at Posted at 2021-05-06

xpsファイルをマージして一つにする方法。備忘録として
参考記事:https://stackoverflow.com/questions/10672456/merging-xps-documents-make-last-one-duplicate

public void DoMerge(List<string> mergedFiles)
{
    // ソースドキュメントの準備
    List<XpsDocument> sourceXps = new List<XpsDocument>();

    foreach (var f in mergedFiles)
    {
        sourceXps.Add(new XpsDocument(f, FileAccess.Read));
    }

    // 出力先
    var name = destinationPath;
    if (File.Exists(name))
    {
        File.Delete(name);
    }
    XpsDocument destXps = new XpsDocument(name, System.IO.FileAccess.ReadWrite);

    // ライター
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(destXps);
    FixedDocumentSequence seq = new FixedDocumentSequence();

    // マージ
    foreach (XpsDocument doc in sourceXps)
    {
        FixedDocumentSequence sourceSeq = doc.GetFixedDocumentSequence();

        foreach (DocumentReference dr in sourceSeq.References)
        {
            DocumentReference newDr = new DocumentReference();
            newDr.Source = dr.Source;
            (newDr as System.Windows.Markup.IUriContext).BaseUri = (dr as System.Windows.Markup.IUriContext).BaseUri;
            FixedDocument fd = newDr.GetDocument(true);
            newDr.SetDocument(fd);
            seq.References.Add(newDr);
        }
    }

    // 終了処理
    writer.Write(seq);
    destXps.Close();
}
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?