How to insert values into an existing PDF on the fly?

*爱你&永不变心* 提交于 2021-02-08 11:42:32

问题


There is a PDF with some fields to accept values from the user(for example: a "bio data" form). My question is that how can I insert the user inputs to the Correct fields of the existing PDF and to generate the filled PDF? if i using iTextSharp, then how can i choose the co ordinates to print values?

Is there any design tools to design rectangle fields to accept values? because my PDF template have lots of fields to get values from user side. tnx in adv.


回答1:


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).




回答2:


ItextSharp provides you to do the same, using pdfStamper class of ItextSharp. Just a sample for your reference.

//create pdfreader instance and read content of existing PDF file into it, by providing it's path
PdfReader pdfReader = new PdfReader(FILE_PATH);

// create stamper instance to edit the exiting file
PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);

// perform your edit operation here.....
    .
    .
    .
// close pdfStamper instance
stamper.Close();


来源:https://stackoverflow.com/questions/34508058/how-to-insert-values-into-an-existing-pdf-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!