Search code examples
c#itextitext7

How to get and set value for /BSIColumnData of an annotation PDF itext c#


How to get and set value for /BSIColumnData of an annotation (markup) in PDF using itext c# as attached file? I'm using Itext7 code below, but it is error at BSIColumnData:

public void BSIcontents ()
    {
        string pdfPath = @"C:\test PDF.pdf";
        iText.Kernel.Pdf.PdfReader pdfReader = new iText.Kernel.Pdf.PdfReader(pdfPath);
        iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(pdfReader);
        int numberOfPages = pdfDoc.GetNumberOfPages();

        int z = 0;
            for (int i = 1; i <= numberOfPages; i++)
            {
                iText.Kernel.Pdf.PdfDictionary page = pdfDoc.GetPage(i).GetPdfObject();
                iText.Kernel.Pdf.PdfArray annotArray = page.GetAsArray(iText.Kernel.Pdf.PdfName.Annots);
                if (annotArray == null)
                {
                    z++;
                    continue;
                }
                int size = annotArray.Size();
                for (int x = 0; x < size; x++)
                {
                    iText.Kernel.Pdf.PdfDictionary curAnnot = annotArray.GetAsDictionary(x);
                    if (curAnnot != null)
                    {
                        if (curAnnot.GetAsString(iText.Kernel.Pdf.PdfName.BSIColumnData) != null)
                        {
                            MessageBox.Show("BSIColumnData: " + curAnnot.GetAsString(iText.Kernel.Pdf.PdfName.BSIColumnData).ToString());
                        }
                    }
                }
            }
        pdfReader.Close();
    }

In Bluebeam Revu, you can see as below: enter image description here

In Itext-rups 5.5.9, you can see as below: enter image description here


Solution

  • I see two errors:

    • You try to use the BSIColumnData name like this:

      iText.Kernel.Pdf.PdfName.BSIColumnData
      

      This assumes that there is already a static PdfName member for your custom name. But of course there isn't, there only are predefined members for standard names used in itext itself. If you want to work with other names, you have to create a PdfName instance yourself and use that instance, e.g. like this

      var BSIColumnData = new iText.Kernel.Pdf.PdfName("BSIColumnData");
      
    • You try to retrieve the value of that name as string

      curAnnot.GetAsString(iText.Kernel.Pdf.PdfName.BSIColumnData)
      

      but it is clear from your RUPS screenshot that the value of that name is an array of strings. Thus, even after correcting as described in the first item GetAsString(BSIColumnData) will return null. Instead do

      var BSIColumnData = new iText.Kernel.Pdf.PdfName("BSIColumnData");
      var array = curAnnot.GetAsArray(BSIColumnData);
      

      After checking if (array != null) you now can access the strings at their respective indices using array.GetAsString(index).