How to change the color of a circle annotation?

Tags: annotationscircle annotationcolorannotation coloriText 7

I am adding a SquareCircle annotation to an already existing PDF using iTextSharp in C#. Now I want to change the Fill Color annotation property, but I don't know how. When opening the PDF in Acrobat, the fill color property is in the appearance tab of the annotation properties.

Appearance tab

Posted on StackOverflow on Mar 26, 2015 by Lupetto Burlone

Please take a look at the CircleAnnotation example. It creates a circle annotation with a blue border and red as the interior color:

Colored circle annotation
Colored circle annotation

The code to add this annotation in iText 7 looks like this:

protected void manipulatePdf(String src, String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(new FileInputStream(src)),
            new PdfWriter(new FileOutputStream(dest)));
    Rectangle rect = new Rectangle(150, 770, 50, 50);
 
    PdfAnnotation annotation = new PdfCircleAnnotation(rect)
            .setContents("Circle")
            .setTitle(new PdfString("Circle"))
            .setColor(Color.BLUE)
            .setFlags(PdfAnnotation.PRINT)
            .setBorderStyle(PdfAnnotation.STYLE_DASHED)
            .setDashPattern(new PdfArray(new int[]{3, 2}))
            .setBorder(new PdfArray(new float[]{0, 0, 2}))
            .put(PdfName.IC, new PdfArray(new int[]{1, 0, 0}));
    pdfDoc.getFirstPage().addAnnotation(annotation);
    pdfDoc.close();
}

This line sets the interior color:

annotation.put(PdfName.IC, new PdfArray(new int[]{1, 0, 0}));

Caveat:

some PDF viewers (such as Chrome PDF viewer) are not full PDF viewers. They don't support every type of annotation. For instance, if you open hello_circle.pdf in Chrome, you won't see the annotation. That is not a problem caused by the PDF (nor iTextSharp), that is a viewer problem.

Click this link if you want to see how to answer this question in iText 5.