Marco Araujo
Marco Araujo

Reputation: 163

itextsharp - Problems reading PDFs with 1 column (page1) and 2 columns (page2)

My code below is lost when opening PDF file which has only one column on the front page and more than 1 column on other pages.

Someone can tell me what I'm doing wrong? Below my code:

PdfReader pdfreader = new PdfReader(pathNmArq);
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

    for (int page=1; page <= lastPage; page++) 
    {
         extractText = PdfTextExtractor.GetTextFromPage(pdfreader, page, strategy);
         extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));
        / / ... 
    }

Upvotes: 0

Views: 246

Answers (1)

mkl
mkl

Reputation: 95918

You use the SimpleTextExtractionStrategy. This strategy assumes that the text drawing instructions in the PDF are sorted by the reading order. In your case that does not seem to be the case.

If you cannot count on the PDF containing drawing operations in reading order but are only using iText text extraction strategies from the distribution, you have to know areas which constitute a single column. If a page contains multiple columns, you have to use RegionTextRenderFilter to restrict to a column and then use the LocationTextExtractionStrategy.

PS: What exactly is your intention in that

extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));

line?

Upvotes: 1

Related Questions