@@ -51,11 +51,13 @@ public class PDFWriter {
5151
5252 private static final List <List <PDAnnotation >> annotations = new ArrayList <>();
5353 private static final Logger LOGGER = Logger .getLogger (PDFWriter .class .getCanonicalName ());
54+ private static final List <BoundingBox > pageBoundingBoxes = new ArrayList <>();
5455
5556 public static void updatePDF (File inputPDF , String password , String outputFolder , List <List <IObject >> contents ) throws IOException {
5657 try (PDDocument document = Loader .loadPDF (inputPDF , password )) {
5758 for (int pageNumber = 0 ; pageNumber < StaticContainers .getDocument ().getNumberOfPages (); pageNumber ++) {
5859 annotations .add (new ArrayList <>());
60+ pageBoundingBoxes .add (DocumentProcessor .getPageBoundingBox (pageNumber ));
5961 for (IObject content : contents .get (pageNumber )) {
6062 drawContent (content , PDFLayer .CONTENT );
6163 }
@@ -64,6 +66,7 @@ public static void updatePDF(File inputPDF, String password, String outputFolder
6466 document .getPage (pageNumber ).getAnnotations ().addAll (annotations .get (pageNumber ));
6567 }
6668 annotations .clear ();
69+ pageBoundingBoxes .clear ();
6770 createOptContentsForAnnotations (document );
6871 document .setAllSecurityToBeRemoved (true );
6972
@@ -136,9 +139,15 @@ private static void drawListItems(PDFList list, PDAnnotation annot) throws IOExc
136139
137140 public static PDAnnotation draw (BoundingBox boundingBox , float [] colorArray ,
138141 String contents , Long id , PDAnnotation linkedAnnot , String level , PDFLayer layerName ) {
142+
143+ BoundingBox movedBoundingBox = new BoundingBox (boundingBox );
144+ BoundingBox pageBoundingBox = pageBoundingBoxes .get (boundingBox .getPageNumber ());
145+ if (pageBoundingBox != null ) {
146+ movedBoundingBox .move (pageBoundingBox .getLeftX (), pageBoundingBox .getBottomY ());
147+ }
139148 PDAnnotationSquareCircle square = new PDAnnotationSquare ();
140- square .setRectangle (new PDRectangle (getFloat (boundingBox .getLeftX ()), getFloat (boundingBox .getBottomY ()),
141- getFloat (boundingBox .getWidth ()), getFloat (boundingBox .getHeight ())));
149+ square .setRectangle (new PDRectangle (getFloat (movedBoundingBox .getLeftX ()), getFloat (movedBoundingBox .getBottomY ()),
150+ getFloat (movedBoundingBox .getWidth ()), getFloat (movedBoundingBox .getHeight ())));
142151 square .setConstantOpacity (0.4f );
143152 PDColor color = new PDColor (colorArray , PDDeviceRGB .INSTANCE );
144153 square .setColor (color );
0 commit comments