I don't see any problem, but I never created a form object from scratch.
The PDRectangle(1,1) looks unusual but might still work, I remember that
there is some math when forms are rendered.
Your image isn't displayed, it's probably a non-inline attachment. I
don't have Safari.
A solution would be to create and modify your annotation with Adobe
until it works with Safari, and then use PDFDebugger to compare with
your own.
Tilman
On 13.06.2023 17:21, Frédéric Ravetier wrote:
Hello,
I am using PDFBox to add an image as an annotation into a PDF.
When I open the PDF into Safari (macOS) the width of the image is
reduced (the image does not look like it should be)
LoremIpsum_Test_simple_2_pages-testA_pdf.png
This is the PDF :
https://drive.google.com/file/d/1lP6v90nEAQWuY84t1S0L8BWZkbiZ0mUw/view?usp=sharing
This is the original image :
https://drive.google.com/file/d/1X-dtqisuXLOkwBq1MvZueC69wdxhO8QH/view?usp=sharing
I apply a reducing zoom on the image.
Do you have any idea where my mistake is ? (annotation name :
73d51ba49022499ca1315b530f194276).
If I open the PDF into chrome,... acrobat, everything is fine (which
does not means my code is fine :D, may be these viewer are catching my
mistake)
I simplified my method but this is the code (because I manage other
use cases...):
private static void addAnnotationStamp(final PDDocument doc,
AnnotationObject annotationObject) throws IOException { PDPage page;
int nbPages = doc.getNumberOfPages(); int pageNumber = 0; if
(annotationObject.getPage() != null) { pageNumber =
annotationObject.getPage(); nbPages = 1; } while (nbPages > 0) { page
= doc.getPage(pageNumber); float pw =
page.getMediaBox().getUpperRightX(); float ph =
page.getMediaBox().getUpperRightY(); logger.debug("Page(" + pageNumber
+ ") - size : " + pw + " x " + ph); List<PDAnnotation> annotations =
page.getAnnotations(); PDAnnotationRubberStamp stamp = new
PDAnnotationRubberStamp(); //Name is important and should be unique if
(annotationObject.getPage() != null)
stamp.setAnnotationName(annotationObject.getName()); else
stamp.setAnnotationName(annotationObject.getName() + "-" +
pageNumber); logger.debug("Annotation : " +
stamp.getAnnotationName()); stamp.setPrinted(true); // always visible
stamp.setReadOnly(true); // does not interact with user
stamp.setLocked(true); stamp.setLockedContents(true); //Stackoverflow
https://stackoverflow.com/questions/74836898/pdfbox2-acrobat-consider-my-annotation-added-between-two-signature-as-modified
stamp.setPage(page); float objectWidth; float objectHeight; int
heightTranslate = 1; PDImageXObject image = null; image =
PDImageXObject.createFromFile(annotationObject.getImgSrc(), doc);
objectWidth = image.getWidth() * annotationObject.getImgZoom() / 100;
objectHeight = image.getHeight() * annotationObject.getImgZoom() /
100; heightTranslate = 5; logger.debug("Image size: " + objectWidth +
" x " + objectHeight + " zoom is " + annotationObject.getImgZoom());
PDRectangle rect = new PDRectangle(annotationObject.getX(), ph -
annotationObject.getY() - heightTranslate - objectHeight, objectWidth,
objectHeight); stamp.setRectangle(rect); PDAppearanceStream
appearanceStream = new PDAppearanceStream(doc);
appearanceStream.setBBox(new PDRectangle(1, 1));
appearanceStream.setResources(new PDResources()); try
(PDPageContentStream contentStream = new PDPageContentStream(doc,
appearanceStream)) { contentStream.drawImage(image, new Matrix()); }
PDAppearanceDictionary appearance = new PDAppearanceDictionary();
appearance.setNormalAppearance(appearanceStream);
stamp.setAppearance(appearance);
stamp.setIntent(PDAnnotationRubberStamp.IT_FREE_TEXT);
annotations.add(stamp); page.setAnnotations(annotations); // these
must be set for incremental save to work properly (PDFBOX < 3.0.0 at
least?) //ap.getCOSObject().setNeedToBeUpdated(true);
stamp.getCOSObject().setNeedToBeUpdated(true);
page.getResources().getCOSObject().setNeedToBeUpdated(true);
page.getCOSObject().setNeedToBeUpdated(true);
doc.getDocumentCatalog().getPages().getCOSObject().setNeedToBeUpdated(true);
doc.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
nbPages--; pageNumber++; } }
Best regards,
Fred