Call Merger class save method and specify the filename for the merged PDF file as parameter.
// Load the source PDF file
Mergermerger=newMerger("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.
StringfilePath1="c:/sample1.pptx";StringfilePath2="c:/sample2.pptx";FileTypefileType1=FileType.fromExtension(filePath1.substring(filePath1.lastIndexOf('.')));FileTypefileType2=FileType.fromExtension(filePath2.substring(filePath2.lastIndexOf('.')));FileinitialFile1=newFile(filePath1);InputStreamfileStream1=newFileInputStream(initialFile1);FileinitialFile2=newFile(filePath2);InputStreamfileStream2=newFileInputStream(initialFile2);// Init Load options with defined FileType
LoadOptionsloadOptions=newLoadOptions(fileType1);// Load the source PPTX stream
Mergermerger=newMerger(fileStream1,loadOptions);// Define join options with PPTX file type
JoinOptionsjoinOptions=newJoinOptions(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.
StringfilePath1="c:/sample1.docx";StringfilePath2="c:/sample2.pptx";FileinitialFile1=newFile(filePath1);InputStreamfileStream1=newFileInputStream(initialFile1);FileinitialFile2=newFile(filePath2);InputStreamfileStream2=newFileInputStream(initialFile2);// Init Load options with defined FileTypes
LoadOptionsloadOptions=newLoadOptions(FileType.DOCX,FileType.PDF);// Load the source DOCX stream as PDF
Mergermerger=newMerger(fileStream1,loadOptions);// Define join options with PPTX file type
JoinOptionsjoinOptions=newJoinOptions(FileType.PPTX);// Add another PPTX stream to merge
merger.join(fileStream2,joinOptions);// Merge document streams and save PDF result
merger.save("c:\merged.pdf");
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.