'What is the proper way to add rich text annotation in PDFBox?

I add three annotations into empty PDF:

  1. call .setContents("...")
  2. call .setRichContents("...");
  3. call .setContents("..."); and .setRichContents("...");

First annotation displays properly in Adobe Reader and in Preview (on Mac).

Second annotation displays properly in Adobe Reader only (as formatted text), and empty box in Preview.

Third annotation displays as plain text Simple text content for Test Case 3 from the method .setContent in Adobe Reader ( NOT rich text from the method .setRichContents and PDF tag /RC!) and in Preview.

Text for comments in PDF contains rich formatted elements, I need to show them in annotations. I assume that Preview doesn't support rich text in annotations.

I've tried to re-save PDF in Adobe Reader and then open in Preview - after that I see all comments in Adobe Reader (with rich text) and in Preview (as not formatted text).

Question: how to show rich formatted annotation in Adobe Reader and plain text in Preview?

My code:

PDDocument document = new PDDocument();

PDPage blankPage = new PDPage();
document.addPage(blankPage);

List<PDAnnotation> annotations = document.getPage(0).getAnnotations();

float pageHeight = document.getPage(0).getCropBox().getHeight();

PDAnnotationText text_TC1 = new PDAnnotationText();
PDRectangle positionTC1 = new PDRectangle();
positionTC1.setLowerLeftX(10);
positionTC1.setLowerLeftY(pageHeight - 10);
positionTC1.setUpperRightX(20);
positionTC1.setUpperRightY(pageHeight - 20);
text_TC1.setContents("Simple text content for Test Case 1.");
text_TC1.setRectangle(positionTC1);
text_TC1.setOpen(true);
text_TC1.constructAppearances();
annotations.add(text_TC1);

PDAnnotationText text_TC2 = new PDAnnotationText();
PDRectangle positionTC2 = new PDRectangle();
positionTC2.setLowerLeftX(10);
positionTC2.setLowerLeftY(pageHeight - 110);
positionTC2.setUpperRightX(20);
positionTC2.setUpperRightY(pageHeight - 120);
text_TC2.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" + 
                        "<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 2.</p>" + 
                        "</body>");
text_TC2.setRectangle(positionTC2);
text_TC2.setOpen(true);
text_TC2.constructAppearances();
annotations.add(text_TC2);

PDAnnotationText text_TC3 = new PDAnnotationText();
PDRectangle positionTC3 = new PDRectangle();
positionTC3.setLowerLeftX(10);
positionTC3.setLowerLeftY(pageHeight - 210);
positionTC3.setUpperRightX(20);
positionTC3.setUpperRightY(pageHeight - 220);
text_TC3.setContents("Simple text content for Test Case 3.");
text_TC3.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" + 
                        "<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 3.</p>" + 
                        "</body>");
text_TC3.setRectangle(positionTC3);
text_TC3.setOpen(true);

text_TC3.constructAppearances();

annotations.add(text_TC3);

document.save("test_so.pdf");        
document.close();

Update 2022-05-08: I've found a workaround solution - add comments via import XFDF:

    PDDocument document = new PDDocument();

    PDPage blankPage = new PDPage();
    document.addPage(blankPage);
    FDFDocument fdfDoc = FDFDocument.loadXFDF("test.xfdf");
    
    List<FDFAnnotation> fdfAnnots = fdfDoc.getCatalog().getFDF().getAnnotations();
    
    List<PDAnnotation> pdfannots = new ArrayList<>();
    for (int i=0; i<fdfDoc.getCatalog().getFDF().getAnnotations().size(); i++) {
        FDFAnnotation fdfannot = fdfAnnots.get(i);
        PDAnnotation pdfannot = PDAnnotation.createAnnotation(fdfannot.getCOSObject());

        pdfannots.add(pdfannot);
    }
    document.getPage(0).setAnnotations(pdfannots);
    
    fdfDoc.close();
    document.save("test.pdf");
    document.close();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source