Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Complex PDF document with C#

Tags:

c#

pdf

itext

See attached K-1 Document. I have attempted to use numerous tweaks with iTextSharp library but haven't had success in loading data correctly.

Ideally I would like to parse out the document similar to how humans would read them, one textbox at a time, reading its contents.

       var reader = new PdfReader(FILE, Encoding.ASCII.GetBytes(password));
        string[] lines;
        var strategy = new LocationTextExtractionStrategy();
        string currentPageText = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
        lines = currentPageText.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None);

I also tried playing with Annotation parsing but didn't have luck.

I'm a newbie and probably looking at wrong place. Can you help guide me in the right direction?

Thanks a lot.

enter image description here

like image 235
Ros Avatar asked Sep 12 '25 07:09

Ros


1 Answers

You would like to parse out the document similar to how humans would read them, one textbox at a time, reading its contents. That means you first will have to try and automatically recognize those text boxes. Then you can extract text by these areas.

To recognize those text boxes automatically in your document, you have to extract the border lines enclosing the boxes. For this you will first have to find out how those border lines are created. They might be drawn using vector graphics as lines or rectangles, but they could also be part of a background bitmap image.

Unfortunately I don't have your IRS form at hand and so cannot analyze its internals. Let's assume the borders are created using vector graphics for now. Thus, you have to extract vector graphics.

To extract vector graphics with iText(Sharp), you make use of classes from the iText(Sharp) parser namespace by making them parse the document and feed the parsing events into a listener you create which collects the vector graphic operations:

  • You implement IExtRenderListener, in particular its ModifyPath and RenderPath methods which respectively are called when additional path elements (e.g. lines or rectangles) are added to the current path or when the current path is rendered (stroked? filled?). Your implementation collects these information.
  • You parse your document into an instance of your listener, e.g. using PdfReaderContentParser.
  • You analyse the lines and rectangles found and derive the coordinates of the boxes they build.
  • You parse the same page in a LocationTextExtractionStrategy instance.
  • You retrieve the texts of the recognized text boxes by calling LocationTextExtractionStrategy.GetResultantText with a matching ITextChunkFilter argument for each box.

(Actually you can do the parsing into the instance of your listener and the LocationTextExtractionStrategy instance in one pass for a bit of optimization.)

All iText(Sharp) specific tasks are trivial, and the only other task, the analysis of the lines and rectangles found to derive the coordinates of the boxes, should be no big problem for a software developer proficient in C#.

like image 145
mkl Avatar answered Sep 14 '25 19:09

mkl