iText 5

How to change the color of a circle annotation? | iText 5 PDF Development Guide

How do I change the Fill Color annotation property?


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.

https://itextpdf.com/sites/default/files/0balS.jpg

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:

https://itextpdf.com/sites/default/files/iHjBD.png

Colored circle annotation

The code to add this annotation looks like this:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); Rectangle rect = new Rectangle(150, 770, 200, 820); PdfAnnotation annotation = PdfAnnotation.createSquareCircle( stamper.getWriter(), rect, "Circle", false); annotation.setTitle("Circle"); annotation.setColor(BaseColor.BLUE); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setBorder(new PdfBorderArray(0, 0, 2, new PdfDashPattern())); annotation.put(PdfName.IC, new PdfArray(new int[]{1, 0, 0})); stamper.addAnnotation(annotation, 1); stamper.close(); }

I based this example on the TimetableAnnotations3 example. The only thing I added was the line that 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.