2

I'm working on a program that is attempting to parse data within a PDF file. The issue is we need to parse a certain section of the PDF that is formatted in a three-tier hierarchy: Heading, sub-heading, paragraph. So far we're using line breaks (\n) to try and parse the data, but it is largely inefficient. Even if we had all the possible values for the headings and sub-headings the solution would still not be ideal since new PDFs might be created with new heading and sub-heading values.

However, the PDFs are created in such a way that the heading is the closest to the left margin, with the sub-heading being indented further away, and the indentation being even greater on the paragraph. We'd like to be able to use the indentations to identify the lines we want to parse. Is there a PDF library for Visual Studio that can do this? I'm coding in C#.

2

PDF files are designed for displaying content and not for storing content in an organized manner. PDF files do not contain heading, subheadings or paragraphs. What you see on the PDF page as headings or paragraphs, inside the PDF file they are the same thing, some text written at fixed positions with different fonts so that visually you have headings or paragraphs.
The indentations you see do not exist as specific objects in the PDF file. The indentations are achieved by writing the heading a X coordinate, the subheading at X + 50 and the paragraph content at X + 100 (50 and 100 are just dummy examples).
What you need to do is to extract the text fragments to include also the text position. You organize the text fragments on lines based on Y position of text fragments (text fragments with same Y are on the same line). The text fragments are added to the line based on their increasing X position. Once you have the lines you inspect the X position of each line (the X position of the first text fragment in the line). Smallest X represents a heading, next X represents a subheading and the next X represents a paragraph.
The company I work for develops the XFINIUM.PDF library which can help you with text extraction. You have to implement the logic above on the extracted text fragments.

1

I'd suggest you look at iTextSharp. I've used the Java version to create, update, and inspect PDF files. Best of all, it's fully supported on StackExchange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.