# JavaでPDFからページを削除する方法
IronPDF では、Java で PDF ページを削除するのは単純です:ライブラリは `removePage` と `removePages` メソッドを公開しており、`PageSelection` バックのため、削除するページを正確に制御できます -- それが1ページか、連続する範囲か、分散したページインデックスのセットであるかに関わらず。 IronPDFのすべてのページインデックスはゼロベースであるため、文書の最初のページは常にインデックス0です。
*as-heading:2(クイックスタート: JavaでPDFページを削除)*
1. MavenまたはGradleを介してJava用のIronPDFをインストールする
2. `License.setLicenseKey()` でライセンスキーを設定します
3. `PdfDocument.fromFile()` でPDFをロードします
4. 単一ページを `removePage()` で削除します
5. 結果を `saveAs()` で保存します
```java
:title=Quickstart
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf"));
// Remove the first page (zero-based index 0)
pdf.removePage(0);
// Save the modified PDF to a new file
pdf.saveAs(Path.of("modified.pdf"));
}
}
```
PDFからページを削除することは一般的な文書処理タスクです。 報告書を配布する前に表紙を削除したり、機密部分を削除して外部に共有したり、スキャナーやテンプレートが導入した空白ページをクリーンアップするために必要になることがあります。 IronPDFはホストマシンにネイティブPDF編集ツールを必要とせず、これらのケースすべてを一貫したJava APIを通じて処理します。
このライブラリはMavenまたはGradleを介してJavaアプリケーションに統合され、ページ削除のほかにもPDF操作のフルレンジをサポートし、[PDFの結合](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/)、[文書の分割](https://ironpdf.com/java/how-to/java-split-pdf-tutorial/)、および[ウォーターマークの追加](https://ironpdf.com/java/how-to/custom-watermark/)も含まれます。 セットアップと利用可能な機能の完全な概要については、[Get Started Overview](https://ironpdf.com/java/docs/)をご覧ください。
このガイドの例では、3つのシナリオをカバーしています:インデックスで単一ページを削除する、`PageSelection` を使用して連続したページ範囲を削除する、インデックスシフトエラーを引き起こさずに複数の非連続ページを安全に削除する。
<div class="hsg-featured-snippet">
<h2>JavaでPDFからページを削除する方法</h2>
<ol>
<li><a href="https://ironpdf.com/java/#download-modal">JavaライブラリをインストールしてPDFページを削除する</a></li>
<li><strong>PdfDocument</strong>クラスを使用してPDFを読み込む</li>
<li><code>removePage</code>を使用してインデックスにより単一ページを削除します</li>
<li><code>removePages</code>と<code>PageSelection</code>を使用して複数のページや範囲を削除します</li>
<li><code>saveAs</code>で変更されたPDFを保存します</li>
</ol>
</div>
## 始める前に何が必要ですか?
PDFからページを削除する前に、IronPDFがJavaプロジェクトに構成されていることを確認してください。 このライブラリはJava 8以上を必要とし、[MavenまたはGradle](https://central.sonatype.com/artifact/com.ironsoftware/ironpdf)で統合されます。 プロジェクトビルドファイルに `IronPdf` 依存性を追加してください。完全なセットアップ手順については、[Get Started Overview](https://ironpdf.com/java/docs/) を参照してください。
開発および本番利用には有効なライセンスキーが必要です。 アプリケーションの開始時にIronPDFメソッドを呼び出す前にキーを設定します。 ライセンスオプションの詳細については、[ライセンスキーガイド](https://ironpdf.com/java/get-started/license-keys/)をご覧ください。
[[t:(IronPDFのすべてのページインデックスはゼロベースの番号を使用します。 ドキュメントのページ1はインデックス0、ページ2はインデックス1と続きます。)]]
## PDFから単一のページを削除する方法は?
`removePage(int pageIndex)` メソッドはゼロベースのページインデックスを受け入れ、書類から正確にそのページを削除します。 呼び出しが完了すると、その後のすべてのページが1つ下にシフトするため、呼び出し前にキャッシュしたインデックスが同じページを指さない可能性があります。
例えば、5ページ (インデックス0から4) を持つドキュメントがあり、インデックス2を削除すると、インデックス3のページがインデックス2になり、インデックス4のページがインデックス3になります。このシフトを考慮に入れた削除シーケンスを計画し、特に`removePage` を続けて複数回呼び出す場合に備えてください。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/single-page.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("report.pdf"));
// Remove the cover page at index 0 (the first page)
pdf.removePage(0);
// Save the modified PDF -- the original file is not overwritten
pdf.saveAs(Path.of("report-no-cover.pdf"));
}
}
```
これはどのページを削除するか正確に知っていて、1つだけ削除すればよい場合に最も簡単なアプローチです。 単一操作で連続したページ範囲を削除するには、`removePages` を `PageSelection` 範囲とともに使用してください。
## PDFからページの範囲を削除する方法は?
`removePages(PageSelection)` メソッドは与えられた選択でカバーされるすべてのページを単一のアトミック操作で削除し、`removePage` を複数回呼び出すと生じるインデックスシフトの問題を回避します。 範囲を指定するには `PageSelection.pageRange(int fromIndex, int toIndex)` を使用します -- 両方のエンドポイントはゼロベースで包括的です。
以下の例では、`fromIndex = 2` および `toIndex = 4` を渡して、ドキュメントのページ3,4,5を削除します。 すべてのページが一度に削除されるため、操作中に中間的なインデックスシフトは発生しません。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/page-range.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load a multi-page PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("annual-report.pdf"));
// Remove pages 3, 4, and 5 using a zero-based inclusive range (indexes 2 to 4)
pdf.removePages(PageSelection.pageRange(2, 4));
// Save the result to a new file
pdf.saveAs(Path.of("annual-report-trimmed.pdf"));
}
}
```
隣接するページのブロックを削除する必要がある場合、`PageSelection.pageRange` は好ましいアプローチです。 各@--CODE-608--@@ 呼び出しをループするよりもクリーンで効率的であり、単一の操作セマンティクスによりページ数は一度だけ更新されます。
## 複数の非連続ページを削除する方法は?
互いに隣接していないページを削除する必要がある場合には、`PageSelection` をターゲットとする `removePages` を使用するか、`removePage` を注意深く順序付けられたシーケンス内で複数回呼び出す2つの実用的なオプションがあります。
複数回 `removePage` を呼び出す場合は、常に最大のインデックスから最小のインデックスまでの順に作業してください。低インデックスのページを最初に削除すると、より高いインデックスがすべて一つ下がり、後続の呼び出しが誤ったページをターゲットにしてしまう原因となります。 ドキュメントの終わりから逆方向に作業を開始することで、各削除が残りの低インデックスを乱さないままにします。
以下の例では、6ページのドキュメントから最初のページ、中間のページ、最後のページを削除します。 呼び出しはインデックスが高い方から低い方へと並んでおり(5、3、0)、インデックスのずれを防ぎます。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/non-consecutive-pages.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load a six-page PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("contract.pdf"));
// Remove non-consecutive pages: always work from highest index to lowest
// to prevent index shifting from affecting subsequent removals.
// Remove the last page (index 5 in a six-page document)
pdf.removePage(5);
// Remove a page in the middle (index 3 -- now safe because no lower index has shifted)
pdf.removePage(3);
// Remove the first page (index 0 -- lowest, so processed last)
pdf.removePage(0);
// Save the modified PDF
pdf.saveAs(Path.of("contract-redacted.pdf"));
}
}
```
[[i:(実行時に削除するページインデックスのリストを構築する際 - 例えば、ユーザー入力や設定ファイルから - の場合は、イテレーションする前にリストを降順に並べ替えます。 これにより、@--CODE-613--@@ 呼び出しが、すでに削除されたページ数に関係なく正しいページをターゲットにすることを保証します。)]]
削除するページのセットが動的に決定される場合、逆順にインデックスリストをソートしてループするのが簡潔なパターンです。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/dynamic-removal.java
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("document.pdf"));
// Define the zero-based page indexes to remove
List<Integer> pagesToRemove = Arrays.asList(0, 3, 5);
// Sort descending to avoid index-shift errors during sequential removal
pagesToRemove.sort(Comparator.reverseOrder());
// Remove each page in reverse-index order
for (int pageIndex : pagesToRemove) {
pdf.removePage(pageIndex);
}
// Save the modified PDF
pdf.saveAs(Path.of("document-pages-removed.pdf"));
}
}
```
ループの前にインデックスリストを降順でソートするのは、多くのページが対象であっても、入力リストがどのように並べ替えられていても機能する一般的な安全パターンです。
## JavaでPDFページを削除するための次のステップは何ですか?
IronPDF の `removePage` と `removePages` メソッドは、ページ削除を細かく制御でき、単一のページを削除する必要がある場合、範囲を削除する場合、または分散した複数のページを削除する場合に役立ちます。 ゼロベースのインデックスモデルは、完全なIronPDF Java APIにわたって一貫しているため、ページの分割、統合、再注文に進む際も同じ慣習が適用されます。
PDFページ構造およびドキュメント操作を続行するには、これらの関連リソースを調査してください:
- [JavaでのPDFの分割](https://ironpdf.com/java/how-to/java-split-pdf-tutorial/):特定のページまたはページ範囲を別のPDFファイルに抽出
- [JavaでのPDFの統合](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/):複数のドキュメントを1つのPDFに統合し、ページ全体の制御を可能にする
- [JavaでのPDFの圧縮](https://ironpdf.com/java/how-to/compress-pdf-java-tutorial/):ページを削除した後のファイルサイズを小さくし、配布のために最適化
- [Javaでのアウトラインとブックマーク](https://ironpdf.com/java/how-to/bookmarks/):削除したページを参照する可能性のあるナビゲーション構造を管理する
- [IronPDF for Javaの例](https://ironpdf.com/java/examples/):IronPDF Java APIの完全なコードサンプルをコピー貼り付け
[無料トライアルを開始](#trial-license)して、JavaワークフローでのPDFからページを削除します。 生産用途のライセンスを購入するには、[ライセンスオプションを表示](#licensing)してください。
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.javaimport java.io.IOException;import java.nio.file.Path;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;public class Main { public static void main(String[] args) throws IOException { // Set the license key before calling any IronPDF methodsLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load the source PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf")); // Remove the first page (zero-based index 0) pdf.removePage(0); // Save the modified PDF to a new file pdf.saveAs(Path.of("modified.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf"));
// Remove the first page (zero-based index 0)
pdf.removePage(0);
// Save the modified PDF to a new file
pdf.saveAs(Path.of("modified.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/single-page.javaimport java.io.IOException;import java.nio.file.Path;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;public class Main { public static void main(String[] args) throws IOException { // Set the license key before calling any IronPDF methodsLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load the source PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("report.pdf")); // Remove the cover page at index 0 (the first page) pdf.removePage(0); // Save the modified PDF -- the original file is not overwritten pdf.saveAs(Path.of("report-no-cover.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/single-page.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("report.pdf"));
// Remove the cover page at index 0 (the first page)
pdf.removePage(0);
// Save the modified PDF -- the original file is not overwritten
pdf.saveAs(Path.of("report-no-cover.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/page-range.javaimport java.io.IOException;import java.nio.file.Path;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;import com.ironsoftware.ironpdf.edit.PageSelection;public class Main { public static void main(String[] args) throws IOException { // Set the license key before calling any IronPDF methodsLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load a multi-page PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("annual-report.pdf")); // Remove pages 3, 4, and 5 using a zero-based inclusive range (indexes 2 to 4) pdf.removePages(PageSelection.pageRange(2, 4)); // Save the result to a new file pdf.saveAs(Path.of("annual-report-trimmed.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/page-range.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load a multi-page PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("annual-report.pdf"));
// Remove pages 3, 4, and 5 using a zero-based inclusive range (indexes 2 to 4)
pdf.removePages(PageSelection.pageRange(2, 4));
// Save the result to a new file
pdf.saveAs(Path.of("annual-report-trimmed.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/non-consecutive-pages.javaimport java.io.IOException;import java.nio.file.Path;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;public class Main { public static void main(String[] args) throws IOException { // Set the license key before calling any IronPDF methodsLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load a six-page PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("contract.pdf")); // Remove non-consecutive pages: always work from highest index to lowest // to prevent index shifting from affecting subsequent removals. // Remove the last page (index 5 in a six-page document) pdf.removePage(5); // Remove a page in the middle (index 3 -- now safe because no lower index has shifted) pdf.removePage(3); // Remove the first page (index 0 -- lowest, so processed last) pdf.removePage(0); // Save the modified PDF pdf.saveAs(Path.of("contract-redacted.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/non-consecutive-pages.java
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load a six-page PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("contract.pdf"));
// Remove non-consecutive pages: always work from highest index to lowest
// to prevent index shifting from affecting subsequent removals.
// Remove the last page (index 5 in a six-page document)
pdf.removePage(5);
// Remove a page in the middle (index 3 -- now safe because no lower index has shifted)
pdf.removePage(3);
// Remove the first page (index 0 -- lowest, so processed last)
pdf.removePage(0);
// Save the modified PDF
pdf.saveAs(Path.of("contract-redacted.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/dynamic-removal.javaimport java.io.IOException;import java.nio.file.Path;import java.util.Arrays;import java.util.Comparator;import java.util.List;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;public class Main { public static void main(String[] args) throws IOException { // Set the license key before calling any IronPDF methodsLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load the PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("document.pdf")); // Define the zero-based page indexes to remove List<Integer> pagesToRemove = Arrays.asList(0, 3, 5); // Sort descending to avoid index-shift errors during sequential removal pagesToRemove.sort(Comparator.reverseOrder()); // Remove each page in reverse-index order for (int pageIndex : pagesToRemove) { pdf.removePage(pageIndex); } // Save the modified PDF pdf.saveAs(Path.of("document-pages-removed.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/dynamic-removal.java
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
public class Main {
public static void main(String[] args) throws IOException {
// Set the license key before calling any IronPDF methods
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("document.pdf"));
// Define the zero-based page indexes to remove
List<Integer> pagesToRemove = Arrays.asList(0, 3, 5);
// Sort descending to avoid index-shift errors during sequential removal
pagesToRemove.sort(Comparator.reverseOrder());
// Remove each page in reverse-index order
for (int pageIndex : pagesToRemove) {
pdf.removePage(pageIndex);
}
// Save the modified PDF
pdf.saveAs(Path.of("document-pages-removed.pdf"));
}
}
How can I handle index-shift errors when removing PDF pages in IronPDF?
Avoid index-shift errors by sorting the page indexes in descending order before removal and processing from the highest to lowest index, or use the `removePages` method for contiguous page ranges.
How do you save a PDF after removing pages with IronPDF in Java?
After removing pages, save the modified PDF using the `saveAs(Path path)` method, specifying the file path for the new PDF.
What methods does IronPDF provide for PDF page manipulation beyond deletion?
IronPDF offers methods for merging, splitting, and adding watermarks to PDF documents, in addition to page deletion, all accessible through its Java API.
Where can I find additional IronPDF Java examples for PDF manipulation?
Visit the IronPDF for Java examples page for copy-paste code samples and an overview of the full API, including document manipulation capabilities beyond page deletion.