如何在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中的所有页面索引使用零基编号。 您的文档的第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

微软MVP

查看案例研究
Icon Quote related to 如何从PDF中删除单个页面?

IronOCR 意味着我们每年可以节省 $40,000 的人工处理成本,同时提高生产力,并释放资源用于高影响任务。我强烈推荐它。

Brent Matzelle related to 如何从PDF中删除单个页面?

Brent Matzelle

首席技术官,OPYN

查看案例研究
Icon Quote related to 如何从PDF中删除单个页面?

Iron Suite 在我们的运营中起着至关重要的作用。这些工具提高了业务各方面的效率,包括创建平面图和改善库存管理。

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时,每次删除都会将所有后续页面索引下移一位。从高索引到低索引删除确保每次调用都针对正确的页面,因为还没有较低索引的页面被删除导致移动。

IronPDF Java中的PageSelection是什么?

PageSelection是位于com.ironsoftware.ironpdf.edit包中的一个类,它定义了一个方法应针对哪些页面。PageSelection.pageRange(fromIndex, toIndex)创建一个覆盖一块连续零起始、包容的页面索引的选择,以用于removePages

使用IronPDF在Java中删除PDF页面的前提条件是什么?

您需要Java 8或更高版本,作为Maven或Gradle依赖项添加IronPDF库,并使用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文件。