I am trying to make text selectable at PDF reading application made on JavaFX. I have PDF files that contain screenshots with text and OCR layer. So I need the text to be selectable like at regular viewer. I set up getting image from page and now trying to figure out how to highlight text.
I tried following:
InputStream is = this.getClass().getResourceAsStream(currentPdf);
Image convertedImage;
try {
PDDocument document = PDDocument.load(is);
List<PDPage> list = document.getDocumentCatalog().getAllPages();
PDPage page = list.get(pageNum);
List annotations = page.getAnnotations();
PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
markup.setRectangle(new PDRectangle(600, 600));
markup.setQuadPoints(new float[]{100, 100, 200, 100, 100, 500, 200, 500});
annotations.add(markup);
page.setAnnotations(annotations);
BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 128);
convertedImage = SwingFXUtils.toFXImage(image, null);
document.close();
imageView.setImage(convertedImage);
} catch (Exception e) {
throw new RuntimeException(e);
}
but that results in image without any highlights.
I also tried to find information at stack overflow or other resources, but haven't found anything.
Would appreciate some Java code sample which enables text highlighting with mouse.
I used ICEpdf and did the following:
question.getSelectedBounds()
.stream()
.map(Shape::getBounds)
.forEach(bounds -> {
SquareAnnotation squareAnnotation = (SquareAnnotation)
AnnotationFactory.buildAnnotation(
pdfController.getPageTree().getLibrary(),
Annotation.SUBTYPE_SQUARE,
bounds);
squareAnnotation.setFillColor(true);
squareAnnotation.setFillColor(new Color(255, 250, 57, 120));
squareAnnotation.setRectangle(bounds);
squareAnnotation.setBBox(bounds);
squareAnnotation.resetAppearanceStream(null);
AbstractAnnotationComponent annotationComponent = AnnotationComponentFactory
.buildAnnotationComponent(squareAnnotation, pdfController.getDocumentViewController(),
pageViewComponent, pdfController.getDocumentViewController().getDocumentViewModel());
pageViewComponent.addAnnotation(annotationComponent);
});