푸터 콘텐츠로 바로가기
JAVA용 IRONPDF 사용

Java에서 PDF 파일을 분할하는 방법

This article will use IronPDF in Java to split PDF files from a source PDF file.

IronPDF Java PDF Library

IronPDF for Java is a Java library that prioritizes accuracy, ease of use, and speed. It is especially designed for Java and is easy to use when working with PDF documents. It inherits all its functionality from the well-established IronPDF Library for .NET Framework. This makes IronPDF for Java a versatile tool for working with PDF documents in various contexts.

IronPDF offers developers methods to render PDF documents into images and extract text and content from PDFs. Additionally, IronPDF is also capable of rendering charts within PDFs, applying watermarks to PDF files, working with PDF forms, and managing digital signatures programmatically.

Steps to Split PDF Files

Prerequisites for Project Setup

To make IronPDF work with PDFs in a Java Maven project, you will need to make sure that you have the following prerequisites:

  1. JDK (Java Development Kit): You must have a current version of Java running on your computer along with an IDE. If you don't have it, then download the latest JDK from the Oracle website. Use any IDE like NetBeans, Eclipse, or IntelliJ.
  2. Maven: To manage your project and dependencies, Maven is an important tool built specifically for Java projects. Download Maven from the Apache Maven website if you don't have it installed.
  3. IronPDF Java Library: Now you require the IronPDF Java library. This can be done by adding the following dependency to your project's pom.xml file. Maven will automatically download and install it in the project.

    <dependency>
        <groupId>com.ironpdf</groupId>
        <artifactId>ironpdf</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.ironpdf</groupId>
        <artifactId>ironpdf</artifactId>
        <version>1.0.0</version>
    </dependency>
    XML
  4. Another dependency required is SLF4J. Add the SLF4J dependency in the pom.xml file.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    XML

Once everything is downloaded and installed in your split PDF file Java program, you are ready to use the IronPDF library.

Import Classes

Firstly, import the IronPDF required classes in Java code. Add the following code on top of the "Main.java" file:

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;

import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;

import java.io.IOException;
import java.nio.file.Paths;
JAVA

Now, set your license key using the IronPDF setLicenseKey() method in the Main method:

License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
JAVA

Create or Open an Existing PDF File

To split a PDF file into multiple single-page files, it is necessary to either create a PDF file with multiple pages or open a PDF file with multiple pages.

The next code sample will create a four-page PDF that can be used for this demonstration.

// HTML content used to create a four-page PDF
String html = "<p> [PDF With Multiple Pages] </p>"
        + "<p> 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 2nd Page</p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 3rd Page</p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 4th Page</p>";

// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);

// Save the PDF document
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
// HTML content used to create a four-page PDF
String html = "<p> [PDF With Multiple Pages] </p>"
        + "<p> 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 2nd Page</p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 3rd Page</p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> 4th Page</p>";

// Render HTML to a PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);

// Save the PDF document
pdf.saveAs(Paths.get("assets/multiplePages.pdf"));
JAVA

The PDF Document looks like this:

How to Split PDF Files in Java, Figure 1: Creating New PDFs with IronPDF Creating New PDFs with IronPDF

Open PDF File for Splitting

As an alternative to the previous section, the next code sample uses the PdfDocument.fromFile method to open an existing PDF file using IronPDF.

// Open the existing PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
// Open the existing PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));
JAVA

You can also open a password-protected file by providing a document password as a second argument to the fromFile method.

Split a PDF File into Multiple PDF Files

To split a PDF file, the code is straightforward. Simply copy several pages from the original document using the copyPage method, as shown below:

// Take the first page
PdfDocument page1Doc = pdf.copyPage(0);
page1Doc.saveAs(Paths.get("assets/split1.pdf"));

// Take the second page
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));

// Take the third page
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));

// Take the fourth page
PdfDocument page4Doc = pdf.copyPage(3);
page4Doc.saveAs(Paths.get("assets/split4.pdf"));
// Take the first page
PdfDocument page1Doc = pdf.copyPage(0);
page1Doc.saveAs(Paths.get("assets/split1.pdf"));

// Take the second page
PdfDocument page2Doc = pdf.copyPage(1);
page2Doc.saveAs(Paths.get("assets/split2.pdf"));

// Take the third page
PdfDocument page3Doc = pdf.copyPage(2);
page3Doc.saveAs(Paths.get("assets/split3.pdf"));

// Take the fourth page
PdfDocument page4Doc = pdf.copyPage(3);
page4Doc.saveAs(Paths.get("assets/split4.pdf"));
JAVA

The PDF file is split by passing the index number as an argument to the copyPage method. Then, save each page in a separate file.

Page 1

Page 1

Page 2

Page 2

Page 3

Page 3

Page 4

Page 4

The copyPages method can also split a PDF by a range of pages. Below, we split the sample PDF evenly in half.

// Copy the first two pages into a new PDF document
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));

// Copy the last two pages into another PDF document
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
// Copy the first two pages into a new PDF document
PdfDocument halfPages = pdf.copyPages(0, 1);
halfPages.saveAs(Paths.get("assets/halfPages.pdf"));

// Copy the last two pages into another PDF document
PdfDocument endPages = pdf.copyPages(2, 3);
endPages.saveAs(Paths.get("assets/endPages.pdf"));
JAVA

How to Split PDF Files in Java, Figure 6: Splitting a PDF into Two Halves Splitting a PDF into Two Halves

The complete code example is shown below:

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("Your license key");

        // HTML content to create a four-page PDF document
        String html = "<p> [PDF With Multiple Pages] </p>"
                + "<p> 1st Page </p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 2nd Page</p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 3rd Page</p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 4th Page</p>";

        // Render HTML to a PDF document
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
        pdf.saveAs(Paths.get("assets/multiplePages.pdf"));

        // Open the existing PDF document
        pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));

        // Split each PDF page into separate documents
        PdfDocument page1Doc = pdf.copyPage(0);
        page1Doc.saveAs(Paths.get("assets/split1.pdf"));

        PdfDocument page2Doc = pdf.copyPage(1);
        page2Doc.saveAs(Paths.get("assets/split2.pdf"));

        PdfDocument page3Doc = pdf.copyPage(2);
        page3Doc.saveAs(Paths.get("assets/split3.pdf"));

        PdfDocument page4Doc = pdf.copyPage(3);
        page4Doc.saveAs(Paths.get("assets/split4.pdf"));

        // Split the PDF into two halves
        PdfDocument halfPages = pdf.copyPages(0, 1);
        halfPages.saveAs(Paths.get("assets/halfPages.pdf"));

        PdfDocument endPages = pdf.copyPages(2, 3);
        endPages.saveAs(Paths.get("assets/endPages.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.License;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {

        // Set the IronPDF license key
        License.setLicenseKey("Your license key");

        // HTML content to create a four-page PDF document
        String html = "<p> [PDF With Multiple Pages] </p>"
                + "<p> 1st Page </p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 2nd Page</p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 3rd Page</p>"
                + "<div style='page-break-after: always;'></div>"
                + "<p> 4th Page</p>";

        // Render HTML to a PDF document
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
        pdf.saveAs(Paths.get("assets/multiplePages.pdf"));

        // Open the existing PDF document
        pdf = PdfDocument.fromFile(Paths.get("assets/multiplePages.pdf"));

        // Split each PDF page into separate documents
        PdfDocument page1Doc = pdf.copyPage(0);
        page1Doc.saveAs(Paths.get("assets/split1.pdf"));

        PdfDocument page2Doc = pdf.copyPage(1);
        page2Doc.saveAs(Paths.get("assets/split2.pdf"));

        PdfDocument page3Doc = pdf.copyPage(2);
        page3Doc.saveAs(Paths.get("assets/split3.pdf"));

        PdfDocument page4Doc = pdf.copyPage(3);
        page4Doc.saveAs(Paths.get("assets/split4.pdf"));

        // Split the PDF into two halves
        PdfDocument halfPages = pdf.copyPages(0, 1);
        halfPages.saveAs(Paths.get("assets/halfPages.pdf"));

        PdfDocument endPages = pdf.copyPages(2, 3);
        endPages.saveAs(Paths.get("assets/endPages.pdf"));
    }
}
JAVA

IronPDF can also merge PDF documents effortlessly.

Summary

This article explored how a Java program opens an existing PDF document and splits a PDF file into multiple PDFs using the IronPDF Library.

IronPDF makes the life of a developer a lot easier while working with PDF files in Java. Whether you want to create a new document or work with existing PDF documents, IronPDF helps to achieve all PDF-related tasks with almost a single line of code.

You can use IronPDF in production for free, and it can be licensed for commercial use with flexible options. The IronPDF Lite package starts at $799.

자주 묻는 질문

서식을 잃지 않고 Java에서 PDF 파일을 분할하려면 어떻게 해야 하나요?

Java에서 IronPDF를 사용하면 copyPage 메서드를 사용하여 개별 페이지를 별도의 PDF 문서로 생성하여 서식을 잃지 않고 PDF 파일을 분할할 수 있습니다.

PDF 분할을 위한 Java 프로젝트를 설정하는 단계는 무엇인가요?

IronPDF를 사용하여 PDF를 분할하기 위한 Java 프로젝트를 설정하려면 JDK와 Maven이 설치되어 있는지 확인한 다음 Maven 프로젝트에 IronPDF를 종속 요소로 포함하세요. com.ironsoftware.ironpdf.PdfDocument와 같은 필요한 클래스를 가져옵니다.

Java에서 PDF를 여러 페이지로 분할할 수 있나요?

예, IronPDF의 copyPages 메서드를 사용하면 페이지 범위를 지정하여 PDF를 쉽게 분할하여 Java에서 별도의 PDF 문서를 만들 수 있습니다.

Java에서 분할할 때 암호로 보호된 PDF를 처리하려면 어떻게 해야 하나요?

IronPDF를 사용하면 암호로 보호된 PDF를 fromFile 메서드에 암호를 매개변수로 제공하여 문서에 액세스하고 분할할 수 있도록 할 수 있습니다.

IronPDF를 PDF 분할 이외의 작업에도 사용할 수 있나요?

예, Java용 IronPDF는 PDF를 이미지로 렌더링하고, 텍스트를 추출하고, 디지털 서명을 관리하고, PDF 문서를 병합하는 등 다양한 PDF 조작 기능을 제공합니다.

IronPDF를 Java Maven 프로젝트에 통합하려면 어떻게 해야 하나요?

IronPDF의 Maven 종속성을 pom.xml 파일에 추가한 다음 Java 코드에서 필요한 클래스를 가져오는 방식으로 IronPDF를 Java Maven 프로젝트에 통합할 수 있습니다.

Java에서 PDF를 분할할 때 IronPDF를 사용하면 어떤 이점이 있나요?

IronPDF는 copyPagecopyPages와 같은 사용하기 쉬운 방법을 제공하여 Java에서 PDF를 분할하는 프로세스를 간소화하여 문서 서식을 잃지 않고 효율적인 PDF 관리를 보장합니다.

IronPDF는 상업적 사용을 위한 유연한 라이선스 옵션을 제공하나요?

예, IronPDF는 상업적 사용을 위한 유연한 라이선스 옵션과 함께 개발 및 테스트 목적에 이상적인 프로덕션용 무료 버전을 제공합니다.

커티스 차우
기술 문서 작성자

커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.

커티스는 개발 분야 외에도 사물 인터넷(IoT)에 깊은 관심을 가지고 있으며, 하드웨어와 소프트웨어를 통합하는 혁신적인 방법을 연구합니다. 여가 시간에는 게임을 즐기거나 디스코드 봇을 만들면서 기술에 대한 애정과 창의성을 결합합니다.