Is it possible to merge multiple pdf files into a single pdf without page breaks? For example - Merging 1.pdf (containing 2 pages, but the content spread only on 1 and a half page) and 2.pdf (containing 3 pages). The merged document should be such that as soon as the content of the first pdf ends, the content of the second pdf should begin. It should not start from the next page.
The current code that I am using is -
File mergedDoc = new File("MergedResponse.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(mergedDoc));
Document document = new Document(pdfDoc);
Map<Integer, PdfDocument> filesToMerge = new TreeMap<Integer, PdfDocument>();
//String[] files - contains the location and name of files to be merged
for (int i = 0; i < files.length; i++) {
filesToMerge.put(i, new PdfDocument(new PdfReader(files[i])));
}
for (Map.Entry<Integer, PdfDocument> entry : filesToMerge.entrySet()) {
int n = entry.getValue().getNumberOfPages();
for (int i = 1; i <= n; i++) {
entry.getValue().copyPagesTo(i, i, pdfDoc);
}
}
I also tried merging using the below code -
File mergedDoc = new File("MergedResponse.pdf");
PdfDocument pdf = new PdfDocument(new PdfWriter(mergedDoc));
PdfMerger merger = new PdfMerger(pdf);
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(files[0]));
merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
//Add pages from the second pdf document
PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(files[1]));
merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
firstSourcePdf.close();
secondSourcePdf.close();
But both the codes generate the merge pdf where the second document starts from a new page and not just follows the end of the first document.
Any help is appreciated.