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)?:
Attached source PDF file: here