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);
});
Related
I am not getting how to add transparent text with the help of pdfBOX.
Here's something that shows alpha with 1.8 (you should use 2.*, that is a bit easier).
PDExtendedGraphicsState gs1 = new PDExtendedGraphicsState();
gs1.setNonStrokingAlphaConstant(1f);
PDExtendedGraphicsState gs2 = new PDExtendedGraphicsState();
gs2.setNonStrokingAlphaConstant(0.2f);
Map<String, PDExtendedGraphicsState> graphicsStatesMap = page.getResources().getGraphicsStates();
if (graphicsStatesMap == null)
{
graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
}
graphicsStatesMap.put("gs1", gs1);
graphicsStatesMap.put("gs2", gs2);
page.getResources().setGraphicsStates(graphicsStatesMap);
cs.setFont(PDType1Font.HELVETICA_BOLD, 60);
cs.setNonStrokingColor(255, 0, 0);
cs.appendRawCommands("/gs1 gs\n");
cs.beginText();
cs.moveTextPositionByAmount(50, 600);
cs.drawString("Apache PDFBox 1");
cs.endText();
cs.setNonStrokingColor(0, 0, 255);
cs.appendRawCommands("/gs2 gs\n");
cs.beginText();
cs.moveTextPositionByAmount(70, 620);
cs.drawString("Apache PDFBox 2");
cs.endText();
cs.close();
I am not getting any tutorial for adding a text watermark in a PDF file? Can you all please guide me, I am very new to PDFBOX.
Its not duplicate, the link in the comment didn't help me. I want to add text, not an image to the pdf.
Here is an example using PDFBox 2.0.2. This will load a PDF and write some text in the bottom right corner in a red transparent font. If it is a multiple page PDF the watermark will appear on every page. It might not be production ready, as I am not sure if there are some additional null conditions that need to be checked, but it should get you running in the right direction.
Keep in mind that this particular block of code will not modify the original PDF, but will create a new PDF using the Tmp_(filename) as the output.
private static void watermarkPDF (File fileStored) {
File tmpPDF;
PDDocument doc;
tmpPDF = new File(fileStored.getParent() + System.getProperty("file.separator") +"Tmp_"+fileStored.getName());
doc = PDDocument.load(fileStored);
for(PDPage page:doc.getPages()){
PDPageContentStream cs = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
String ts = "Some sample text";
PDFont font = PDType1Font.HELVETICA_BOLD;
float fontSize = 14.0f;
PDResources resources = page.getResources();
PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
r0.setNonStrokingAlphaConstant(0.5f);
cs.setGraphicsStateParameters(r0);
cs.setNonStrokingColor(255,0,0);//Red
cs.beginText();
cs.setFont(font, fontSize);
cs.setTextMatrix(Matrix.getTranslateInstance(0f,0f));
cs.showText(ts);
cs.endText();
}
cs.close();
}
doc.save(tmpPDF);
}
I'm trying to convert a TIFF to a PDF using Apache Imaging and PDFBox. Everything I've tried results in a blank (but non-zero-byte) pdf.
There are some examples in other SO questions like Add BufferedImage to PDFBox document and PDFBox draw black image from BufferedImage which I've tried.
I know the buffered image that I'm reading from the TIFF is valid because I can display it and see it in a JFrame.
I've also tried a PDFJpeg instead of a PDFPixelMap, and contentStream.drawImage() instead of .drawXObject(), the result is always the same.
I also tried creating the PDXObjectImage before the content stream and writing multiple images as recommended in Can't add an image to a pdf using PDFBox, the result is still the same. The output file is larger when I write the image multiple times, so it's doing "something" but I don't know what it is.
How can I write a BufferedImage to a page on a PDF using PDFBox?
try(PDDocument document = new PDDocument();ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
{
PDPage blankPage = new PDPage();
document.addPage( blankPage );
// PDXObjectImage pdImage = new PDJpeg(document, image);
PDXObjectImage pdImage = new PDPixelMap(document, image);
// contentStream.drawImage(pdImage, 5, 300);
PDPageContentStream contentStream = new PDPageContentStream(document, blankPage);
contentStream.drawXObject(pdImage, 5, 5, 100, 100);
document.save("test.pdf");
contentStream.close();
}
catch(COSVisitorException ex) {
throw new IOException(ex);
}
whats the best way to embedded a java GUI component such as JPanel to pdf using PDFBox?
Im currently using this code:
PDDocument doc = null;
PDPage page = null;
PDXObjectImage ximage = null;
try {
doc = new PDDocument();
page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
BufferedImage image = new BufferedImage((int)PDPage.PAGE_SIZE_A4.getWidth()*2,(int)PDPage.PAGE_SIZE_A4.getHeight()*2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
panel.setBounds(0, 0,(int)PDPage.PAGE_SIZE_A4.getWidth()*2,(int)PDPage.PAGE_SIZE_A4.getHeight()*2);
panel.doLayout();
panel.validate();
panel.paint(g);
g.dispose();
ximage = new PDPixelMap(doc, image);
content.drawXObject(ximage, 0 , 0 ,(int)PDPage.PAGE_SIZE_A4.getWidth(),(int)PDPage.PAGE_SIZE_A4.getHeight() );
content.close();
if (path==null || path.equals(""))
doc.print();
else
doc.save(path);
doc.close();
} catch(Exception ie) {
ie.printStackTrace();
}
but two problems that i got with it is 1. sometimes i am getting a blank pdf for some reason, and 2. when it works the PDF looks pixelated.
is there a better way?
PS: i am not allowed to use iText in my project
Because you add the (vector) panel graphics to the PDF page through a PDFPixelMap, the panel graphics will always be pixelated (converted to a raster).
If you want to prevent rasterization, you would have to use the vector drawing commands such as drawLine, stroke, drawText, etc. instead.
I am parsing some html code using HTMLWorker in Java and then inserting it to PDF using iText. I create document by calling new Document(PageSize.A4, 40, 40, 40, 40); which should specify margin 40px on all sides, but when I insert parsed html code that contains table wider than page, right margin dont work and table reaches right border of page... All margins are ok except the right one.. any suggestions?
relevant code:
Document document = new Document(PageSize.A4, 40, 40, 40, 40);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory10());
HTMLWorker worker = new HTMLWorker(document);
worker.setProviders(map);
worker.parse(new StringReader(VARIABLE_CONTAINING_HTML_CODE));
It should work.
Can you provide info about iText version and how the HTML code looks like.
Images can make the table expand beyond margins (it´s not pixels by the way).
Try this code and see if the problem still remains. The table should not expand beyond the page margins. Tables used with HTMLWorker always get a width of 100% of its container.
Document document = new Document(PageSize.A4, 40, 40, 40, 40);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/TEST.pdf"));
document.open();
HTMLWorker worker = new HTMLWorker(document);
String code = "<table border=1><tr><td>Test</td><td>Test</td></tr></table>";
worker.parse(new StringReader(code));
document.close();