Trying to read callouts, how to get value of /IT from PdfDictionary in iTextSharp?

徘徊边缘 提交于 2019-12-08 13:15:17

问题


I want to read the callout text boxes in a PDF. I'm using iTextSharp to iterate through all the annotations as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace PDFAnnotationReader
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder text = new StringBuilder();
            string fileName = @"C:\Users\J123\Desktop\xyz.pdf";
            PdfReader pdfReader = new PdfReader(fileName);
            PdfDictionary pageDict = pdfReader.GetPageN(1);
            PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
            for (int i=0;i<annotArray.Size;i++)
            {
                PdfDictionary curAnnot = annotArray.GetAsDict(i);
            }
        }
    }

Examining the hashMap of curAnnot, I see that when I get to an annotation that is a callout text box, the dictionary includes the following key-value pairs:

{[/IT,/FreeTextCallout]}
{[/Contents,xyz this is a callout]}

So I think what I should do is check each annotation to see if it includes the key /IT with the value /FreeTextCallout and if so, get the value of /Contents as a string like so:

if (curAnnot.Contains(PdfName.IT))
{
    if (curAnnot.Get(PdfName.IT)==PdfName.FREETEXTCALLOUT)
    {
        Console.Writeline(curAnnot.Get(PdfName.CONTENTS).ToString());
    }
}

But there doesn't seem to be a PdfName.IT or PdfName.FREETEXTCALLOUT. How do I check for the existence of /IT and retrieve its value?


回答1:


You can create your own PdfName objects using the constructor on PdfName:

new PdfName("IT");

So:

var myPdfNameIT = new PdfName("IT");
if (curAnnot.Contains(myPdfNameIT)) {
    //...
}


来源:https://stackoverflow.com/questions/25126997/trying-to-read-callouts-how-to-get-value-of-it-from-pdfdictionary-in-itextshar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!