18

We are using iTextSharp with a C# WinForms application to parse a PDF file. Using iTextSharp, I can easily extract the text data from the PDF file. Suppose a PDF file contains an image surrounded by two lines of text. In this case, I could not extract the information about the image.

My requirement is:

  1. Get structural elements of the PDF file
  2. Process whether each is of type text, image, table or other

For example, the structural elements are similar to the following:

text :paragraph1
text :paragraph2
Image:Image
text :paragraph3
Table:table info
text :Paragraph4

If I can obtain information in a format like this, I can easily understand the text, image, table, header or footer information.

So, is it possible to get this kind of information using iTextSharp? If yes, please enlighten me on this. Otherwise, could you please suggest some other tools capable of meeting this requirement?

Thanks to all,

Saravanan

Sam
  • 40,644
  • 36
  • 176
  • 219
Saravanan
  • 11,372
  • 43
  • 143
  • 213

1 Answers1

5

I used to have this kind of need a while ago. I used this function (from Extract images using iTextSharp) :

private static PdfObject FindImageInPDFDictionary(PdfDictionary pg)
{
    PdfDictionary res =
        (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));


    PdfDictionary xobj =
      (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
    if (xobj != null)
    {
        foreach (PdfName name in xobj.Keys)
        {

            PdfObject obj = xobj.Get(name);
            if (obj.IsIndirect())
            {
                PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);

                PdfName type =
                  (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));

                //image at the root of the pdf
                if (PdfName.IMAGE.Equals(type))
                {
                    return obj;
                }// image inside a form
                else if (PdfName.FORM.Equals(type))
                {
                    return FindImageInPDFDictionary(tg);
                } //image inside a group
                else if (PdfName.GROUP.Equals(type))
                {
                    return FindImageInPDFDictionary(tg);
                }

            }
        }
    }

    return null;
}

As you can see in the foreach (PdfName name in xobj.Keys) statement, I think you can easily parse a whole PDF and treat every kind of data from it. But I'm not sure about the "verticality" part of your need.

Hope it could help you.

Community
  • 1
  • 1
cubitouch
  • 1,929
  • 15
  • 28
  • This code completely ignores whether the image in question is *used* at all on any visible page, let alone where in its structure. Furthermore it ignores inline images. Completely. – mkl Jan 18 '14 at 23:18
  • I used this code to extract images "from" a page, whenever the pdf is single or multiple pages. I didn't said that he should use this code block as is... Could you please help me improving my answer then ? – cubitouch Jan 19 '14 at 09:00
  • *Could you please help me improving my answer then ?* - well, you could start by pointing out to the op that his question implies a number of misconceptions. There are **no** paragraphs or tables in PDFs, merely some text chunks drawn in places. I assume that these misconceptions are some of the reasons why the question had remained unanswered for nearly a year before your answer. – mkl Jan 19 '14 at 15:25