-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Annotations Created by Foxit (#2878)
* add some passing tests * Add Squiggly as annotation type * Fix inconsistent number types of PDAnnotations * Refactor the marked text extraction * ChangeLog * Refactoring * Correctly support StrikeOut Annotations * Improve naming and understandability of Bounding boxes * Remove link from supported FileAnnotationTypes * Explicitly ignore widget and link * fix potential NPE * incorporate feedback
- Loading branch information
1 parent
3257f7b
commit f5386b2
Showing
9 changed files
with
188 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.jabref.logic.pdf; | ||
|
||
import java.awt.geom.Rectangle2D; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import org.apache.pdfbox.cos.COSArray; | ||
import org.apache.pdfbox.cos.COSFloat; | ||
import org.apache.pdfbox.cos.COSInteger; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.common.PDRectangle; | ||
import org.apache.pdfbox.util.PDFTextStripperByArea; | ||
|
||
/** | ||
* Extracts the text of marked annotations using bounding boxes. | ||
*/ | ||
public final class TextExtractor { | ||
|
||
private final COSArray boundingBoxes; | ||
private final PDPage page; | ||
|
||
/** | ||
* @param page the page the annotation is on, must not be null | ||
* @param boundingBoxes the raw annotation, must not be null | ||
*/ | ||
public TextExtractor(PDPage page, COSArray boundingBoxes) { | ||
this.page = Objects.requireNonNull(page); | ||
this.boundingBoxes = Objects.requireNonNull(boundingBoxes); | ||
} | ||
|
||
/** | ||
* Extracts the text of a marked annotation such as highlights, underlines, strikeouts etc. | ||
* | ||
* @return The text of the annotation | ||
* @throws IOException If the PDFTextStripperByArea fails to initialize. | ||
*/ | ||
public String extractMarkedText() throws IOException { | ||
// Text has to be extracted by the rectangle calculated from the marking | ||
PDFTextStripperByArea stripperByArea = new PDFTextStripperByArea(); | ||
String markedText = ""; | ||
|
||
// Iterates over the array of segments. Each segment consists of 8 points forming a bounding box. | ||
int totalSegments = boundingBoxes.size() / 8; | ||
for (int currentSegment = 1, segmentPointer = 0; currentSegment <= totalSegments; currentSegment++, segmentPointer += 8) { | ||
try { | ||
stripperByArea.addRegion("markedRegion", calculateSegmentBoundingBox(boundingBoxes, segmentPointer)); | ||
stripperByArea.extractRegions(page); | ||
|
||
markedText = markedText.concat(stripperByArea.getTextForRegion("markedRegion")); | ||
} catch (IllegalArgumentException e) { | ||
throw new IOException("Cannot read annotation coordinates!", e); | ||
} | ||
} | ||
|
||
return markedText.trim(); | ||
} | ||
|
||
private Rectangle2D calculateSegmentBoundingBox(COSArray quadsArray, int segmentPointer) { | ||
// Extract coordinate values | ||
float upperLeftX = toFloat(quadsArray.get(segmentPointer)); | ||
float upperLeftY = toFloat(quadsArray.get(segmentPointer + 1)); | ||
float upperRightX = toFloat(quadsArray.get(segmentPointer + 2)); | ||
float upperRightY = toFloat(quadsArray.get(segmentPointer + 3)); | ||
float lowerLeftX = toFloat(quadsArray.get(segmentPointer + 4)); | ||
float lowerLeftY = toFloat(quadsArray.get(segmentPointer + 5)); | ||
|
||
// Post-processing of the raw coordinates. | ||
PDRectangle pageSize = page.getMediaBox(); | ||
float ulx = upperLeftX - 1; // It is magic. | ||
float uly = pageSize.getHeight() - upperLeftY; | ||
float width = upperRightX - lowerLeftX; | ||
float height = upperRightY - lowerLeftY; | ||
|
||
return new Rectangle2D.Float(ulx, uly, width, height); | ||
} | ||
|
||
private float toFloat(Object cosNumber) { | ||
if (cosNumber instanceof COSFloat) { | ||
return ((COSFloat) cosNumber).floatValue(); | ||
} | ||
if (cosNumber instanceof COSInteger) { | ||
return ((COSInteger) cosNumber).floatValue(); | ||
} | ||
throw new IllegalArgumentException("The number type of the annotation is not supported!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.