There are two possibilities:
Your original PDF is a form:
You can check this by checking if the PDF has any fields as explained here: convert pdf editable fields into text using java programming
You'll need to adapt the Java code to C# code or you can use RUPS as shown in my answer to the question How to get specific types from AcroFields? Like PushButtonField, RadioCheckField, etc
In this case, filling out the form is easy:
PdfStamper pdfStamper = new PdfStamper(new PdfReader(templateFile), new FileStream(fileName, FileMode.Create));
AcroFields acroFields = pdfStamper.AcroFields;
acroFields.SetField(key, value);
pdfStamper.FormFlattening = true;
pdfStamper.Close();
You can have as many lines with SetField()
as you want. In these lines key
is the field name as defined in the original form; value
is the value you want to add at the position(s) of that field.
The line with the pdfStamper.FormFlattening
is optional. If you set that value to true
, all interactivity will be removed: the form will no longer be a form. If you remove the line or set that value to false
, then the form will still be a form. You'll be able to change the content of the fields and extract the value of the fields.
Your original PDF is not a form:
A PDF may look like a form to the human eye, but if it doesn't have AcroForm fields (and no XFA either), then a machine won't consider it as being a form. In this case, you have to understand that all the content is fixed at fixed coordinates on the page. You can add content at absolute positions, but the original content won't move.
There are different ways to add content to an existing PDF and they all involve PdfStamper
. Once you have obtained PdfContentByte
object from this PdfStamper
then you can add text as explained in the documentation. Read the sections Manipulating existing PDFs and Absolute positioning of text or take a look at the content tagged with the keyword PdfStamper. The watermark examples should be interesting too.
I would advice not to use this second approach as it is very hard to find the exact coordinates to use. If your PDF isn't a form, turn it into a form using Adobe Acrobat and use the first approach. The first approach is much more future proof: if you ever have to change something in your form, you can change that form without having to change your code (provided that you preserve the original field names).