Search This Blog

Breaking

Tuesday, 8 February 2022

[Java] [Apache PDFBox] Create a single page with three different pdf pages

If we want to create a pdf by putting 3 pages side by side, we can use the below approach.

Here we are trying to merge first 2 pages side by side and then, we are merging the third page with this newly created page.

This code is very useful if you are performing comparison between 2 pdf's and want to show the result of that comparison also.



import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;

import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class pdfSideBySide {

public static void mergeFileSideBySide(String file1Path, String file2path, String mergedfilepath,
String mergedfilename, String layername1, String layername2) {
File pdf1File = new File(file1Path);
File pdf2File = new File(file2path);
File outPdfFile = new File(mergedfilepath);
PDDocument pdf1 = null;
PDDocument pdf2 = null;
PDDocument outPdf = null;
try {
pdf1 = PDDocument.load(pdf1File);
pdf2 = PDDocument.load(pdf2File);
outPdf = new PDDocument();

// Create output PDF frame
PDRectangle pdf1Frame = pdf1.getPage(0).getCropBox();
PDRectangle pdf2Frame = pdf2.getPage(0).getCropBox();
PDRectangle outPdfFrame = new PDRectangle(pdf1Frame.getWidth()+pdf2Frame.getWidth(), Math.max(pdf1Frame.getHeight(), pdf2Frame.getHeight()));

// Create output page with calculated frame and add it to the document
COSDictionary dict = new COSDictionary();
dict.setItem(COSName.TYPE, COSName.PAGE);
dict.setItem(COSName.MEDIA_BOX, outPdfFrame);
dict.setItem(COSName.CROP_BOX, outPdfFrame);
dict.setItem(COSName.ART_BOX, outPdfFrame);
PDPage outPdfPage = new PDPage(dict);
outPdf.addPage(outPdfPage);

// Source PDF pages has to be imported as form XObjects to be able to insert them at a specific point in the output page
LayerUtility layerUtility = new LayerUtility(outPdf);
PDFormXObject formPdf1 = layerUtility.importPageAsForm(pdf1, 0);
PDFormXObject formPdf2 = layerUtility.importPageAsForm(pdf2, 0);

// Add form objects to output page
AffineTransform afLeft = new AffineTransform();
layerUtility.appendFormAsLayer(outPdfPage, formPdf1, afLeft, layername1);
AffineTransform afRight = AffineTransform.getTranslateInstance(pdf1Frame.getWidth(), 0.0);
layerUtility.appendFormAsLayer(outPdfPage, formPdf2, afRight, layername2);
outPdf.save( outPdfFile + "//" + mergedfilename);

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pdf1 != null) pdf1.close();
if (pdf2 != null) pdf2.close();
if (outPdf != null) outPdf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void moveFile(String sourcefilepath, String targetfilepath) throws IOException {
Path temp = Files.move
(Paths.get(sourcefilepath),
Paths.get(targetfilepath));

if(temp != null)
{
System.out.println("File renamed and moved successfully");
}
else
{
System.out.println("Failed to move the file");
}
}

private static void mergePDF(String filepath) throws IOException { ;
File folder = new File(filepath);
File[] listOfFiles = folder.listFiles();
PDFMergerUtility pdfMerger = new PDFMergerUtility();
pdfMerger.setDestinationFileName(filepath+ "Results.pdf");
for(File file: listOfFiles) {
pdfMerger.addSource(file);
}
pdfMerger.mergeDocuments(null);
System.out.println("PDF Documents merged to a single file");
}

public static void main(String[] args) throws Exception {
String file1path = "src//main//resources//RPT_CPSPM_18875164_7469.pdf";
String file2path = "src//main//resources//sample-pdf-file.pdf";
String layername1 = "left";
String layername2 = "right";
String mergedfilepath = "src//main//resources//";
String mergedfilename = "abc.pdf";
String totalMergedfilepath = mergedfilepath+mergedfilename;

mergeFileSideBySide(file1path,file2path,mergedfilepath,mergedfilename,layername1,layername2);

String finalReportPath = "src//main//resources//Report//";
String finalreportname = "finalymegred.pdf";
String layername3 = "top";
String layername4 = "bottom";
Thread.sleep(1000*2);
mergeFileSideBySide(totalMergedfilepath, "sample-pdf-file.pdf",finalReportPath,
finalreportname,layername3,layername4);

mergePDF(finalReportPath);

}
}

No comments:

Post a Comment