How to extract PDF pages in Java

Extract Pages from PDF in Java - Separate PDF Pages Online.

If you are working with PDF files, you may find yourself in a situation where you need to extract pages from a PDF file. Extracting pages from a PDF file can be a time-consuming task, especially if you have to do it manually or deal with large documents. Fortunately, with the help of GroupDocs.Merger Cloud SDK for Java, you can easily extract pages from a PDF file programmatically. In this article, we will explore how to extract pages from PDF in Java.

The following topics shall be covered in this article:

Java PDF Pages Extractor REST API and SDK Installation

GroupDocs.Merger Cloud SDK for Java is a cloud-based API that allows developers to merge, extract, split, reorder, and remove pages from files and other types of documents, including Word, Excel, PowerPoint, HTML, PDF, and many more. This powerful SDK is easy to use and can be integrated into a Java-based application to automate file manipulation tasks.

You can either download the API’s JAR file or install it using Maven by adding the following repository and dependency into your project’s pom.xml file:

Maven Repository:

<repository>
    <id>groupdocs-artifact-repository</id>
    <name>GroupDocs Artifact Repository</name>
    <url>https://repository.groupdocs.cloud/repo</url>
</repository>

Maven Dependency:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-merger-cloud</artifactId>
    <version>23.2</version>
    <scope>compile</scope>
</dependency>

Now, you need to sign up for a free trial account or purchase a subscription plan on the GroupDocs website to get your API key. Once you have the Client Id and Client Secret, add below code snippet to a Java-based application:

How to Extract PDF Pages by Exact Page Numbers in Java

To extract pages from a PDF file using GroupDocs.Merger Cloud SDK for Java, you will need to follow these steps:

Upload the Files

Firstly, upload the PDF file to the cloud using the code example given below:

As a result, the uploaded PDF file will be available in the [files section][https://dashboard.groupdocs.cloud/files] of your dashboard on the cloud.

Extract Pages from PDF Files in Java

Here’s a step-by-step guide and an example code snippet on how to extract pages from a PDF file using GroupDocs.Merger Cloud SDK for Java:

  • Firstly, import the required classes into your Java file.
  • Secondly, create an instance of the PagesApi class.
  • Thirdly, create an instance of the FileInfo class.
  • After that, set the source input file path.
  • Now, create an instance of the ExtractOptions() class.
  • Then, define extract options setFileInfo, setOutputPath, and setPages collection in array format.
  • Now, create an instance of the ExtractRequest() class and pass the ExtractOptions parameter.
  • Finally, extract pages by calling the extract() method of the PagesApi and passing the ExtractRequest parameter.

The following code snippet shows how to separate PDF files into individual pages in Java using REST API:

Download the File

The above code sample will save specific pages of PDF on the cloud. You can download it using the following code sample:

How to Extract Pages from PDF by Page Ranges using Java

In this section we will cover a step-by-step guide and an example code snippet to save selected pages from PDF using Java by applying the pages range:

  • Firstly, import the required classes into your Java file.
  • Secondly, create an instance of the PagesApi class.
  • Thirdly, create an instance of the FileInfo class.
  • After that, set the source input file path.
  • Now, create an instance of the ExtractOptions() class.
  • Then, define extract options setFileInfo, setOutputPath, setStartPageNumber, and setEndPageNumber.
  • Next, set page options setRangeMode to EVENPAGES.
  • Now, create an instance of the ExtractRequest() class and pass the ExtractOptions parameter.
  • Finally, extract pages by calling the extract() method of the PagesApi and passing the ExtractRequest parameter.

The following code snippet shows how to extract PDF pages by applying pages range and mode in Java:

Free Online PDF Page Extractor

How to extract PDF pages for free? Please try the free PDF pages extractor to extract specific pages from PDF for free. This online PDF page extractor is developed using the above-mentioned Groupdocs.Merger Cloud APIs.

Conclusion

GroupDocs.Merger Cloud SDK for Java provides an easy way to extract PDF pages to new PDF in Java. The following is what you have learned in this article:

  • how to separate PDF files into multiple pages by number using Java;
  • programmatically upload and download the PDF file using Java on the cloud;
  • separate PDF into individual pages in Java using page ranges;
  • and extract pages from PDF online for free using a free PDF pages extractor online.

Additionally, we also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Java SDK’s complete source code is freely available on Github. Please check the GroupDocs.Merger Cloud SDK for Java Examples here.

Moreover, we suggest you follow our Getting Started guide for detailed steps and API usage.

Finally, we keep writing new blog posts on different document operations using REST API. So, please get in touch for the regular updates.

Ask a question

If you have any questions about the PDF pages extractor API, please feel free to ask us on the Free Support Forum.

FAQs

How do I extract pages from a PDF file in Java?

You can extract pages from a PDF file using GroupDocs.Merger Cloud SDK for Java.

Is it possible to export a single page from PDF in Java?

Yes, you can extract PDF pages into single files programmatically in Java using GroupDocs.Merger Cloud SDK for Java.

What file formats can I extract pages from using GroupDocs.Merger Cloud SDK for Java?

In addition to PDF files, GroupDocs.Merger Cloud SDK for Java can extract pages from Microsoft Word, Excel, PowerPoint, HTML, and other file formats.

See Also

Here are some related articles that you may find helpful: