Split Files via Java
Files splitting with the help of server-side Java Library.
Split Files Using Java Library
Need just part of your large files? To separate one or more parts from your document, you must split the pages of the original file. In order to split files, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml. Our software tool also provides developers to split Word, PDF, HTML, TXT, and DOCX documents into parts. For more details please learn Documentation Pages.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>
Split documents via Java
You need Aspose.PDF for Java to try the code in your environment.
- Load the PDF with an instance of Document.
- Create a new Document class object to split PDF pages.
- Add current page to the document.
- Save current page as a separate PDF
System Requirements
Just make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
- Development environment like Eclipse or IntelliJ IDEA.
- Aspose.PDF for Java library referenced in your project.
How to split PDFs using Java
With the Aspose.PDF for Java library, you can split large PDF documents. Splitting a PDF document is a common use case when working with PDF documents. It helps reduce the size of a PDF file by breaking large documents into smaller files to send via email.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>
Split PDF - Java.
//Read the source PDF file
doc = new com.aspose.pdf.Document("1.pdf");
//Instantiate PdfFileEditor object
pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();
//Split the PDF file into two halves
pdfEditor.extract(pathSource, 1, doc.getPages().size() / 2, "pdf_half.pdf");
How to split HTML files using Java
HTML documents can be large as they can contain text, images, charts, and more. You may need to separate HTML files according to different requirements or use cases. The Aspose.PDF for Java library will help you with this task.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>
Split HTML - Java.
//Read the source HTML file
doc = new com.aspose.pdf.Document(pathSource, new com.aspose.pdf.HtmlLoadOptions());
//save input html to pdf to file
doc.save("test.pdf", com.aspose.pdf.SaveFormat.Pdf);
//Instantiate PdfFileEditor object
pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();
//slit first page
pdfEditor.splitFromFirst("test.pdf", 1, "test.pdf");
//Convert first pdf page to html
outputDoc = new com.aspose.pdf.Document("test.pdf");
outputDoc.save("first_page.html", com.aspose.pdf.SaveFormat.Html);
How to split TXT files using Java
The Aspose.PDF for Java library allows Java developers to split TXT files into separate parts. The function of splitting a TXT document into separate files will help you work with different sections of a large document at the same time and for several users. Splitting a TXT document will speed up and streamline your work. Use the following code snippet to extract the pages of your TXT file.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
<classifier>jdk17</classifier>
</dependency>
Split TXT - Java.
//Read the source TXT file to Aspose Document
doc = new com.aspose.pdf.Document(pathSource, new com.aspose.pdf.TxtLoadOptions());
//Instantiate PdfFileEditor object
pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();
//save input text to pdf to file
doc.save("test.pdf", com.aspose.pdf.SaveFormat.Pdf);
//split pdf to pages
ByteArrayInputStream[] pages = pdfEditor.splitToPages("test.pdf");
int index = 1;
//5. save each page to text file
//extract text from page
for(var ms : pages) {
page = new com.aspose.pdf.Document(ms);
textAbsorber = new com.aspose.pdf.TextAbsorber();
page.getPages().accept(textAbsorber);
String extractedText = textAbsorber.getText();
Files.writeString(Path.of("text_"+ Integer.toString(index)+".txt"), extractedText);
index++;
}