How to change the color of a circle annotation?

How do I change the Fill Color annotation property?

25th October 2015
admin-marketing

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 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.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now