# IronPDFを使ってC#でPDFを結合または分割する方法
IronPDF を使用すると、C# 開発者は、結合用の `Merge()` や分割用の `CopyPages()` といったシンプルなメソッドを用いて、複数の PDF ファイルを 1 つのドキュメントに結合したり、PDF を個別のファイルに分割したりすることができ、.NET アプリケーションにおけるドキュメント管理を効率化できます。
複数のPDFファイルを1つに結合すると、さまざまなシナリオで非常に便利です。 例えば、複数のファイルを共有する代わりに、履歴書のような類似文書を1つのファイルにまとめることができます。 この記事では、C#を使用して複数のPDFファイルを結合する手順を案内します。 IronPDFは、C#アプリケーション内で直感的なメソッドコールを使ってPDFの分割と結合を簡素化します。 以下では、すべてのページ操作機能について説明します。
*as-heading:2(クイックスタート: IronPDFで PDF を結合する )*
IronPDFを使って複数のPDFファイルを1つのドキュメントに統合します。 数行のコードで、開発者はPDFマージ機能をC#アプリケーションに統合することができます。 このクイックガイドでは、IronPDFライブラリの`Merge`メソッドを使用してPDFを結合し、ドキュメント管理タスクを効率化する方法について説明します。
```cs
:title=Merge multiple PDFs into one with a single call!
IronPdf.PdfDocument
.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf"))
.SaveAs("merged.pdf");
```
<div class="hsg-featured-snippet">
<h3>最小限のワークフロー(5ステップ)</h3>
<img class="featured-snippet__image featured-snippet__image--float-right featured-snippet__image--new-template" src="/static-assets/pdf/images/How-to-Merge-or-Split-PDFs.webp" alt="IronPDF C# チュートリアル概要: インストールコマンド付きで PDF を結合・分割する 5 ステップのプロセス" />
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://www.nuget.org/packages/IronPdf/">PDFドキュメント操作のためのIronPDFライブラリをダウンロード</a>してください。</li>
<li>既存のPDFをロードするか、HTML文字列、ファイル、またはURLからPDFを作成します</li>
<li>C#で<code>Merge</code>メソッドを使って2つのPDFファイルをマージする</li>
<li><code>CopyPage</code>と<code>CopyPages</code>メソッドを使用して、PDFファイルをページごとに分割します。</li>
<li>PDFドキュメントを希望の場所に保存する</li>
</ol>
</div>
<br class="clear" />
## PDFをC#で結合するにはどうすればよいですか?
次のデモンストレーションでは、2つのHTML文字列を初期化し、IronPDFで別々のPDFとしてレンダリングし、それらをマージします。 このアプローチは、[HTMLをPDFに変換](https://ironpdf.com/tutorials/html-to-pdf/)し、複数のHTMLドキュメントを1つのPDF出力にまとめる必要がある場合に特に役立ちます。
```csharp
using IronPdf;
// Two paged PDF
const string html_a =
@"<p> [PDF_A] </p>
<p> [PDF_A] 1st Page </p>
<div style = 'page-break-after: always;' ></div>
<p> [PDF_A] 2nd Page</p>";
// Two paged PDF
const string html_b =
@"<p> [PDF_B] </p>
<p> [PDF_B] 1st Page </p>
<div style = 'page-break-after: always;' ></div>
<p> [PDF_B] 2nd Page</p>";
var renderer = new ChromePdfRenderer();
var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
// Four paged PDF
var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
merged.SaveAs("Merged.pdf");
```
上のコードは、[改ページ](https://ironpdf.com/how-to/html-to-pdf-page-breaks/)を効果的に使って、結合前に複数ページのPDFを作成する方法を示しています。 `page-break-after: always` CSSプロパティにより、各セクションが新しいページから開始されるようになります。
### マージされたPDFはどのように見えますか?
これはコードによって生成されたファイルです:
<iframe loading="lazy" src="/static-assets/pdf/how-to/merge-or-split-pdfs/Merged.pdf#view=fit" width="100%" height="500px">
</iframe>
### PDFマージはいつ使用すべきですか?
PDFマージは、ドキュメントの統合がワークフローの効率を向上させる企業環境において特に価値があります。 一般的な用途は以下のとおりです:
- **レポート作成**:[PDFレポート生成](https://ironpdf.com/how-to/csharp-pdf-reports/)を使用して、複数の部門レポートを包括的なエグゼクティブサマリーにまとめます。
- **請求書処理**:請求書ワークフローを簡素化するために、顧客の請求書をサポート文書とマージします。
- **法的文書**:契約書、追補、署名を1つの法的文書に統合します。
- **教材**:コースの教材、課題、リソースを包括的な学習ガイドにまとめる。
### 一般的なマージ シナリオとは
IronPDFは基本的なファイル結合にとどまらず、高度な結合シナリオにも対応しています:
1. **バッチ処理**: ループやコレクションを使って数百のPDFをプログラムで統合
2. **条件付きマージ**: ビジネスロジックまたはメタデータ基準に基づいてPDFを結合
3. **テンプレート統合**: 動的コンテンツを事前にデザインされたPDFテンプレートと統合
4.**クロスフォーマット結合**:[HTML文字列](https://ironpdf.com/how-to/html-string-to-pdf/)、[URL](https://ironpdf.com/how-to/url-to-pdf/)、[画像](https://ironpdf.com/how-to/image-to-pdf/)のような異なるソースから作成されたPDFを結合します。
<hr />
## PDFページを結合するにはどうすればよいですか?
`CombinePages` メソッドを使用して、複数の PDF ページを 1 つのページに結合します。 このメソッドは、幅、高さ、行数、および列数を必要とします。 この機能は、サムネイル、コンタクトシート、複数ページのプレビューを作成する際に特に便利です。
```csharp
using IronPdf;
// Load an existing PDF document from a file.
PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");
// Combine pages of the loaded PDF into a grid with specified dimensions.
// The parameters for CombinePages are the width and height of each page
// in millimeters followed by the number of rows and columns to create the grid.
int pageWidth = 250; // Width of each page in the grid
int pageHeight = 250; // Height of each page in the grid
int rows = 2; // Number of rows in the grid
int columns = 2; // Number of columns in the grid
// Combine the pages of the PDF document into a single page with specified dimensions.
PdfDocument combinedPages = pdf.CombinePages(pageWidth, pageHeight, rows, columns);
// Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf");
```
### 結合されたページはどのように見えますか?
<iframe loading="lazy" src="/static-assets/pdf/how-to/merge-or-split-pdfs/combinePages.pdf#view=fit" width="100%" height="500px">
</iframe>
### なぜページをグリッドにまとめるのか
グリッドレイアウトにページを組み合わせることで、複数の目的が達成されます:
– **ドキュメントプレビュー**: クイックビジュアルスキャンのためにマルチページ文書のサムナイルビューを作成
– **比較シート**: 簡単な比較のために複数のバージョンを並べて表示
– **印刷最適化**: 1枚のシートに複数ページを収めることで用紙使用量を削減
- **プレゼンテーション資料**:1ページに複数のスライドを含む配布資料の作成
### `CombinePages`のパラメータは何ですか?
`CombinePages` メソッドは、4つの必須パラメータを受け取ります:
1. **`pageWidth`**: グリッド内の個々のページ幅(ミリメートル単位)
2. **`pageHeight`**: グリッド内の個々のページの高さ(ミリメートル単位)
3. **`rows`**: グリッドレイアウトにおける水平行数
4. **`columns`**: グリッドレイアウトにおける縦列の数
カスタムページサイズについては、[カスタム用紙サイズ](https://ironpdf.com/how-to/custom-paper-size/)設定のガイドを参照してください。
<hr />
## C#でPDFを分割するには?
次のデモンストレーションでは、前の例からマルチページPDF文書を分割します。 PDFの分割は、特定のページを抽出したり、文書の抜粋を作成したり、個々のセクションを異なる受信者に配布したりするために不可欠です。
```csharp
using IronPdf;
// We will use the 4-page PDF from the Merge example above:
var pdf = PdfDocument.FromFile("Merged.pdf");
// Takes only the first page into a new PDF
var page1doc = pdf.CopyPage(0);
page1doc.SaveAs("Page1Only.pdf");
// Take the pages 2 & 3 (Note: index starts at 0)
var page23doc = pdf.CopyPages(1, 2);
page23doc.SaveAs("Pages2to3.pdf");
```
このコードは2つのファイルを保存します:
- **`Page1Only.pdf`** (1ページ目のみ)
- **`Pages2to3.pdf`** (2ページ目から3ページ目)
### 分割されたPDFはどのように見えますか?
これらは製作された2つのファイルです。
#### Page1Only.pdf
<iframe loading="lazy" src="/static-assets/pdf/how-to/merge-or-split-pdfs/Page1Only.pdf#view=fit" width="100%" height="500px">
</iframe>
#### Pages2to3.pdf
<iframe loading="lazy" src="/static-assets/pdf/how-to/merge-or-split-pdfs/Pages2to3.pdf#view=fit" width="100%" height="500px">
</iframe>
### PDFはいつ分割すべきですか?
PDFの分割は、文書管理に多くの利点をもたらします:
– **選択的配信**: 特定の利害関係者にのみ関連するページを共有
– **ファイルサイズ管理**: 大きなPDFをメール添付に適した管理可能なチャンクに分割
– **章の抽出**: 本やマニュアルから個々の章を抽出
– **フォーム処理**: 完成したフォームを指示ページから分離
- **アーカイブの構成**: ドキュメントを日付、部署、またはカテゴリで分割します。
より高度なページ操作については、[ページの追加、コピー、削除](https://ironpdf.com/how-to/add-copy-delete-pages-pdf/)に関するガイドをご覧ください。
### `CopyPage` と `CopyPages` の違いは何ですか?
PDFを効率的に操作するためには、これらの方法の違いを理解することが重要です:
- **`CopyPage(int pageIndex)`**: 指定されたインデックス(0 から数える)の単一ページを抽出します
- **`CopyPages(int startIndex, int endIndex)`**: 指定した範囲のページを包括的に抽出します
ここでは、両方の方法を示す高度な例を示します:
```csharp
using IronPdf;
// Load a PDF document
var sourcePdf = PdfDocument.FromFile("LargeDocument.pdf");
// Extract cover page (first page)
var coverPage = sourcePdf.CopyPage(0);
coverPage.SaveAs("CoverPage.pdf");
// Extract table of contents (pages 2-5)
var tableOfContents = sourcePdf.CopyPages(1, 4);
tableOfContents.SaveAs("TableOfContents.pdf");
// Extract specific chapter (pages 20-35)
var chapter3 = sourcePdf.CopyPages(19, 34);
chapter3.SaveAs("Chapter3.pdf");
// Create a custom selection by merging specific pages
var customSelection = PdfDocument.Merge(
sourcePdf.CopyPage(0), // Cover
sourcePdf.CopyPages(5, 7), // Executive Summary
sourcePdf.CopyPage(50) // Conclusion
);
customSelection.SaveAs("ExecutiveBrief.pdf");
```
この例では、分割操作とマージ機能を組み合わせてカスタム文書を作成する方法を紹介します。
次に何ができるのかを見てみましょうか? チュートリアルのページはこちらをご覧ください:[IronPDFを整理する](https://ironpdf.com/tutorials/organize-pdfs-complete-tutorial/)では、[メタデータ管理](https://ironpdf.com/how-to/metadata/)、[しおり作成](https://ironpdf.com/how-to/bookmarks/)、高度なページ操作戦略など、包括的なPDF整理テクニックを紹介しています。
CombinePages メソッドを使用して、複数の PDF ページを 1 つのページに結合します。 このメソッドは、幅、高さ、行数、および列数を必要とします。 この機能は、サムネイル、コンタクトシート、複数ページのプレビューを作成する際に特に便利です。
using IronPdf;// Load an existing PDF document from a file.PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");// Combine pages of the loaded PDF into a grid with specified dimensions.// The parameters for CombinePages are the width and height of each page// in millimeters followed by the number of rows and columns to create the grid.int pageWidth = 250; // Width of each page in the gridint pageHeight = 250; // Height of each page in the gridint rows = 2; // Number of rows in the gridint columns = 2; // Number of columns in the grid// Combine the pages of the PDF document into a single page with specified dimensions.PdfDocument combinedPages = pdf.CombinePages(pageWidth, pageHeight, rows, columns);// Save the combined document as a new PDF file.combinedPages.SaveAs("combinedPages.pdf");
using IronPdf;
// Load an existing PDF document from a file.
PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");
// Combine pages of the loaded PDF into a grid with specified dimensions.
// The parameters for CombinePages are the width and height of each page
// in millimeters followed by the number of rows and columns to create the grid.
int pageWidth = 250; // Width of each page in the grid
int pageHeight = 250; // Height of each page in the grid
int rows = 2; // Number of rows in the grid
int columns = 2; // Number of columns in the grid
// Combine the pages of the PDF document into a single page with specified dimensions.
PdfDocument combinedPages = pdf.CombinePages(pageWidth, pageHeight, rows, columns);
// Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf");
ImportsIronPdf' Load an existing PDF document from a file.Private pdf AsPdfDocument = PdfDocument.FromFile("Merged.pdf")' Combine pages of the loaded PDF into a grid with specified dimensions.' The parameters for CombinePages are the width and height of each page' in millimeters followed by the number of rows and columns to create the grid.Private pageWidth AsInteger = 250 ' Width of each page in the gridPrivate pageHeight AsInteger = 250 ' Height of each page in the gridPrivate rows AsInteger = 2 ' Number of rows in the gridPrivate columns AsInteger = 2 ' Number of columns in the grid' Combine the pages of the PDF document into a single page with specified dimensions.Private combinedPages AsPdfDocument = pdf.CombinePages(pageWidth, pageHeight, rows, columns)' Save the combined document as a new PDF file.combinedPages.SaveAs("combinedPages.pdf")
Imports IronPdf
' Load an existing PDF document from a file.
Private pdf As PdfDocument = PdfDocument.FromFile("Merged.pdf")
' Combine pages of the loaded PDF into a grid with specified dimensions.
' The parameters for CombinePages are the width and height of each page
' in millimeters followed by the number of rows and columns to create the grid.
Private pageWidth As Integer = 250 ' Width of each page in the grid
Private pageHeight As Integer = 250 ' Height of each page in the grid
Private rows As Integer = 2 ' Number of rows in the grid
Private columns As Integer = 2 ' Number of columns in the grid
' Combine the pages of the PDF document into a single page with specified dimensions.
Private combinedPages As PdfDocument = pdf.CombinePages(pageWidth, pageHeight, rows, columns)
' Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf")
using IronPdf;// We will use the 4-page PDF from the Merge example above:var pdf = PdfDocument.FromFile("Merged.pdf");// Takes only the first page into a new PDFvar page1doc = pdf.CopyPage(0);page1doc.SaveAs("Page1Only.pdf");// Take the pages 2 & 3 (Note: index starts at 0)var page23doc = pdf.CopyPages(1, 2);page23doc.SaveAs("Pages2to3.pdf");
using IronPdf;
// We will use the 4-page PDF from the Merge example above:
var pdf = PdfDocument.FromFile("Merged.pdf");
// Takes only the first page into a new PDF
var page1doc = pdf.CopyPage(0);
page1doc.SaveAs("Page1Only.pdf");
// Take the pages 2 & 3 (Note: index starts at 0)
var page23doc = pdf.CopyPages(1, 2);
page23doc.SaveAs("Pages2to3.pdf");
ImportsIronPdf' We will use the 4-page PDF from the Merge example above:Private pdf = PdfDocument.FromFile("Merged.pdf")' Takes only the first page into a new PDFPrivate page1doc = pdf.CopyPage(0)page1doc.SaveAs("Page1Only.pdf")' Take the pages 2 & 3 (Note: index starts at 0)Dim page23doc = pdf.CopyPages(1, 2)page23doc.SaveAs("Pages2to3.pdf")
Imports IronPdf
' We will use the 4-page PDF from the Merge example above:
Private pdf = PdfDocument.FromFile("Merged.pdf")
' Takes only the first page into a new PDF
Private page1doc = pdf.CopyPage(0)
page1doc.SaveAs("Page1Only.pdf")
' Take the pages 2 & 3 (Note: index starts at 0)
Dim page23doc = pdf.CopyPages(1, 2)
page23doc.SaveAs("Pages2to3.pdf")
Can IronPDF handle merging PDFs from different sources?
Yes, IronPDF supports cross-format merging that allows combining PDFs created from HTML strings, URLs, images, or other content sources, offering flexible integration in diverse scenarios.
How can I customize the layout of combined pages using IronPDF?
When using the `CombinePages` method, you can specify the width, height, number of rows, and columns for the grid layout. This allows precise customization for creating specific page arrangements or visual preps.
What coding example demonstrates merging PDFs with IronPDF?
An example is: `IronPdf.PdfDocument.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")).SaveAs("merged.pdf");` which combines two PDF files into one.
What are the benefits of using IronPDF for document management in C#?
IronPDF enhances document management by offering easy-to-use methods for merging and splitting PDFs, managing document layout, and ensuring efficient consolidation or distribution across various business needs.