[ 
https://issues.apache.org/jira/browse/PDFBOX-2162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=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);
                        
                        List<PDAnnotation> 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();
                PDDocument pddoc = PDDocument.load(pdfPath); //Input PDF File 
Name  
                imageWriter.writeImage(pddoc, "png", "", 1, 3, "output_image");
        }

}


was (Author: julstudy):
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);
                        
                        List<PDAnnotation> 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();
                PDDocument pddoc = PDDocument.load(pdfPath); //Input PDF File 
Name  
                imageWriter.writeImage(pddoc, "png", "", 1, 3, "output_image");
        }

}

> annotation that highlights a text is not visible in image (converted from the 
> pdf)
> ----------------------------------------------------------------------------------
>
>                 Key: PDFBOX-2162
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-2162
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Rendering
>    Affects Versions: 1.8.6
>         Environment: Java 1.7 and Eclipse Kepler
>            Reporter: Julien Savoyet
>         Attachments: myfile1.pdf, myfile1_re_saved.pdf
>
>
> Hi, I'm trying to convert in images (png or jpeg) a PDF file in which I've 
> added an annotation within each page through the PDAnnotationTextMarkup 
> object.
> I've used PDFImageWriter to achieve this convertion and the images are 
> correctly generated excepted that the annotation has disappeared.
> It seems that there is a mistake because if for example I open my PDF file 
> with acrobat reader and then I re-save it under a new PDF file and then I 
> relaunch my image conversion script with this latter one, the images 
> generated will this time contain the annotations.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to