The lower left isn't at (0,0), that is the reason. Another problem,
although not here, is that it's the cropbox that really counts.
Tilman
On 21.08.2024 16:55, Aniket Kulkarni wrote:
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.
<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
<https://www.linkedin.com/company/assai-software-services-b.v./><https://www.youtube.com/channel/UCipQXkL4bbqKYf5uhyR4OGA>
---------------------------------------------------------------------
To unsubscribe, e-mail:[email protected]
For additional commands, e-mail:[email protected]