Editing a PDF in MAUI / .NET

2 minute(s) read

MAUI is a framework that allows to develop native applications targeting both mobiles and desktops using C# and .NET. This framework is very powerful and supports many complex use cases, one of them is working with PDF documents programmatically.

This small example shows the use of the iTextSharp PdfStamper to modify the fields of an editable PDF, in a .NET MAUI application.

PDF edition with iTextSharp

After creating a MAUI project, the first step consists of including the iTextSharp lib in the project:

dotnet add package XamiTextSharpLGPLv2 --version 1.0.0

I use here a package under LGPL license, because it uses an old version of iTextSharp which was still under LGPL.

Depending on the type of project and the desired features, it may be necessary to use a more recent version, iText 5 or iText 7, available under AGPL or paid licenses.

In the example linked to this article, the editable PDF file is included in the project as an “Embedded Resource”, which allows the application to be independent of any server.

To read an embedded resource, simply do the following:

var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var inputStream = assembly.GetManifestResourceStream("TestPdfMauiApp.Resources.feuille_de_personnage_editable.pdf");

We could use any class of the current application instead of MainPage to get the right assembly.

Now that we have a stream to the editable PDF, we can open it with iTextSharp :

var reader = new PdfReader(inputStream);

To create the PdfStamper that will allow us to modify the editable fields, we also need an output stream.

Since we are in a MAUI application, the MAUI Essentials lib contains everything we need:

var outputFilename = Path.Combine(FileSystem.CacheDirectory, "feuille_de_personnage_modifiee.pdf");
var outputStream = File.Create(outputFilename)

The destination file will be created in the application’s cache directory.

var stamper = new PdfStamper(reader, outputStream);

Now that we have our stamper, we can list all the editable fields in the PDF:

var form = stamper.AcroFields;
var fields = form.Fields;
foreach (DictionaryEntry field in fields)
{
    var item = field.Value as AcroFields.Item;
    Debug.WriteLine(field.Key);
    form.SetField(field.Key.ToString(), field.Key.ToString());
}

By the way, we have filled in each editable field with its own name, which makes it easy to identify them. Useful if you are not the creator of the PDF used!

And finally, nothing could be easier than to fill our PDF dynamically with the desired values:

form.SetField("Nom", "Legolas");
form.SetField("Niveau", "5");
form.SetField("Race", "Elfe");
form.SetField("Classe", "Ranger");
form.SetField("Alignement", "Chaotique Bon");
form.SetField("Historique", "");
form.SetField("Trait de personnalité",
   "Fils de Thranduil\n\n" +
   "Compagnon de l'Anneau\n\n" +
   "Ami des Nains"
   );
form.SetField("For Valeur", "17");
form.SetField("For MOD", "+3");
form.SetField("Dex Valeur", "20");
form.SetField("Dex MOD", "+5");
form.SetField("Con Valeur", "16");
form.SetField("Con MOD", "+3");
form.SetField("Int Valeur", "8");
form.SetField("Int MOD", "-1");
form.SetField("Sag Valeur", "13");
form.SetField("Sag MOD", "+1");
form.SetField("Cha Valeur", "12");
form.SetField("Cha MOD", "+1");

And that’s it!

Capture

Of course, don’t forget to close all the objects used, whether it’s the streams or the PdfStamper or the PdfReader, as the latter two are not disposable objects in this version of the lib. An omission would lead to possible complications when opening your freshly created file in another application!

To display it in an external application, nothing could be easier:

Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(outputFilename) });

Conclusion

itext is a very complete pdf editing library, available in both .net and java. Much of the article could be used in the same way on your favorite server!

I’ll come back in a future article on how to use this iTextSharp lib to create a PDF from scratch, but not right now, I have several other topics to follow up on first.

Resources

Sources of the article + french

feuille_de_personnage_editable.pdf

feuille_de_personnage_modifiee.pdf

Written by

Yan Maniez

Mobile developer, Expert