Color of a circle annotation

25th October 2015
admin-marketing

Switch code for this example

CircleAnnotation.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/29275194/change-pdf-fillcolor-annotation-property-using-itextsharp-c-sharp
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfAnnotation;
  11. import com.itextpdf.text.pdf.PdfArray;
  12. import com.itextpdf.text.pdf.PdfBorderArray;
  13. import com.itextpdf.text.pdf.PdfDashPattern;
  14. import com.itextpdf.text.pdf.PdfName;
  15. import com.itextpdf.text.pdf.PdfReader;
  16. import com.itextpdf.text.pdf.PdfStamper;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class CircleAnnotation {
  25.  
  26.     public static final String SRC = "resources/pdfs/hello.pdf";
  27.     public static final String DEST = "results/annotations/hello_circle.pdf";
  28.  
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new CircleAnnotation().manipulatePdf(SRC, DEST);
  33.     }
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         Rectangle rect = new Rectangle(150, 770, 200, 820);
  38.         PdfAnnotation annotation = PdfAnnotation.createSquareCircle(
  39.             stamper.getWriter(), rect, "Circle", false);
  40.         annotation.setTitle("Circle");
  41.         annotation.setColor(BaseColor.BLUE);
  42.         annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
  43.         annotation.setBorder(new PdfBorderArray(0, 0, 2, new PdfDashPattern()));
  44.         annotation.put(PdfName.IC, new PdfArray(new int[]{1, 0, 0}));
  45.         stamper.addAnnotation(annotation, 1);
  46.         stamper.close();
  47.     }
  48. }
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