如何动态地将值插入到现有的PDF中? [英] How to insert values into an existing PDF on the fly?

查看:79
本文介绍了如何动态地将值插入到现有的PDF中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个PDF,其中包含一些字段以接受用户的值(例如:生物数据"表格).我的问题是,如何将用户输入插入现有PDF的正确"字段并生成填充的PDF? 如果我使用iTextSharp,那么如何选择坐标来打印值?

是否有设计工具来设计矩形字段以接受值? 因为我的PDF模板有很多字段可以从用户端获取值. 广告中的tnx.

解决方案

有两种可能性:

您的原始PDF是一种表格:

您可以通过检查PDF是否具有如下所述的任何字段来进行检查:使用Java编程将pdf可编辑字段转换为文本

您需要使Java代码适应C#代码,或者您可以使用RUPS,如我对问题操作现有PDF 水印示例也应该很有趣. >

我建议不要使用第二种方法,因为很难找到要使用的确切坐标.如果您的PDF不是表单,请使用Adobe Acrobat将其转换为表单,然后使用第一种方法.第一种方法是面向未来的证明:如果您必须更改表单中的某些内容,则无需更改代码即可更改该表单(前提是您保留了原始字段名称).

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.

解决方案

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

这篇关于如何动态地将值插入到现有的PDF中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆