Ink Annotation is not showing in PDF

154 views
Skip to first unread message

Madushan Amarasinghe

unread,
May 19, 2022, 10:54:23 AM5/19/22
to pdfium
Hi,

I'm trying to convert ink drawings to an ink annotation and save it to a PDF. When I analyze the PDF after the process the annotation is available in the Annots array but it is not visible.
(It shows up in the annotation list of Acrobat Reader).

It seems the stroke color and the stroke size is not properly set to the annotation.

Below is my logic, could anyone please help me to see if I'm doing something wrong here.

using(var fdfAnnotation = page.CreateAnnot(page, annotation.AnnotationType)) {
    var fs_points = new List < FS_POINTF > ();
    var color = annotation.DrawingAttributes.Color;

    var initialStroke = annotation.InkStrokes[0];
    var initialPoint = new FS_POINTF(
        (float) initialStroke.GetInkPoints()[0].Position.X,
        (float) initialStroke.GetInkPoints()[0].Position.Y
    );

    var pageObject = PDFium.FPDFPageObj_CreateNewPath(initialPoint.X1, initialPoint.Y1);

    for (int i = 0; i < annotation.InkStrokes.Count; i++) {
        var stroke = annotation.InkStrokes[i];
        initialPoint = new FS_POINTF(
            (float) stroke.GetInkPoints()[0].Position.X,
            (float) stroke.GetInkPoints()[0].Position.Y
        );

        if (i > 0)
            PDFium.FPDFPath_MoveTo(pageObject, initialPoint.X1, initialPoint.Y1);

        var points = stroke.GetInkPoints();
        for (int j = 0; j < points.Count; j++) {
            var fs_point = new FS_POINTF((float) points[j].Position.X, (float) points[j].Position.Y);

            if (j > 0)
                PDFium.FPDFPath_LineTo(pageObject, fs_point.X1, fs_point.Y1);

            fs_points.Add(fs_point);
        }

        unsafe {
            fixed(FS_POINTF * ptr = fs_points.ToArray())
            fdfAnnotation.Annot_AddInkStroke(fdfAnnotation.Handle, ptr, fs_points.ToArray().Length);
        }

        fs_points.Clear();
    }

    if (annotation.DrawingAttributes.DrawAsHighlighter)
        fdfAnnotation.SetObjBlendMode(pageObject, "Multiply");

    PDFium.FPDFPageObj_SetLineCap(pageObject, (int) FPDF_LINE_CAP.CIRCLE);
    PDFium.FPDFPageObj_SetLineJoin(pageObject, (int) FPDF_LINE_JOIN.LINE_JOIN_ROUND);
    PDFium.FPDFPageObj_SetStrokeColor(pageObject, (uint) color.R, (uint) color.G, (uint) color.B, (uint) color.A);
    PDFium.FPDFPageObj_SetStrokeWidth(pageObject, (float) annotation.DrawingAttributes.Size.Width);

    fdfAnnotation.AppendObj(fdfAnnotation.Handle, pageObject);

    fdfAnnotation.SetColor(fdfAnnotation.Handle, FPDFANNOT_COLORTYPE.FPDFANNOT_COLORTYPE_Color,
        (uint) color.R,
        (uint) color.G,
        (uint) color.B,
        (uint) color.A);

    fdfAnnotation.SetStringValue(fdfAnnotation.Handle, "ca", ((float) color.A / 255).ToString());

    if (annotation.DrawingAttributes.DrawAsHighlighter)
        fdfAnnotation.SetStringValue(fdfAnnotation.Handle, "IT", "InkHighlight");

    SetFPDFAnnotationMetaData(annotation, fdfAnnotation);
}

private void SetFPDFAnnotationMetaData(BaseAnnotation annotation, PdfAnnotation fdfAnnotation) {
    var rect = new FS_RECTF();
    rect = annotation.Rect;
    fdfAnnotation.SetRect(fdfAnnotation.Handle, ref rect);
    fdfAnnotation.SetStringValue(fdfAnnotation.Handle, "CreationDate", annotation.CreationDate);
    fdfAnnotation.SetStringValue(fdfAnnotation.Handle, "M", annotation.ModifiedDate);
    if (!string.IsNullOrEmpty(annotation.Contents))
        fdfAnnotation.SetStringValue(fdfAnnotation.Handle, "Contents", annotation.Contents);
}

I added the 'cs' and 'IT' string values to the annotation since they were used in other viewers and I wanted to maintain the compatibility.

Thanks.

Madushan Amarasinghe

unread,
May 19, 2022, 11:12:37 AM5/19/22
to pdfium
This is the document which has the generated ink annotation.
embedded_doc.pdf
Reply all
Reply to author
Forward
0 new messages