[jira] [Comment Edited] (PDFBOX-2162) annotation that highlights a text is not visible in image (converted from the pdf)
[ https://issues.apache.org/jira/browse/PDFBOX-2162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14048881#comment-14048881 ] Julien Savoyet edited comment on PDFBOX-2162 at 7/1/14 3:23 PM: Hi Maruan, thanks a lot for your comment, this help me really to understand the way of working about the annotations in PdbBox. So I've tried to add an appearance COSName.AP to the dictionnary, but I remain in the same impasse without success at the moment because the annotations are still not taken into account in the png images generated in the end of the script. Hereafter is the initial code refined (without the appearance part since it doesn't work). If you have an idea how I could add the appearance part in order to make the annotation appear in the output images, I'd be happy to receive your suggestions : package fr.annotation.images; import java.io.IOException; import java.util.List; import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.color.PDGamma; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup; import org.apache.pdfbox.util.PDFImageWriter; public class createAnnotation { public static void main(String[] args) throws COSVisitorException, IOException { PDDocument doc = null; try { doc = PDDocument.load(myfile1.pdf); //Input PDF File Name List? pages = doc.getDocumentCatalog().getAllPages(); for (int i = 0; i pages.size(); i++) { PDPage page = (PDPage) pages.get(i); ListPDAnnotation annotations = page.getAnnotations(); PDGamma colourBlue = new PDGamma(); colourBlue.setB(1); float pw = page.getMediaBox().getUpperRightX(); float ph = page.getMediaBox().getUpperRightY(); // Now add the markup annotation, a highlight to PDFBox text PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColour(colourBlue); txtMark.setConstantOpacity((float) 0.5); // Make the highlight 50% transparent //PART I - Set the rectangle containing the markup PDRectangle position = new PDRectangle(); float lowerLeftX = 94; float upperRightX = 94+100; float lowerLeftY = ph - 89; float upperRightY = ph - 89 + 20; System.out.println(lowerLeftX = + lowerLeftX + upperRightX = + upperRightX + lowerLeftY = + lowerLeftY + upperRightY = + upperRightY); position.setLowerLeftX(lowerLeftX); position.setLowerLeftY(lowerLeftY); position.setUpperRightX(upperRightX); position.setUpperRightY(upperRightY); txtMark.setRectangle(position); //PART II - Set the quad float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY() - 2; // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.getLowerLeftY() - 2; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); } doc.save(tmpfile.pdf); //Output file name } finally { if (doc != null) { doc.close(); } } String pdfPath = tmpfile.pdf; PDFImageWriter imageWriter = new PDFImageWriter();
[jira] [Comment Edited] (PDFBOX-2162) annotation that highlights a text is not visible in image (converted from the pdf)
[ https://issues.apache.org/jira/browse/PDFBOX-2162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelfocusedCommentId=14048881#comment-14048881 ] Julien Savoyet edited comment on PDFBOX-2162 at 7/1/14 3:24 PM: Hi Maruan, thanks a lot for your comment, this help me really to understand the way of working about the annotations in PdbBox. So I've tried to add an appearance COSName.AP to the dictionnary, but I remain in the same impasse without success at the moment because the annotations are still not taken into account in the png images generated in the end of the script. Hereafter is the initial code refined (without the appearance part since it doesn't work). If you have an idea how I could add the appearance part in order to make the annotation appear in the output images, I'd be happy to receive your suggestions : package fr.annotation.images; import java.io.IOException; import java.util.List; import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.color.PDGamma; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup; import org.apache.pdfbox.util.PDFImageWriter; public class createAnnotation { public static void main(String[] args) throws COSVisitorException, IOException { PDDocument doc = null; try { doc = PDDocument.load(myfile1.pdf); //Input PDF File Name List? pages = doc.getDocumentCatalog().getAllPages(); for (int i = 0; i pages.size(); i++) { PDPage page = (PDPage) pages.get(i); ListPDAnnotation annotations = page.getAnnotations(); PDGamma colourBlue = new PDGamma(); colourBlue.setB(1); float pw = page.getMediaBox().getUpperRightX(); float ph = page.getMediaBox().getUpperRightY(); // Now add the markup annotation, a highlight to PDFBox text PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColour(colourBlue); txtMark.setConstantOpacity((float) 0.5); // Make the highlight 50% transparent //PART I - Set the rectangle containing the markup PDRectangle position = new PDRectangle(); float lowerLeftX = 94; float upperRightX = 94+100; float lowerLeftY = ph - 89; float upperRightY = ph - 89 + 20; System.out.println(lowerLeftX = + lowerLeftX + upperRightX = + upperRightX + lowerLeftY = + lowerLeftY + upperRightY = + upperRightY); position.setLowerLeftX(lowerLeftX); position.setLowerLeftY(lowerLeftY); position.setUpperRightX(upperRightX); position.setUpperRightY(upperRightY); txtMark.setRectangle(position); //PART II - Set the quad float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY() - 2; // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.getLowerLeftY() - 2; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); } doc.save(tmpfile.pdf); //Output file name } finally { if (doc != null) { doc.close(); } } String pdfPath = tmpfile.pdf; PDFImageWriter imageWriter = new PDFImageWriter();