如何使用iText for .NET填写这样的表单 [英] How to fill forms like this using iText for .NET

查看:106
本文介绍了如何使用iText for .NET填写这样的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用

 cb.SetTextMatrix(x, y);// x and y positions .
 cb.ShowText("data");

但是没有这样做.

问题

推荐答案

您使用的代码并非完全不正确,但是存在一些缺陷.对于初学者:您不知道xy参数的值,并且如果您希望文本位于正确的位置,那么这很关键.

The code you are using isn't entirely incorrect, but it has several flaws. For starters: you don't know the value of the x and y parameters, and that's kind of crucial if you want the text to be in the correct position.

此外:您正在将PDF语法直接写入内容流.在您的代码段中,您忘记了创建文本对象(使用cb.BeginText()cb.EndText()).如果您不熟悉PDF,除非您对ISO-32000-1 有深入的了解,否则不要尝试直接将PDF语法写入内容流.您读过ISO-32000-1吗?如果不是,那么那您为什么要使用低级操作?那没有多大意义,对吗?有诸如ColumnText之类的帮助器类可以在绝对位置添加内容.

Also: you are writing PDF syntax directly into the content stream. In your snippet, you forgot to create the text object (with cb.BeginText() and cb.EndText()). If you are new at PDF, you shouldn't try writing PDF syntax directly into the content stream unless you have a solid understanding of ISO-32000-1. Have you ever read ISO-32000-1? If not, then why are you using low-level operations? That doesn't make much sense, does it? There are helper classes such as ColumnText to add content at absolute positions.

查看您共享的屏幕快照,我发现某些字段需要合并"功能.此功能可确保每个小盒子仅包含一个字形(如果您不知道字形是什么,可将其视为字符的视觉表示).

Looking at the screen shot you shared, I see that some fields require "Comb" functionality. This functionality makes sure that each small box contains exactly one glyph (if you don't know what a glyph is, think of it as the visual representation of a character).

如果您想使自己更轻松,则应首先测试表单是否是交互式的.回答这个问题:

If you want to make it easy on yourself, you should test if the form is interactive first. Answer this question:

该表单是否包含AcroFields?

Does the form contain AcroFields?

如果答案为是",则使用AcroFields对象填写表格.您可以按照此问题的答案中的说明找出要使用的字段名称:

If the answer is "Yes", fill out the form using the AcroFields object. You can find out which field names to use by following the instructions in the answer to this question: How do I enumerate all the fields in a PDF file in ITextSharp ?

如果答案为否",请在Adobe Acrobat中打开文件,然后手动添加字段.将字段定义为Comb字段,以便每个框包含每个字形.为了获得美观的结果,请选择等宽字体,例如Courier(使用比例字体可能会给您带来难看的结果).此操作将添加AcroForm字段.

If the answer is "No", open the file in Adobe Acrobat, and manually add fields. Define the fields as Comb fields so that each box contains each glyph. To get a nice-looking result, select a monospaced font such as Courier (using a proportional font will probably give you an uglier result). This operation adds AcroForm fields.

一旦您有了一个带有AcroFields的交互式表单(假设您已经正确定义了它们),填写表单就很容易,就像在iText 5中一样:

Once you have an interactive form with AcroFields (assuming you have defined them correctly), filling out the form is as easy as this in iText 5:

PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader,
    new FileStream(newFile, FileMode.Create));
AcroFields form = stamper.AcroFields;         
form.SetField(key1, value1);      
form.SetField(key2, value2);      
form.SetField(key3, value3);
...
stamper.Close();

请参见如何创建和填写PDF表单

但是,由于您是新手,因此我建议您使用

However, since you are new at all of this, I recommend that you use iText 7 as described in the jump-start tutorial:

PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue(key1, out toSet);
toSet.SetValue(value1);
fields.TryGetValue(key2, out toSet);
toSet.SetValue(value2);
...
pdf.Close();

如果要在填写表格后删除交互性,则需要拼合字段.官方网站上也有记录.

If you want to remove the interactivity after filling out the form, you need to flatten the fields. This is also documented on the official web site.

这篇关于如何使用iText for .NET填写这样的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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