Firas S Firas S - 8 days ago 4
C# Question

ItextSharp read textbox text

I am trying to read PDF files using ItextSharp, however I have noticed that the text included in a textbox is not read.

Please any advise.

PdfDictionary pagedic = pdfRead.GetPageN(page);
PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS));
if (annotarray == null || annotarray.Size == 0)
continue;
string Title = "";
string Title2 = "";
string Content2 = "";
foreach (PdfObject A in annotarray.ArrayList)
{
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);
Title2 += AnnotationDictionary.GetAsString(PdfName.T).ToString() + "\n";
if (AnnotationDictionary.GetAsString(PdfName.CONTENTS)!=null)
{
Content2 += AnnotationDictionary.GetAsString(PdfName.CONTENTS).ToString() + "\n";
}
if (AnnotationDictionary.GetAsString(PdfName.T)!=null)
{
Title += AnnotationDictionary.GetAsString(PdfName.T).ToString() + "\n";
}
if (AnnotationDictionary.GetAsString(PdfName.T)!=null)
{
txt += AnnotationDictionary.GetAsString(PdfName.T).ToString() + "\n";
}
if (AnnotationDictionary.GetAsString(PdfName.CONTENTS)!=null)
{
txt += PdfName.CONTENTS.ToString() + "\n";
}
txt += AnnotationDictionary.GetAsString(PdfName.NOTE) + "\n";
txt += AnnotationDictionary.GetAsString(PdfName.FREETEXT)+"\n";
}
MessageBox.Show(txt);
MessageBox.Show(Title);
MessageBox.Show(Title2);
MessageBox.Show(Content2);

Answer

The Problem is that I was reading the content as string, where it should not be. as in below. Content Text so using ToUnicodeString() solved the issue.

annotation.GetAsString(PdfName.CONTENTS).ToUnicodeString();