Search code examples
c#.netitext7

Set layer for markup (FreeText) PDF using itext7 .NET


How to set layer for the existing markup (FreeText) PDF using itext7 .NET? I use the code below but it does not work. Please help. Thanks.

public void SetLayerMarkup()
{
    string inPDF = @"C:\in PDF.pdf";
    string outPDF = @"C:\out PDF.pdf";
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(inPDF), new PdfWriter(outPDF));
    PdfLayer notPrint = new PdfLayer("Non Print", pdfDoc);
    int numberOfPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++)
    {
        PdfDictionary page = pdfDoc.GetPage(i).GetPdfObject();
        PdfArray annotArray = page.GetAsArray(PdfName.Annots);
        if (annotArray == null)
        {
            continue;
        }
        int size = annotArray.Size();
        for (int x = 0; x < size; x++)
        {
            PdfDictionary curAnnot = annotArray.GetAsDictionary(x);
            if (curAnnot.GetAsString(PdfName.Contents) != null)
            {
                string contents = curAnnot.GetAsString(PdfName.Contents).ToString();
                if (contents != "" && contents.Contains("old content")) //set layer for a FreeText with this content
                {
                    PdfDictionary layer = new PdfDictionary();
                    layer.Put(PdfName.Type, PdfName.OCG);
                    layer.Put(PdfName.Name, new PdfString("Non Print"));
                    curAnnot.Put(PdfName.OC, layer);
                }
            }
        }
    }
    pdfDoc.Close();
}

Or how to fix the code above to match this image (in iText RUPS 5.5.9)?:

enter image description here

Attached source PDF file: here


Solution

  • So your original document does not have any layers:

    original document

    First off, we need to create one with the helper API:

    PdfLayer layer = new PdfLayer("Non Print", pdfDocument);
    

    The code can now be made a bit simpler (my sample is in Java but conversion to C# should be very straighforward):

    PdfDocument pdfDocument = new PdfDocument(new PdfReader("in PDF.pdf"),
            new PdfWriter("out PDF.pdf"));
    PdfLayer layer = new PdfLayer("Non Print", pdfDocument);
    
    int numberOfPages = pdfDocument.getNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++) {
        PdfDictionary page = pdfDocument.getPage(i).getPdfObject();
        PdfArray annotArray = page.getAsArray(PdfName.Annots);
        if (annotArray == null) {
            continue;
        }
        int size = annotArray.size();
        for (int x = 0; x < size; x++) {
            PdfDictionary curAnnot = annotArray.getAsDictionary(x);
            if (curAnnot.getAsString(PdfName.Contents) != null) {
                String contents = curAnnot.getAsString(PdfName.Contents).toString();
                if (!contents.isEmpty() && contents.contains("old content")) //set layer for a FreeText with this content
                {
                    curAnnot.put(PdfName.OC, layer.getPdfObject());
                }
            }
        }
    }
    
    pdfDocument.close();
    

    As a result, you get a document with a layer:

    result

    If you click on the eye icon next to the layer name, the content of the layer becomes hidden:

    result with layer hidden