# 如何在Java中分割PDF
在Java中分割PDF很簡單,IronPDF提供`PdfDocument`類上,可以用來提取單個頁面、抽取頁面範圍或在邊界處分割文件。 每個方法都會返回一個新的`PdfDocument`,您可以獨立於原件保存。
快速開始:在Java中分割PDF
1. 透過Maven或Gradle安裝 IronPDF for Java
2. 使用`License.setLicenseKey()`設置您的授權金鑰
3. 使用`PdfDocument.fromFile()`載入PDF
4. 使用`copyPage()`提取單個頁面
5. 使用`saveAs()`保存結果
```java
:title=Quickstart
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-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"));
// Extract the first page (zero-based index 0) into a new document
PdfDocument firstPage = pdf.copyPage(0);
// Save the extracted page as a standalone PDF
firstPage.saveAs(Path.of("page-one.pdf"));
}
}
```
程式化地生成文件時,分割PDF是一個常見需求。 生成的一個報告可能需要在分發前拆分成單獨的部分。 歸檔工作流通常需要將每個章節或發票儲存為單獨的文件。在選擇性共享場景中,如只向客戶發送特定頁面的範圍,提取所需的頁面比發送整個文件更有利。
IronPDF通過一個一致的API在`PdfDocument`類上處理所有三種分割模式。 用來從HTML生成PDF或載入現有文件的相同類,公開了頁面提取方法,因此分割自然整合到任何現有IronPDF工作流中。 有關設置和依賴項配置的資料,請參閱[入門概述](https://ironpdf.com/java/docs/)。
IronPDF中的所有頁面索引都是從零開始的。 如果您的原件文件有十頁,有效索引範圍從0到9。這適用於`splitBy`的邊界參數。 傳入一個超出範圍的索引將在運行時拋出異常,因此在處理可變長度的文件時,請對頁面計數進行驗證。
<div class="hsg-featured-snippet">
<h2>如何在Java中分割PDF</h2>
<ol>
<li><a href="https://ironpdf.com/java/#download-modal">安裝Java程式庫以分割PDFs</a></li>
<li>使用<strong>PdfDocument</strong>類載入源PDF</li>
<li>使用<code>copyPage</code>將單個頁面提取到新文件中</li>
<li>使用<code>copyPages</code>提取一個頁面範圍</li>
<li>使用<code>splitBy</code>將PDF在特定頁面邊界處分割</li>
<li>使用<code>saveAs</code>保存每個生成的文件</li>
</ol>
</div>
## 開始之前我需要什麼準備?
在分割PDFs之前,請確認已將IronPDF新增到您的Java專案中作為Maven或Gradle的依賴項。 程式庫需要Java 8或更高版本。 從Maven Central新增IronPDF artifact到您的構建文件中:
```xml
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2024.9.1</version>
</dependency>
```
對於Gradle專案,將等效的`build.gradle`中。 完整的依賴項和配置指導可在[入門概述](https://ironpdf.com/java/docs/)中找到。
需要有效的授權金鑰。 在應用啟動時設置它,然後再調用任何IronPDF方法:
```java
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
```
有關授權選項和試用金鑰,請存取[授權金鑰指南](https://ironpdf.com/java/get-started/license-keys/)。
[[t:(IronPDF中的全部頁面索引使用從0開始的編號。 文件的第1頁為索引0,第2頁為索引1,依此類推。)]]
## 如何從PDF中提取單個頁面?
`PdfDocument`返回。 原始文件不會被修改。 `1`代表第二頁,以此類推。
當您的工作流需要隔離特定頁面時,這個方法是合適的,比如提取封面頁,簽名頁或從批量生成的文件中提取單據。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-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("source.pdf"));
// Extract the third page (zero-based index 2) into a new document
PdfDocument thirdPage = pdf.copyPage(2);
// Save the extracted page as a standalone PDF
thirdPage.saveAs(Path.of("third-page.pdf"));
}
}
```
返回的`PdfDocument`完全獨立於源文件。 在保存前,您可以進一步轉換,新增註釋,或與其他文件合併。
## 如何從PDF中提取一個頁面範圍?
`PdfDocument`返回。 `copyPages(1, 4)`將提取頁面2, 3, 4和5(索引值1到4)。
頁面範圍提取適合的場景有: 從多章節報告中抽取一個章節,提取一個多頁的附錄,或從導出為PDF的演示中隔離一組幻燈片。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-page-range.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 multi-page source PDF
PdfDocument pdf = PdfDocument.fromFile(Path.of("multi-page-report.pdf"));
// Extract pages 2 through 5 (zero-based indexes 1 through 4, inclusive)
PdfDocument chapter = pdf.copyPages(1, 4);
// Save the extracted page range as a standalone document
chapter.saveAs(Path.of("chapter-pages-2-to-5.pdf"));
}
}
```
如果源文件的頁面數少於請求的`toIndex`,IronPDF將拋出異常。 在調用`pdf.getPageCount()`,當操作的文件頁面數可能會有所變化時。
## 如何將PDF分成兩個文件?
`List<PdfDocument>`,其中包含正好兩個元素。 第一個元素包含從索引0到`splitAfterPageIndex`(包含)的頁面。 第二個元素包含從`splitAfterPageIndex + 1`到文件結束的所有剩餘頁面。
例如,在一份十頁的文件上調用`splitBy(2)`將產生:
- `parts.get(0)`:頁面0, 1, 2(前三頁)
- `parts.get(1)`:頁面3到9(剩餘的七頁)
此方法適合在已知邊界處分割文件:將封面與正文分開,在段落分割處分割文件,或者將一個批處理文件分成兩半以便並行處理。
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/split-by-boundary.java
import java.io.IOException;
import java.nio.file.Path;
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 source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf"));
// Split after page index 2 -- first part: pages 0-2, second part: pages 3 to end
List<PdfDocument> parts = pdf.splitBy(2);
// Save the first part (pages 0 through 2)
parts.get(0).saveAs(Path.of("part-one.pdf"));
// Save the second part (pages 3 through end)
parts.get(1).saveAs(Path.of("part-two.pdf"));
}
}
```
[[i:(`splitBy`總是返回一個包含恰好兩個元素的列表。 如果`splitAfterPageIndex`等於文件的最後一頁索引,那麼第二個元素將是一個空文件。 在分割點是動態計算時,根據`pdf.getPageCount() - 1`驗證分割邊界。)]]
## 在Java中分割PDF的下一步是什麼?
IronPDF的`splitBy`方法涵蓋了Java中PDF分割的全範圍需求,從單頁提取到基於範圍的分割,甚至到基於邊界的分割。 每個方法都返回一個獨立的`PdfDocument`,準備進一步操作或立即保存。
要繼續在Java中處理PDF文件,請探索這些相關資源:
- [在Java中合併PDFs](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/):將多個PDF文件或提取的部分合併為單一文件
- [在Java中刪除PDF頁面](https://ironpdf.com/java/how-to/java-delete-pdf-pages-tutorial/):從現有的PDF中移除不需要的頁面而不提取其餘部分
- [在Java中壓縮PDFs](https://ironpdf.com/java/how-to/compress-pdf-java-tutorial/):在儲存或傳輸前減小分割文件的文件大小
- [Java PDF教程](https://ironpdf.com/java/tutorials/html-to-pdf/):針對常見PDF工作流程的其他端到端範例
- [IronPDF for Java範例](https://ironpdf.com/java/examples/):複製和粘貼整個IronPDF Java API的程式碼范例
[開始您的免費試用](#trial-license),以將PDF分割新增到您的Java工作流程中。 若要購買用於生產的授權,請[查看授權選項](#licensing)。
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-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")); // Extract the first page (zero-based index 0) into a new document PdfDocument firstPage = pdf.copyPage(0); // Save the extracted page as a standalone PDF firstPage.saveAs(Path.of("page-one.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-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"));
// Extract the first page (zero-based index 0) into a new document
PdfDocument firstPage = pdf.copyPage(0);
// Save the extracted page as a standalone PDF
firstPage.saveAs(Path.of("page-one.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-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("source.pdf")); // Extract the third page (zero-based index 2) into a new document PdfDocument thirdPage = pdf.copyPage(2); // Save the extracted page as a standalone PDF thirdPage.saveAs(Path.of("third-page.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-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("source.pdf"));
// Extract the third page (zero-based index 2) into a new document
PdfDocument thirdPage = pdf.copyPage(2);
// Save the extracted page as a standalone PDF
thirdPage.saveAs(Path.of("third-page.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-page-range.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 multi-page source PDF PdfDocument pdf = PdfDocument.fromFile(Path.of("multi-page-report.pdf")); // Extract pages 2 through 5 (zero-based indexes 1 through 4, inclusive) PdfDocument chapter = pdf.copyPages(1, 4); // Save the extracted page range as a standalone document chapter.saveAs(Path.of("chapter-pages-2-to-5.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-page-range.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 multi-page source PDF
PdfDocument pdf = PdfDocument.fromFile(Path.of("multi-page-report.pdf"));
// Extract pages 2 through 5 (zero-based indexes 1 through 4, inclusive)
PdfDocument chapter = pdf.copyPages(1, 4);
// Save the extracted page range as a standalone document
chapter.saveAs(Path.of("chapter-pages-2-to-5.pdf"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/split-by-boundary.javaimport java.io.IOException;import java.nio.file.Path;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 source PDF from disk PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf")); // Split after page index 2 -- first part: pages 0-2, second part: pages 3 to end List<PdfDocument> parts = pdf.splitBy(2); // Save the first part (pages 0 through 2) parts.get(0).saveAs(Path.of("part-one.pdf")); // Save the second part (pages 3 through end) parts.get(1).saveAs(Path.of("part-two.pdf")); }}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/split-by-boundary.java
import java.io.IOException;
import java.nio.file.Path;
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 source PDF from disk
PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf"));
// Split after page index 2 -- first part: pages 0-2, second part: pages 3 to end
List<PdfDocument> parts = pdf.splitBy(2);
// Save the first part (pages 0 through 2)
parts.get(0).saveAs(Path.of("part-one.pdf"));
// Save the second part (pages 3 through end)
parts.get(1).saveAs(Path.of("part-two.pdf"));
}
}
How does the `splitBy` method function in IronPDF?
The `splitBy(int splitAfterPageIndex)` method divides a PDF at a specified boundary, returning a list with two `PdfDocument` parts, split at the given index.
What is the page index base used in IronPDF?
IronPDF uses zero-based numbering for page indexes. For example, the first page is indexed as 0.
What are the prerequisites for splitting a PDF using IronPDF in Java?
Before splitting a PDF using IronPDF, ensure the IronPDF library is integrated as a Maven or Gradle dependency, and a valid license key is set.
Where can you find more examples of using IronPDF for Java?
More examples and code samples for using IronPDF with Java can be found in the IronPDF documentation and on the official website's example section.