itextitext7pdf-annotations

iText7 PdfPolyGeomAnnotation not rendering in browser PDF viewers


I'm using iText7 in C# to create a Polyline Annotation in a PDF document. The annotation renders correctly in Adobe Acrobat but doesn't show in browser PDF viewers like Chrome. I did not encounter any issue when rendering other type of annotations such as Ink, Highlight or Circle, but only Polyline so far.

Here's the code snippet I am using:

List<float> vertices = new List<float>();

// Create Polyline Annotation
PdfPolyGeomAnnotation polyline = PdfPolyGeomAnnotation.CreatePolyLine(rect, points.ToArray());

polyline.SetColor(Colour.GetColorValue());

// Set Line Width
polyline.SetBorder(new PdfAnnotationBorder(0, 0, CommentData.strokewidth));
polyline.SetFlags(PdfAnnotation.PRINT | PdfAnnotation.LOCKED | PdfAnnotation.READ_ONLY);

// Set Opacity
if (CommentData.opacity > 0 && CommentData.opacity <= 1)
{
    polyline.SetOpacity(new PdfNumber(CommentData.opacity));
}

// Add Annotation to Page
page.AddAnnotation(polyline);

Sample inputs:

What am I missing to make the polyline annotation visible in browser PDF viewers? Thanks.


Solution

  • This is a generic all PDF libraries answer, since the same problem, can easily occur in other PDF annotation cases than iText.

    Let us use your basic given data, without knowing its context. You should see here it was defined with a red fill, but there is no red visible. Likewise the grey vectors may also not be shown, depending how they were defined. enter image description here

    Here is the decompressed source code.

    %PDF-1.7
    %€€€€
    1 0 obj <</Type/Catalog/Pages 2 0 R>> endobj
    2 0 obj <</Type/Pages/Count 1/Kids[3 0 R]>> endobj
    3 0 obj <</Type/Page/Parent 2 0 R/MediaBox[0 0 100 100]/Contents 4 0 R/Resources<<>>/Annots[5 0 R]>> endobj
    4 0 obj <</Length 0>>
    stream
    endstream
    endobj
    
    5 0 obj <</Type/Annot/P 3 0 R/Subtype/PolyLine/CreationDate(D:20250308)/M(D:20250308)/NM(01234567-0123-0123-0123456789abcdef)/Subj(Polygon Line)
    /F 4/Rect[21.12 3.94 63.37 23.68]/Vertices[23.12 5.94 56.87 21.68 61.37 13.43]/AP<</N 6 0 R>>/C[0 0 0]/CA .5/BS<</W 2>>/IC[1 0 0]>> endobj
    
    6 0 obj <</Type/XObject/Subtype/Form/BBox[21.12 3.94 63.37 23.68]/Matrix[1 0 0 1 0 0]/Resources<</ExtGState<</H<</Type/ExtGState/CA .5/ca .5>>>>>>/Length 72>>
    stream
    q /H gs 2 w 23.12 5.94 m 56.87 21.68 l 61.37 13.43 l h 0 0 0 RG S Q
    endstream
    endobj
    xref
    0 7
    0000000000 65536 f 
    0000000015 00000 n 
    0000000060 00000 n 
    0000000111 00000 n 
    0000000219 00000 n 
    0000000266 00000 n 
    0000000551 00000 n 
    trailer
    <</Size 7/Root 1 0 R/ID[<F4B68448FD7DE34283F6D059675615A0><F4B68448FD7DE34283F6D059675615A0>]>>
    startxref
    802
    %%EOF
    

    You can look at your own minimal example and see where it differs from these essentials.

    So the header says this is a PDF that may include transparency (%PDF-1.4 or more) and is to be considered ANSI binary rather than ASCII (%€€€€ or more). The first 4 objects define the way to build the document which is one blank page, since annotations are not drawn within a PDF page. Page 0 (3 0 obj) has a link to additional related comments (/Annots[5 0 R])

    So we start here with any problems.

    The most common problem are the appearances since if the object is locked the linked graphics may not become visible. (/AP<</N 6 0 R>>/C[0 0 0]/CA .5/BS<</W 2>>/IC[1 0 0]).
    Here we see that request that the Inner Colour be shown as red, but it does not appear.

    If we look at 6 0 obj we see there is no mention of using red. And it also is potentially unstable unlike perhaps a PolyGon ? But that is a different viewer problem.

    enter image description here

    When the 3 vertices are correctly written it should only be 2 lines and will be seen in Acrobat and Chrome no problem.

    enter image description here

    %PDF-1.7
    %€€€€
    1 0 obj <</Type/Catalog/Pages 2 0 R>> endobj
    2 0 obj <</Type/Pages/Count 1/Kids[3 0 R]>> endobj
    3 0 obj <</Type/Page/Parent 2 0 R/MediaBox[0 0 100 100]/Contents 4 0 R/Resources<<>>/Annots[5 0 R]>> endobj
    4 0 obj <</Length 0>>
    stream
    endstream
    endobj
    5 0 obj <</Type/Annot/AP<</N 6 0 R>>/BS<</W 2>>/C[0 0 0]/CA .5/CreationDate(D:20250309024714Z)/F 132/M(D:20250309025157Z)/NM(cb820a70-966a-4c7b-ba410bde48de499a)
    /P 3 0 R/Rect[21.12 3.94 63.37 23.68]/Subj(Polygon Line)/Subtype/PolyLine/Vertices[23.12 5.94 56.87 21.68 61.37 13.43]>> endobj
    6 0 obj <</Type/XObject/BBox[21.12 3.94 63.37 23.68]/Length 23/Matrix[1 0 0 1 -21.12 -3.94]/Resources<</ExtGState<</GS0 7 0 R>>/XObject<</Fm0 8 0 R>>>>/Subtype/Form>>
    stream
    q /GS0 gs q /Fm0 Do Q Q
    endstream
    endobj
    7 0 obj <</Type/ExtGState/ca .4/CA .4>> endobj
    8 0 obj <</Type/XObject/BBox[21.12 3.94 63.37 23.68]/Group<</I false/K false/S/Transparency>>/Length 59/Resources<<>>/Subtype/Form>>
    stream
    q 2 w 23.12 5.94 m 56.87 21.68 l 61.37 13.43 l 0 0 0 RG S Q
    endstream
    endobj
    xref
    0 9
    0000000000 65536 f 
    0000000015 00000 n 
    0000000060 00000 n 
    0000000111 00000 n 
    0000000219 00000 n 
    0000000265 00000 n 
    0000000555 00000 n 
    0000000770 00000 n 
    0000000817 00000 n 
    trailer
    <</Size 9/Root 1 0 R/ID[<F4B68448FD7DE34283F6D059675615A0><F4B68448FD7DE34283F6D059675615A0>]>>
    startxref
    1034
    %%EOF
    

    So now you know what to look for in your own minimal example.