Color of a circle annotation

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

CircleAnnotation.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/29275194/change-pdf-fillcolor-annotation-property-using-itextsharp-c-sharp
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.*;
  17. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  18. import com.itextpdf.kernel.pdf.annot.PdfCircleAnnotation;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25. @Category(SampleTest.class)
  26. public class CircleAnnotation extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/annotations/circle_annotation.pdf";
  28.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new CircleAnnotation().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  39.         Rectangle rect = new Rectangle(150, 770, 50, 50);
  40.  
  41.         PdfAnnotation annotation = new PdfCircleAnnotation(rect)
  42.                 .setContents("Circle")
  43.                 .setTitle(new PdfString("Circle"))
  44.                 .setColor(Color.BLUE)
  45.                 .setFlags(PdfAnnotation.PRINT)
  46.                 .setBorderStyle(PdfAnnotation.STYLE_DASHED)
  47.                 .setDashPattern(new PdfArray(new int[]{3, 2}))
  48.                 .setBorder(new PdfArray(new float[]{0, 0, 2}))
  49.                 .put(PdfName.IC, new PdfArray(new int[]{1, 0, 0}));
  50.         pdfDoc.getFirstPage().addAnnotation(annotation);
  51.         pdfDoc.close();
  52.     }
  53. }
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