Dear Team,
I need your help regarding how to calculate actual size of PDF page of specific
pdf files.
We have built a PDF stamping feature. But this feature is getting affected
negatively because of wrong page size calculated by page PDFBox library (Or pdf
document itself may have some issues).
I am using PDFBox library version: 2.0.26
We perform following actions:
*
Calculate page height and width.
*
Based on page size, we create a sample pdf page of that size and print the html
stamp on it. Location of the stamp is based on configured co-ordinates.
*
Overlay this newly generated pdf page with stamp based on original page size
and same the file.
Please find attached files which are created during these processes.
Following is the code base:
try (PDDocument pdDocument = PDDocument.load(downloadedPdfFile,
MemoryUsageSetting.setupTempFileOnly());
Overlay overlay = new Overlay();) {
// Get the pages and create a map with page number and the image to
// be water marked.
Map<Integer, String> overlayProps = new HashMap<Integer, String>();
if (pageNo == -1) {
for (int i = 0; i < pdDocument.getNumberOfPages(); i++) {
burnStampOnPage(position, htmlStamp, sizeByStampFile, pdDocument,
overlayProps, i);
PDPage page = pdDocument.getPage(i);
PDRectangle pageSize = page.getMediaBox();
int rotation = page.getRotation();
boolean rotate = rotation == 90 || rotation == 270;
float pageWidth = rotate ? pageSize.getUpperRightY() :
pageSize.getUpperRightX();
float pageHeight = rotate ? pageSize.getUpperRightX() :
pageSize.getUpperRightY();
//Convert the width and height to inches
pageWidth = pageWidth/72; // 14.114173
pageHeight = pageHeight/72; // 9.980314
stampFilePath = generateStamp(htmlStamp, pageWidth, pageHeight,
rotation, position); // Code to generate sample pdf page with stamp printed on
it at desired position
overlayProps.put(pageNo + 1, stampFilePath);
// Using the Overlay object add the map of properties to the PDF
overlay.setInputPDF(pdDocument);
overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
overlay.overlay(overlayProps);
// Save the PDF to a new location
File stampedPdfFile = new
File(FileHandleUtils.getTempDir(tempStampFolderName),
downloadedPdfFile.getName());
pdDocument.save(stampedPdfFile);
}
}
Ideally, in the final file, stamp should appear at the right age as you can see
in "Generated_pdf_page_with_custom_stamp.pdf" but the final file shows its bit
shifted to left from the right age.
I think the calculated size (marked in yellow above) is wrong. If I manually
set the width from 14.114173 to 16.54 inches and height from 9.980314 to 11.69
inches, then it appears as expected.
Is it possible for you to investigate this?
Kindly let us know if you need more information.
Kind regards,
Aniket Kulkarni | Java Developer
Assai Software Services B.V.
[cid:ec879ade-db18-4ba2-b18b-1cbef5346ca4]<https://www.assai-software.com/https:/www.assai-software.com/>
Phone: +31 (0) 345 516 663
Web: www.assai-software.com<http://www.assai-software.com/> | E-mail:
[email protected]<mailto:[email protected]>
Address: Parallelweg Oost 13a, 4103 NC Culemborg, the Netherlands
[cid:01487fac-7c5c-4b39-909c-a6ab6582970e]<https://www.linkedin.com/company/assai-software-services-b.v./>
[cid:59b93a0d-d9e5-4354-9e85-06a8eea04112]
<https://www.youtube.com/channel/UCipQXkL4bbqKYf5uhyR4OGA>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]