PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Square annotations

Open QuentinAndre11 opened this issue 3 months ago • 0 comments

Hi everyone, I'd like to create some rectangle annotations filled with a color and half opacity. Here is the code I tried:

XRect rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(bbox[0], bbox[1]), new XPoint(bbox[2], bbox[3])));
int[] c = colorMap[ann.color];

PdfTextAnnotation textAnnot = new()
{
    Title = "Title",
    Contents = "Message",
    Rectangle = new PdfSharpCore.Pdf.PdfRectangle(rect),
    Color = XColor.FromArgb(255, c[0], c[1], c[2]),
    Open = false,
    Icon = PdfTextAnnotationIcon.NoIcon,
};
textAnnot.Elements.SetName("/Subtype", "/Square");
textAnnot.Elements.Add("/IC", new PdfLiteral("[1 0 0]"));  // Interior Color normalized
textAnnot.Elements.Add("/CA", new PdfLiteral("0.5"));  // Constant Alpha
page.Annotations.Add(textAnnot);

The problem being that IC and CA are not in the elements keys so it is not kept when writing the annotations. I wanted to create a class that inherits from PdfTextAnnotation and add the two required keys, but the class is sealed. First of all: why? Then: even if I subclass PdfAnnotation, I still have troubles with KeyInfo, KeyType... which are internals, so I have to start from PdfDictionary and it's a bit frustrating. What would be an alternative to that ? Should I write a PdfRectAnnotation and submit a PR ?

Edit: My code actually works, the problem was elsewhere and it's possible to add the keys directly. Still a bit weird that the class is sealed, tho.

QuentinAndre11 avatar Oct 10 '25 15:10 QuentinAndre11