How to make an annotation where only the interior is transparent and border is opaque?

69 views
Skip to first unread message

Ryan

unread,
Aug 11, 2016, 2:27:24 PM8/11/16
to PDFTron PDFNet SDK
Question:

I want to modify the default appearance of a Circle annotation so that the interior is semi transparent, and the border is opaque. It doesn't seem possible with the current interface.

Answer:

Officially, the PDF format does not support this, and annotations have one opacity value for the whole thing. However, you can generate your own custom appearance.

The following code will apply the opacity to only the interior (fill) of the circle.

void AddCircle(PDFDoc doc, int start, int end)
{
   
Page first_page = doc.GetPage(1);


    pdftron
.PDF.Annots.Circle circle = pdftron.PDF.Annots.Circle.Create(doc.GetSDFDoc(), new Rect(start, start, end, end));


    circle
.SetInteriorColor(new ColorPt(1, 0, 0), 3);
    circle
.SetColor(new ColorPt(1, 0, 0), 3);
    circle
.SetOpacity(0.5);


   
SetAppearance(circle, doc);


    first_page
.AnnotPushBack(circle);
}


void SetAppearance(pdftron.PDF.Annots.Markup annotation, PDFDoc doc)
{
   
ElementBuilder elementBuilder = new ElementBuilder();
   
ElementWriter elementWriter = new ElementWriter();
   
Obj appearanceObject = null;


   
var rect = annotation.GetContentRect();
   
var point1 = new Point(rect.x1, rect.y1);
   
var point2 = new Point(rect.x2, rect.y2);


    appearanceObject
= annotation.GetAppearance();
   
if (appearanceObject != null)
   
{
        elementWriter
.Begin(appearanceObject, true);
   
}
   
else
   
{
        elementWriter
.Begin(doc);
   
}


   
Element element;


    elementBuilder
.PathBegin(); // start constructing the path


   
var wWidth = point2.x - point1.x;
   
var wHeight = point2.y - point1.y;


   
switch (annotation.GetType())
   
{


       
case Annot.Type.e_Circle:
           
var wCenterX = wWidth / 2;
           
var wCenterY = wHeight / 2;
           
var wRadius = (wWidth / 2.0) - (annotation.GetBorderStyle().width / 2.0);


            elementBuilder
.CreateEllipse(wCenterX, wCenterY, wRadius, wRadius);
           
break;


   
}
    element
= elementBuilder.PathEnd();
    element
.SetPathStroke(true);
    element
.SetPathFill(true);


   
GState gState = element.GetGState();
    gState
= element.GetGState();


    gState
.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
    gState
.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());


    gState
.SetStrokeColor(annotation.GetColorAsRGB());
    gState
.SetFillColor(annotation.GetColorAsRGB());
    gState
.SetFillOpacity(annotation.GetOpacity());
    gState
.SetLineWidth(annotation.GetBorderStyle().width);


    elementWriter
.WritePlacedElement(element);
    appearanceObject
= elementWriter.End();


   
var r = annotation.GetRect();
   
Rect rect1 = new Rect();
    rect1
.x1 = 0.0;
    rect1
.y1 = 0.0;
    rect1
.x2 = r.Width();
    rect1
.y2 = r.Height();


    appearanceObject
.PutRect("BBox", rect1.x1, rect1.y1, rect1.x2, rect1.y2);
    appearanceObject
.PutName("Subtype", "Form");
    appearanceObject
.PutName("Type", "XObject");
    annotation
.SetAppearance(appearanceObject);
}

Reply all
Reply to author
Forward
0 new messages