如何在Java中刪除PDF的頁面

This article was translated from English: Does it need improvement?
Translated
View the article in English

在Java中使用IronPDF刪除PDF頁面很簡單:這個程式庫提供PageSelection來支持,讓您精準控制要刪除哪些頁面—無論是單一頁面、連續範圍還是分散的頁碼集合。 IronPDF中的所有頁碼都是從零開始的,因此文件的第一頁總是索引0。

快速入門:在Java中刪除PDF頁面

  1. 透過Maven或Gradle安裝 IronPDF for Java
  2. 使用License.setLicenseKey()設定您的授權金鑰
  3. 使用PdfDocument.fromFile()載入PDF
  4. 使用removePage()刪除單一頁面
  5. 使用saveAs()儲存結果
 :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"));
    }
}
 :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"));
    }
}
JAVA

從PDF中刪除頁面是常見的文件處理任務。 您可能需要在分發報告之前剝去封面頁,在外部共享文件之前刪除機密部分,或清理掃描儀或模板引入的空白頁。 IronPDF通過一致的Java API處理所有這些情況,而不需要在主機上安裝任何原生PDF編輯工具。

該程式庫通過Maven或Gradle整合到Java應用程式中,並支持超出頁面刪除的完整PDF操作範圍,包括合併PDF分割文件新增水印。 欲了解設置和可用功能的完整概覽,請存取入門概覽

本指南中的範例涵蓋三種情景:按索引刪除單頁、使用PageSelection刪除連續頁範圍,以及安全地刪除多個非連續頁面而不會引發索引移位錯誤。

開始之前我需要什麼準備?

在從PDF中刪除頁面之前,確認IronPDF已在您的Java專案中配置。 該程式庫需要Java 8或更高版本,並透過Maven或Gradle整合。 將IronPdf依賴新增到您的專案構建文件中。欲了解完整設置說明,請參閱入門概覽

開發和生產使用都需要有效的授權金鑰。 在調用任何IronPDF方法之前,在您的應用程式開頭設置金鑰。 有關授權選項的詳細資訊,請存取授權金鑰指南

提示IronPDF中的全部頁面索引使用從0開始的編號。 文件的第1頁為索引0,第2頁為索引1,依此類推。

如何從PDF中刪除單頁?

removePage(int pageIndex)方法接受從零開始的頁面索引,並從文件中精確刪除該頁。 呼叫完成後,所有後續頁面向下移動一個位置,因此您在呼叫前快取的任何索引可能不再指向同一頁。

例如,如果文件有五頁(索引0到4),並且您刪除索引2,那麼原本在索引3的頁面現在在索引2,原本在索引4的頁面現在在索引3。特別是在多次連續呼叫removePage時,請考慮到這種移位來規劃您的刪除順序。

//: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/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"));
    }
}
JAVA

當您確切知道要刪除哪一頁並且只需要刪除一頁時,這種模式是最簡單的方法。 如需在單次操作中刪除一個連續頁面範圍,請改用PageSelection範圍。

Icon Quote related to 如何從PDF中刪除單頁?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 如何從PDF中刪除單頁?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 如何從PDF中刪除單頁?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 如何從PDF中刪除單頁?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 如何從PDF中刪除單頁?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 如何從PDF中刪除單頁?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

如何從PDF中刪除一個頁面範圍?

removePage時出現的索引移動問題。 使用PageSelection.pageRange(int fromIndex, int toIndex)制定範圍—兩個端點都包括在內並且是從零開始的。

下面的範例通過傳遞toIndex = 4來刪除文件的第3、4和5頁。 由於所有三頁都一次性刪除,因此在操作過程中不會發生中間索引移位。

//: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/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"));
    }
}
JAVA

每當您需要刪除一塊連續的頁面時,PageSelection.pageRange是首選方法。 它比逐個迴圈removePage呼叫更簡潔和高效,單次操作語義意味著頁面計數僅更新一次。

如何刪除多個非連續頁面?

當您需要刪除彼此不相鄰的頁面時,有兩個實用選擇:使用removePage

如果您多次呼叫removePage,請始終從最高索引逐次處理到最低。首先刪除較低索引的頁面會將所有更高的索引向下移動一個位置,這將導致後續呼叫定位錯誤的頁面。 從文件末尾開始並反向操作,每次刪除操作會保持其餘的較低索引不受影響。

下面的範例刪除了一個六頁文件的首頁、一個中間頁面和最後一頁。 呼叫按索引從高到低排序—5、3、0—以防止索引漂移。

//: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/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"));
    }
}
JAVA

請注意在運行時建立要刪除的頁面索引列表時—例如來自使用者輸入或配置文件—在遍歷之前按降序對列表進行排序。 這確保每個removePage呼叫無論已經刪除了多少頁,都能正確定位。

如果要刪除的頁面集是動態確定的,一個簡潔的模式是將您的索引列表反向排序然後迴圈:

//: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"));
    }
}
//: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

在迴圈之前按降序排序索引列表是一種安全的一般用途模式,無論有多少頁面被針對或輸入列表的順序如何都能起作用。

刪除Java中PDF頁面的下一步是什麼?

IronPDF的removePages方法提供了針對頁面刪除的精確控制—無論您需要刪除單一頁面、剔除範圍還是刪除分散的頁集。 基於零的索引模型在整個IronPDF Java API中是一致的,因此在您進行頁面拆分、合併或重新排序時應用相同的約定。

要繼續操作PDF頁面結構和文件操作,請探索這些相關資源:

開始您的免費試用在您的Java工作流程中刪除PDF中的頁面。 若要購買用於生產的授權,請查看授權選項

常見問題

如何在Java中從PDF刪除單個頁面?

使用PdfDocument.fromFile()載入PDF,然後使用從零開始的索引呼叫pdf.removePage(pageIndex)。例如,pdf.removePage(0)移除第一頁。使用pdf.saveAs()儲存結果。

如何在Java中從PDF刪除頁面範圍?

呼叫pdf.removePages(PageSelection.pageRange(fromIndex, toIndex))。起點和終點索引都是從零開始且包含在內。例如,PageSelection.pageRange(2, 4)在單次原子操作中移除文件的第三、第四和第五頁。

為什麼需要從最高索引到最低索引刪除頁面?

當您多次呼叫removePage時,每次移除都會使所有隨後的頁面索引向下移動一位。從最高到最低索引移除可以確保每次呼叫都針對正確的頁面,因為還沒有移除較低索引的頁面而引起的偏移。

PageSelection在IronPDF Java中是什麼?

PageSelectioncom.ironsoftware.ironpdf.edit套件中的一個類別,定義方法應作用於哪些頁面。PageSelection.pageRange(fromIndex, toIndex)建立一個選擇範圍,用於removePages的連續零基區塊索引。

在Java中使用IronPDF刪除PDF頁面的先決條件是什麼?

您需要Java 8或更高版本,將IronPDF程式庫新增為Maven或Gradle依賴項,並使用License.setLicenseKey()設定有效的授權金鑰,然後才能進行任何IronPDF呼叫。不需要本地PDF工具或額外的框架。

removePage會修改原始PDF文件嗎?

不會。removePageremovePages會修改記憶體中的PdfDocument物件。磁碟上的原始文件不變,直到您呼叫pdf.saveAs(Path.of("output.pdf"))將修改的文件寫入新路徑。

Darrius Serrant
全端軟體工程師(WebOps)

Darrius Serrant擁有邁阿密大學的電腦科學學士學位,並在Iron Software擔任全端WebOps行銷工程師。從小就對程式設計有興趣,他認為計算既神秘又易於理解,成為創意和問題解決的完美媒介。

在Iron Software,Darrius喜歡創造新事物並簡化複雜的概念,使其更易於理解。作為我們的常駐開發人員之一,他還志願教學,將他的專業知識傳授給下一代。

對Darrius來說,他的工作是有意義的,因為它有價值且對社會有真正的影響。

準備好開始了嗎?
版本: 2026.6 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想要快速證明嗎?
運行範例 觀看您的HTML變成PDF。