Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use PDFBox to fill out a PDF Form

I have a pdf with a form in it. I am trying to write a class that will take data from my database and automatically populate the fields in the form.

I have already tried ITextSharp and their pricing is out of my budget, even though it works perfectly fine with my pdf. I need a free pdf parser that will let me import the pdf, set the data, and save the PDF out, preferably to a stream so that I can return a Stream object from my class rather than saving the pdf to the server.

I found this pdf reader and it doesn't work. Null reference errors are abundant and when I tried to "fix" them, it still couldn't find my fields.

So, I have moved on to PdfBox, as the documentation says it can manipulate a PDF, however, I cannot find any examples. Here is the code I have so far.

var document = PDDocument.load(inputPdf);

var catalog = document.getDocumentCatalog();

var form = catalog.getAcroForm();

form.getField("MY_FIELD").setValue("Test Value");

document.save("some location on my hard drive");
document.close();

The problem is that catalog.getAcroForm() is returning a null, so I can't access the fields. Does anyone know how I can use PdfBox to alter the field values and save the thing back out?

EDIT:

I did find this example, which is pretty much what I am doing. It's just that my acroform is null in pdfbox. I know there is one there because itextsharp can pull it out just fine.

like image 959
Josh Avatar asked Jul 28 '11 14:07

Josh


1 Answers

Have you tried with the 1.2.1 version?

http://pdfbox.apache.org/apidocs/overview-summary.html

like image 142
SIMS Avatar answered Oct 03 '22 23:10

SIMS