Document Merger

その Aspose.Words Document Merger for .NET プログラムで 任意の数の Word‑compatible ドキュメントを結合 単一の出力にまとめ、書式、スタイル、ページレイアウト、ヘッダー/フッター、フィールドを保持します。典型的な使用例として、レポートの作成、契約書添付資料のバンドル、または生成されたコンテンツをアーカイブ用に集約することが挙げられます。.

Windows、Linux、macOS でヘッドレスに動作します。Microsoft Office は不要です。.

インストールとセットアップ

  1. NuGet パッケージをインストール Aspose.Words.
  2. アプリ起動時にメーター制ライセンスを適用して、評価用ウォーターマークを回避します。参照 Metered Licensing .
  3. フレームワーク要件を確認してください インストールガイド .

IDEs: Visual Studio 2017–2026、JetBrains Rider、MonoDevelop。.

サポートされている入力 / 出力: DOC、DOCX、RTF、DOT、DOTX、DOTM、DOCM、Word 2003 XML、Word 2007 XML。.

クイックスタート: 複数のドキュメントを追加

using Aspose.Words;

var master = new Document();
var builder = new DocumentBuilder(master);

builder.InsertBreak(BreakType.SectionBreakNewPage);

ImportFormatMode mode = ImportFormatMode.KeepSourceFormatting;

foreach (string path in new[] { "Blank.docx", "Background images.docx", "annex.rtf" })
{
    var src = new Document(path);
    master.AppendDocument(src, mode);
    builder.MoveToDocumentEnd();
    builder.InsertBreak(BreakType.SectionBreakNewPage);
}

master.UpdateFields();
master.Save("MergedOutput.docx");

インポート形式モード:

  • KeepSourceFormatting — 各ソースのスタイルと直接書式設定を保持します(ブランド化された挿入物に最適)。.
  • UseDestinationStyles — 同一のスタイル名を宛先の定義にマッピングし、統一感のある外観にします。.

Controlled Section & Page Layout — 制御されたセクションとページレイアウト

各文書の開始位置を 新しいページ, 連続,、または 偶数/奇数ページ:

var sect = master.LastSection;
sect.PageSetup.SectionStart = SectionStart.NewPage; // Continuous, EvenPage, OddPage

余白/ページサイズをコピーして統一する PageSetup テンプレートセクションから:

using Aspose.Words;
var master = new Document();

var template = new Document("template.docx");
var pageSetup = template.FirstSection.PageSetup;
foreach (Section s in master.Sections)
{
    s.PageSetup.Orientation = pageSetup.Orientation;
    s.PageSetup.PageWidth  = pageSetup.PageWidth;
    s.PageSetup.PageHeight = pageSetup.PageHeight;
    s.PageSetup.TopMargin  = pageSetup.TopMargin;
    s.PageSetup.BottomMargin = pageSetup.BottomMargin;
    s.PageSetup.LeftMargin = pageSetup.LeftMargin;
    s.PageSetup.RightMargin = pageSetup.RightMargin;
}

Headers, Footers, and Watermarks — ヘッダー、フッター、ウォーターマーク

Keep source headers/footers, or replace them with a master set after the merge: — ソースのヘッダー/フッターを保持するか、マージ後にマスターセットに置き換えます:

// Copy headers/footers from a master template into every section
var hft = new Document("header-footer-template.docx");
foreach (Section s in master.Sections)
{
    s.HeadersFooters.Clear();
    s.HeadersFooters.AddClone(hft.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary]);
    s.HeadersFooters.AddClone(hft.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary]);
}

// Add a simple text watermark
foreach (Section s in master.Sections)
{
    var header = s.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(master, HeaderFooterType.HeaderPrimary);
    if (header.ParentNode == null) s.HeadersFooters.Add(header);
    var shape = new Shape(master, ShapeType.TextPlainText)
    {
        RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
        RelativeVerticalPosition   = RelativeVerticalPosition.Page,
        WrapType = WrapType.None,
        Rotation = -40,
        Width = 400, Height = 100, Left = 100, Top = 200,
        BehindText = true
    };
    shape.TextPath.Text = "CONFIDENTIAL";
    header.AppendChild(shape);
}

Fields, Cross‑References, and TOC — フィールド、相互参照、目次

マージ後、ページ番号、参照、そして 目次 新しいレイアウトに合わせる:

master.UpdateFields();
master.UpdatePageLayout();        // ensures accurate page count
master.UpdateTableLayout();       // improves complex table pagination

単一のマスターTOCを維持している場合は、1回だけ挿入して自動再構築させます:

using Aspose.Words;
var master = new Document();

var b = new DocumentBuilder(master);
b.MoveToDocumentStart();
b.InsertTableOfContents("TOC \\$1 \\$1 \\$1 \\$1");  // classic TOC switch set
master.UpdateFields();

ストリームファースト / 高ボリュームマージ

using System.IO;
using System.Collections.Generic;
using Aspose.Words;
using Aspose.Words.Lists;

IEnumerable<Stream> sourceStreams = new List<Stream>();

using (var output = new MemoryStream())
{
    var dst = new Document();
    foreach (Stream srcStream in sourceStreams)
    {
        using var s = srcStream; // e.g., S3/Blob stream
        var src = new Document(s); // auto‑detects format
        dst.AppendDocument(src, ImportFormatMode.UseDestinationStyles);
    }
    dst.UpdateFields();
    dst.Save(output, SaveFormat.docx);
    output.Position = 0;
    // return/output stream
}

パフォーマンスのヒント:

  • メモリ内でバッチ追加; 呼び出す UpdateFields() 最後に一度だけ。.
  • 優先 UseDestinationStyles 多数の入力に対して一貫したスタイリングを行いたい場合。.
  • 破棄 Document 長いパイプラインでインスタンスを早期に破棄し、ピークメモリを削減します。.

ベストプラクティス

  • 事前検証 破損/パスワードによる部分的なマージを防ぐために。.
  • 選択 KeepSourceFormatting ブランドの忠実性が重要な場合;選択 UseDestinationStyles 統一した外観のために。.
  • 挿入 セクション区切り 各追加の前にページ設定を保護するために。.
  • ヘッダー/フッターを統一する 追加した後、単一の企業テンプレートが欲しい場合。.
  • フィールド & TOC を更新 最終ステップとして。.
  • 非常に大きなバッチでは、グループに分割し、メモリを安定させるためにグループをマージします。.

FAQ

  1. すべてのsourceを新しいページから開始させることはできますか?? はい。各項目の前にsectionまたはpage breakを挿入します AppendDocument callまたはset SectionStart to NewPage.
  2. インポート中にdestinationスタイルを保持するにはどうすればよいですか?? 使用する ImportFormatMode.UseDestinationStyles in AppendDocument.
  3. ページ番号とTOCは自動的に更新されますか?? 呼び出す UpdateFields() (そして UpdatePageLayout() 正確なページングのため) すべての追加の後に。.
  4. Officeは必要ですか?? いいえ。Aspose.Wordsはスタンドアロンのライブラリです。.
 日本語