iTextSharp.LGPLv2.Core get text from PDF into a string

Viewed 2582

recently our project upgraded to a new iTextSharp.LGPLv2.Core v1.6.5. I had a method which extracted a text from the PDF file.

Back then I used this:

        if (File.Exists(pdf1Path))
        {
            var pdfReader = new PdfReader(pdf1Path);
            string pdfText;
            string currentText;

            //Text extracting to List
            for (int i = 1; i <= pdfReader.NumberOfPages; i++)
            {
                currentText = PdfTextExtractor.GetTextFromPage(pdfReader, i);
                currentText =
                    Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8,
                        Encoding.Default.GetBytes(currentText)));
                text.Append(currentText);
            }

            pdfText = text.ToString();
         }

Now suddenly I cannot recognize "PdfTextExtractor". Is there any other option on how to get this working? Note that I am not allowed to install any other libraries or packages.

I tried to use

using iTextSharp.text.pdf.parser;

but it is not recognized anymore. And when I try to download it, it just overlaps with iTextSharp.LGPLv2.Core which gives me an error.

Thanks for the help

2 Answers

IIRC (and if I read the git history correctly), the namespace iTextSharp.text.pdf.parser was introduced in iTextSharp 5.0.2, i.e. it was never part of the LGPL licensed iTextSharp releases.

(The situation differs a bit for the iText/Java releases, here first proofs-of-concept were already present in the latest LGPL releases.)

Thus,

recently our project upgraded to a new iTextSharp.LGPLv2.Core v1.6.5

If you really upgraded, then your previous version appears to have been accompanied by a backported (from version 5.x) or cross-ported (from iText/Java before version 5) parser namespace in it. More likely is, though, that you actually downgraded from an iTextSharp 5.x to a fork based on the iTextSharp 4.2 or earlier, and in downgrading you usually lose features.

I assume you use iTextSharp.LGPLv2.Core to make use of the LGPL instead of the choice between AGPL and commercial license in iTextSharp 5, or you do it for Core support.

If it's really about the license, you only either can try and port the iText/Java parser package in the last LGPL release (2.1.7) or tag (4.2.0), or you can re-implement text extraction completely independently.

If it's about Core support and you would be ready to buy a license or to be subject to the AGPL, you can also try and backport the latest iText 5.x parser namespace. This should be easier than crossporting from Java, and this text extraction code is far more advanced than the iText/Java code from before version 5.

After some time of digging I found out that there is a work around to this issue. Since I am not allowed to use PdfTextExtractor, I used this code which works nearly the same as mentioned method

        var reader = new PdfReader();
        var pdfFile = createSamplePdfFile();
        var reader = new PdfReader(pdfFile);

        var streamBytes = reader.GetPageContent(1);
        var tokenizer = new PrTokeniser(new RandomAccessFileOrArray(streamBytes));

        var stringsList = new List<string>();
        while (tokenizer.NextToken())
        {
            if (tokenizer.TokenType == PrTokeniser.TK_STRING)
            {
                stringsList.Add(tokenizer.StringValue);
            }
        }

        reader.Close();

Hope this will help someone with similar issue than me :)

Thanks all :)

Related