How to write custom metadata to a PDF document in Java with PDFBox

Eni Sinanaj
Oct 22, 2018 · 4 min read

I needed to think of a secure system to produce PDF documents, make them sign to public users, and have them upload the signed PDF documents.

Obviously the signed document should have the same content as the original one with in addition the digital signature.

For the solution I was given a technology stack I had to use which was, Java with Firebase and some JSP pages for the frontend. Most of the webapp had to be built in JavaScript using the Firebase JavaScript API with the exception of the PDF management component that had to be all Java.

I’ll write a tutorial on how to create a simple Java web application as soon as I can. I’ll show some Maven features, some Gradle features, JSP, JSF, CDI and more, on different occasions.

Thinking of a way to built the solution I made a trivial map of the actors that had to work together

  • Java
  • PDF
  • Digital signature
  • Time

Thus I started to get to know a bit better PDF, Digital Signature and time.
I found the following two images (among others) very interesting

This article is not about what a PDF document is made of and how the digital signatures are stored in it so I’m going to leave it with this. Maybe it’s a nice topic for another article.

I’m assuming you (the reader) have a working Java application (I’ll share mine on GitHub at the end of this article anyway) and also that you know the basics of Java.

I’m going to put aside now the requirements since that would require something more elaborate and I’ll focus on trying to implement a simple solution as suggested by the customer.

Let’s add some hash/key/identifier to the original document and compare it with the same information that would be present on the signed document. (There are huge flaws in this solution I know, but I’m interested in learning how to insert information to a PDF document).

It is simple enough. In Java using PDFBox (a Java PDF library) you can do the following:

After this, when you open the newly created document with say Acrobat Reader and go to the properties tab you’ll see the following

How about reading these information with Java? It’s just as simple. Here’s the Java method that is sufficient to reading the metadata (custom or not)

Yeah I know, all that introduction to show just a couple of methods. But I’ll share more on how to get things done with PDFBox since I, myself, found it difficult to understand and the documentation or anything online for that matter didn’t help very much.

Example project on GitHub:

Eni Sinanaj

Written by

#dev #mobiledev #startup #entrepreneur #business #money #excess #earth #motivational #speaker #hype #manager #startup #consulting

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade