Skip to main content
Skip table of contents

Color of a circle annotation

Example written in answer to the question Click How to change the color of a circle annotation?


circleannotation

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.annotations;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfCircleAnnotation;

import java.io.File;

public class CircleAnnotation {
    public static final String DEST = "./target/sandbox/annotations/circle_annotation.pdf";

    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new CircleAnnotation().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        Rectangle rect = new Rectangle(150, 770, 50, 50);
        PdfAnnotation annotation = new PdfCircleAnnotation(rect)
                .setBorderStyle(PdfAnnotation.STYLE_DASHED)
                .setDashPattern(new PdfArray(new int[]{3, 2}))

                // This method sets the text that will be displayed for the annotation or the alternate description,
                // if this type of annotation does not display text.
                .setContents("Circle")
                .setTitle(new PdfString("Circle"))
                .setColor(ColorConstants.BLUE)

                // Set to print the annotation when the page is printed
                .setFlags(PdfAnnotation.PRINT)
                .setBorder(new PdfArray(new float[]{0, 0, 2}))

                // Set the interior color
                .put(PdfName.IC, new PdfArray(new int[]{1, 0, 0}));
        pdfDoc.getFirstPage().addAnnotation(annotation);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;

namespace iText.Samples.Sandbox.Annotations
{
    public class CircleAnnotation
    {
        public static readonly String DEST = "results/sandbox/annotations/circle_annotation.pdf";

        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new CircleAnnotation().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            Rectangle rect = new Rectangle(150, 770, 50, 50);
            PdfAnnotation annotation = new PdfCircleAnnotation(rect)
                .SetBorderStyle(PdfAnnotation.STYLE_DASHED)
                .SetDashPattern(new PdfArray(new int[] {3, 2}))

                // This method sets the text that will be displayed for the annotation or the alternate description,
                // if this type of annotation does not display text.
                .SetContents("Circle")
                .SetTitle(new PdfString("Circle"))
                .SetColor(ColorConstants.BLUE)

                // Set to print the annotation when the page is printed
                .SetFlags(PdfAnnotation.PRINT)
                .SetBorder(new PdfArray(new float[] {0, 0, 2}))

                // Set the interior color
                .Put(PdfName.IC, new PdfArray(new int[] {1, 0, 0}));
            pdfDoc.GetFirstPage().AddAnnotation(annotation);

            pdfDoc.Close();
        }
    }
}

Results

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.