PDF Anotations readonly .NET

270 Views Asked by At

I have a system that's adding anotations to a PDF in form of images. At present if users download the PDF they can click on the anotations and play with then: move, resize, remove...

I'd like a way to change the anotation to be readonly or to make them embeded on the PDF like if they were part of the PDF.

Is that possible? Anyone knows how to achive that using .NET ?

1

There are 1 best solutions below

0
On

You don't mention your PDF library of choice in your question text but you tagged it with (and not ); thus, I assume you use iText for .Net (aka iTextSharp) in a 5.5.x version.

You can make an annotation read-only and locked by setting the annotation flags accordingly, e.g. like this:

using (PdfReader pdfReader = new PdfReader(inputPath))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
    PdfArray vertices = new PdfArray(new int[] { 100, 100, 300, 300, 100, 300, 300, 100 });
    PdfAnnotation polyLine = PdfAnnotation.CreatePolygonPolyline(pdfStamper.Writer, pdfReader.GetPageSize(1),
        "", false, vertices);
    polyLine.Color = BaseColor.GREEN;
    polyLine.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_LOCKED | PdfAnnotation.FLAGS_PRINT;
    pdfStamper.AddAnnotation(polyLine, 1);
}

The meaning of these flags

Print (PDF 1.2) If set, print the annotation when the page is printed unless the Hidden flag is also set. If clear, never print the annotation, regardless of whether it is rendered on the screen. If the annotation does not contain any appearance streams this flag shall be ignored.

ReadOnly (PDF 1.3) If set, do not allow the annotation to interact with the user. The annotation may be rendered or printed (depending on the settings of the NoView and Print flags) but should not respond to mouse clicks or change its appearance in response to mouse motions.

This flag shall be ignored for widget annotations; its function is subsumed by the ReadOnly flag of the associated form field (see "Table 226 — Entries common to all field dictionaries").

Locked (PDF 1.4) If set, do not allow the annotation to be deleted or its properties (including position and size) to be modified by the user. However, this flag does not restrict changes to the annotation’s contents, such as the value of a form field.

(ISO 32000-2, Table 167 — Annotation flags)