Search code examples
javapdfbox

adding sticky notes in an existing pdf using java itext or pdfbox


I am trying to add sticky notes to an existing pdf. please help with any suggestion with itext or pdfbox.

I have tried using pdfbox but couldn't find any solution. Please help...

Here is the sample pdf of what type of sticky notes I want: http://www.pdfill.com/example/pdf_commenting_new.pdf


Solution

  • I found the solution.

    According to the PDF specification, 'a text annotation represents a “sticky note” attached to a point in the PDF document.' Thus, neither the class PDAnnotationTextMarkup nor the subtype SUB_TYPE_POLYGON appears to match your requirements. Instead, you should use the PDAnnotationText class. As an aside, PDAnnotationTextMarkup is documented (JavaDocs) to be the abstract class that represents a text markup annotation. While it is not actually declared abstract, that characterization should make clear that it probably does not work without further ado.

    so I used the below code and it worked like magic for me

    PDRectangle position = new PDRectangle();
    position.setUpperRightX(textPosition.getX());
    position.setUpperRightY(ph - textPosition.getY());
    
    position.setLowerLeftX(textPosition.getX()-4);
    position.setLowerLeftY(ph - textPosition.getY());
    PDGamma colourBlue = new PDGamma();
    colourBlue.setB(1);
    
    PDAnnotationText text = new PDAnnotationText();
    text.setContents(commentNameWithComments.get(word));
    text.setRectangle(position);
    text.setOpen(true);
    text.setConstantOpacity(50f);
    
    assert annotations != null;
    annotations.add(text);
    page1.setAnnotations(annotations);
    replaceText(word);
    

    it might be useful for future devs :-)