Skip to content

Commit 1d7e1b4

Browse files
Fix shifted bounding boxes calculation (#80)
* Fix shifted bounding boxes calculation * Avoid mutating bounding boxes while drawing annotations
1 parent f363e9a commit 1d7e1b4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

java/opendataloader-pdf-core/src/main/java/org/opendataloader/pdf/pdf/PDFWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

java/opendataloader-pdf-core/src/main/java/org/opendataloader/pdf/processors/ContentFilterProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private static void filterOutOfPageContents(int pageNumber, List<IObject> conten
7272
if (pageBoundingBox == null) {
7373
return;
7474
}
75+
pageBoundingBox.move(-pageBoundingBox.getLeftX(), -pageBoundingBox.getBottomY());
7576
for (int index = 0; index < contents.size(); index++) {
7677
IObject object = contents.get(index);
7778
if (object != null && pageBoundingBox.notOverlaps(object.getBoundingBox())) {

0 commit comments

Comments
 (0)