PDF processing control Aspose.PDF function demonstration: Use Java to combine multiple PDF files into one PDF

Aspose.PDF  is an advanced PDF processing API that can easily generate, modify, convert, render, protect and print documents in cross-platform applications. No need to use Adobe Acrobat. Additionally, the API provides compression options, table creation and manipulation, graphics and image functionality, extensive hyperlink functionality, stamp and watermark tasks, extended security controls, and custom font handling.

Aspose API supports popular file format processing and allows exporting or converting various types of documents to fixed layout file formats and most commonly used image/multimedia formats.

Aspose.pdf latest download (qun: 761297826) icon-default.png?t=N6B9https://www.evget.com/product/4118/download

In various situations, the need to merge two or more PDF documents into a single file is encountered. For example, PDF Merger allows merging documents of similar type. Also, combine multiple PDFs into one before sharing online or sending to others.

In this article, I will demonstrate how to automate this functionality and programmatically merge two or more PDF files using Java.

  • Merge two PDF files into one PDF using Java
  • Merge multiple PDF files using Java
  • Merge PDF files using Java using InputStream object

Currently, the Java version of Aspose.PDF has been upgraded to v20.6, which supports converting PDF to CSV and repairing the abnormality of HOCR data. Interested friends can click the button below to download the latest version.

.NET version of PDF processing control Aspose.PDF function demo: convert XFA to AcroForms in PDF

Merge two PDF files into one PDF using Java

Let's first check the simple scenario of merging just two PDF files, which can be done in a few steps.

  • Create an instance of the PdfFileEditor class.
  • Use the PdfFileEditor.concatenate(String firstInputFile, String secInputFile, String outputFile) method to combine PDF files.

The following code sample shows how to combine two PDF files into one PDF using Java.

// Create PDF editor
PdfFileEditor fileEditor = new PdfFileEditor();
// Merge two PDF files
fileEditor.concatenate("file1.pdf", "file2.pdf", "merged-pdf.pdf");

Merge multiple PDF files using Java

In the previous example, we simply merged two PDF files into one PDF. However, sometimes it may be necessary to merge more than two PDF files. In this case you can pass an array of PDF file paths to the concatenation method. Below are the steps to do this.

  • Create an instance of the PdfFileEditor class.
  • Put the paths of the PDF files into a string array.
  • Use the PdfFileEditor.concatenate(String[] inputFiles, String outputFile) method to combine PDF files.

The following code sample shows how to combine multiple PDF files into one PDF using Java.

// Create PdfFileEditor object
PdfFileEditor fileEditor = new PdfFileEditor();
String[] files = new String[] { "file1.pdf", "file2.pdf", "pdf3.pdf" };
// Merge multiple PDF files
fileEditor.concatenate(files, "merged-pdf.pdf");

Merge multiple PDF files using Java

In the previous example, we simply merged two PDF files into one PDF. However, sometimes it may be necessary to merge more than two PDF files. In this case you can pass an array of PDF file paths to the concatenation method. Below are the steps to do this.

  • Create an instance of the PdfFileEditor class.
  • Put the paths of the PDF files into a string array.
  • Use the PdfFileEditor.concatenate(String[] inputFiles, String outputFile) method to combine PDF files.

The following code sample shows how to combine multiple PDF files into one PDF using Java.

// Create PdfFileEditor object
PdfFileEditor fileEditor = new PdfFileEditor();
String[] files = new String[] { "file1.pdf", "file2.pdf", "pdf3.pdf" };
// Merge multiple PDF files
fileEditor.concatenate(files, "merged-pdf.pdf");

Merge PDF files using InputStream in Java

If you process PDF files as InputStream, you can pass InputStream object directly and get merged PDF as OutputStream object. Following are the steps to merge PDF files loaded into InputStream object.

  • Create an instance of the PdfFileEditor class.
  • Load the PDF file into an InputStream object.
  • Merge PDFs using the PdfFileEditor.concatenate(InputStream firstInputStream, InputStream secInputStream, OutputStream outputStream) method.

The following code sample demonstrates how to merge PDF files using InputStream object in Java.

// Create PdfFileEditor object
PdfFileEditor fileEditor = new PdfFileEditor();
// First PDF file
InputStream stream1 = new FileInputStream("file1.pdf");
// Second PDF file
InputStream stream2 = new FileInputStream("file2.pdf");
// OutputStream for merged PDF
OutputStream outstream = new FileOutputStream("merged.pdf");
// Merge PDF files
fileEditor.concatenate(stream1, stream2, outstream);

 

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/131780779