How to properly merge documents using Java

with File Paths

The easiest and most reliable way to merge PDF files, using the “GroupDocs.Merger for Java” product, is to use the full paths to these files.

The following example demonstrates how to merge PDF files using file paths in Java code:

  • Create an instance of Merger class and pass the full PDF file path as a constructor parameter.
  • Add another PDF file to merge with join method.
  • Call Merger class save method and specify the filename for the merged PDF file as parameter.
// Load the source PDF file
Merger merger = new Merger("c:\sample1.pdf");

// Add another PDF file to merge
merger.join("c:\sample2.pdf");
// Merge PDF files and save result
merger.save("c:\merged.pdf");

with File Input Streams

There are situations, and quite often, when it is necessary to work with other classes inherited from java.io.InputStream, for example with FileInputStream or others. In this case, it is necessary to use additional parameters to specify the exact file type that is used in this case. Because the “GroupDocs.Merger for Java” product cannot always accurately determine the file type by its stream. Therefore, to avoid exceptions when merging PDF and other files, see the example below:

  • Create an instance of Merger class and pass instance of LoadOptions with PPTX file type as a constructor parameter.
  • Create an instance of JoinOptions class with PPTX file type as a constructor parameter.
  • Add another PPTX stream to merge with join method and pass instance of JoinOptions class as a method parameter.
  • Call Merger class save method and specify the filename for the merged PPTX file as parameter.

String filePath1 = "c:/sample1.pptx";
String filePath2 = "c:/sample2.pptx";

FileType fileType1 = FileType.fromExtension(filePath1.substring(filePath1.lastIndexOf('.')));
FileType fileType2 = FileType.fromExtension(filePath2.substring(filePath2.lastIndexOf('.')));

File initialFile1 = new File(filePath1);
InputStream fileStream1 = new FileInputStream(initialFile1);

File initialFile2 = new File(filePath2);
InputStream fileStream2 = new FileInputStream(initialFile2);

// Init Load options with defined FileType
LoadOptions loadOptions = new LoadOptions(fileType1);

// Load the source PPTX stream
Merger merger = new Merger(fileStream1, loadOptions);

// Define join options with PPTX file type
JoinOptions joinOptions = new JoinOptions(fileType2);
// Add another PPTX stream to merge
merger.join(fileStream2, joinOptions);
// Merge PPTX streams and save result
merger.save("c:\merged.pptx");

with File Input Streams for cross-merging

There may also be situations when it is necessary to combine different types of streams into PDF. In this case, you can use the example below:

  • Create an instance of Merger class and pass instance of LoadOptions with ini DOCX file type and out PDF one as a constructor parameters.
  • Create an instance of JoinOptions class with PPTX file type as a constructor parameter.
  • Add another PPTX stream to merge with join method and pass instance of JoinOptions class as a method parameter.
  • Call Merger class save method and specify the filename for the merged PDF file as parameter.

String filePath1 = "c:/sample1.docx";
String filePath2 = "c:/sample2.pptx";

File initialFile1 = new File(filePath1);
InputStream fileStream1 = new FileInputStream(initialFile1);

File initialFile2 = new File(filePath2);
InputStream fileStream2 = new FileInputStream(initialFile2);

// Init Load options with defined FileTypes
LoadOptions loadOptions = new LoadOptions(FileType.DOCX, FileType.PDF);

// Load the source DOCX stream as PDF
Merger merger = new Merger(fileStream1, loadOptions);

// Define join options with PPTX file type
JoinOptions joinOptions = new JoinOptions(FileType.PPTX);
// Add another PPTX stream to merge
merger.join(fileStream2, joinOptions);
// Merge document streams and save PDF result
merger.save("c:\merged.pdf");