Reading PDF files with C#

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
Search for a command to run...

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
A recent requirement came up for configurable dynamic forms in a mobile application. There are a few dynamic form packages for Flutter, but I wanted a bit more control over the schema and behavior of the form. Here follows my attempt at creating dyna...

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API. JSON Serialization in Flutter can also take a fair amount of boilerplate...

Back in May 2020, Microsoft announced a new feature for Azure Blob Storage called Blob Index. Essentially, this enables you to add key/value tags to your Blob objects and be able to query said Blob objects without having to use a separate service lik...

In my last post, "Using AWS Cognito with Xamarin Forms", I showed how to authenticate with Amazon Cognito using Xamarin Forms and the Xamarin.Essentials Web Authenticator. In this post, we'll go through the process of using the AWS Cognito Hosted UI ...

In this post, I'll show you how to quickly and easily set up user authentication for your Xamarin Forms app using Amazon Cognito. AWS Cognito is a user identity management solution by Amazon. It is a really easy way to add authentication to your appl...

Recently I needed to grab some text values from a number of pdf files. Instead of having to manually open each and every pdf file I just knew there had to be an easier way.
After a quick search, I found the solution; iTextSharp, an open source C# library that allows you to do a host of awesome stuff with pdf files. It is a port of iText which is a Java library. You can find more info about iText on their website at www.itextpdf.com . I just knew this library is something else when I saw they had an entire book dedicated to it.
Manipulating and reading pdf files is no trivial task, but luckily for me the pdf files I needed to read were fairly straight forward and I used the following code to return the contents of the file as one big string:
private string ParsePdf(string filePath)
{
string text = string.Empty;
PdfReader reader = new iTextSharp.text.pdf.PdfReader(filePath);
byte[] streamBytes = reader.GetPageContent(1);
PRTokeniser tokenizer = new PRTokeniser(streamBytes);
while (tokenizer.NextToken())
{
if (tokenizer.TokenType == PRTokeniser.TokType.STRING)
{
text += tokenizer.StringValue;
}
}
return text;
}
From there I used some string manipulation to grab the values I needed and perform some additional logic. Easy!
Links from this post: