使用c#代码将listchoices添加到Pdfform [英] Adding listchoices to Pdfform with c# code

查看:123
本文介绍了使用c#代码将listchoices添加到Pdfform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过C#和itextsharp库向pdf表单中的List-field添加选项。但我无法找到办法做到这一点。
表格已经存在,我是用Acrobat创建的。我希望PDF中的Listfield选项与我的程序中的相同。
因此我想通过itextsharp在列表字段中创建选项以减少维护。
但我找不到这样做。使用库中的PDFstamper,我可以从表单中填充Fields。并着色一些领域。
是否有可能通过c#Code向List-field添加选项?
如果有人知道答案并向我展示了实现这一目标的方法,那就太棒了。

I'm trying to add choices to a List-field within a pdf form via C# and the itextsharp library. But i cant find a way to do this. The form already exists, i created it with Acrobat. I would like that the Listfield choices from the PDF are the same as in my program. Therefore I want to create the options in the list-fields via itextsharp to reduce the maintenance. But I cant find away to do this. With the PDFstamper from the library I am able to fill the Fields from the form. And Color some fields. Is there a possibility to add options to a List-field via c# Code ? Would be great if someone knows the answer and shows me a way to realize this.

推荐答案

如果您使用的是iText 7,那很简单。这在 iText 7快速入门教程的第5章中有所解释。 :操纵现有PDF文件

If you are using iText 7, it's easy. That's explained in chapter 5 of the iText 7 jump-start tutorial: Manipulating an existing PDF document

PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getFormFields();
List<PdfString> options = new ArrayList<PdfString>();
options.add(new PdfString("Any"));
options.add(new PdfString("8.30 am - 12.30 pm"));
options.add(new PdfString("12.30 pm - 4.30 pm"));
options.add(new PdfString("4.30 pm - 8.30 pm"));
options.add(new PdfString("8.30 pm - 12.30 am"));
options.add(new PdfString("12.30 am - 4.30 am"));
options.add(new PdfString("4.30 am - 8.30 am"));
PdfArray arr = new PdfArray(options);
fields.get("shift").setOptions(arr);

iText 7目前仅适用于Java; C#版本将在2周内发布。

iText 7 currently only exists for Java; the C# version will be released in 2 weeks.

如果您使用的是iText 5,那么您需要咨询 iText in Action的第8章 - 第二版,更具体地说是 ChoiceFields 示例:

If you are using iText 5, then you need to consult Chapter 8 of iText in Action - Second Edition, more specifically at the ChoiceFields example:

AcroFields form = stamper.getAcroFields();
form.setField("choice_1", "NL");
form.setListSelection("choice_2", new String[]{"German", "Spanish"});
String[] languages = form.getListOptionDisplay("choice_3");
String[] exportvalues = form.getListOptionExport("choice_3");
int n = languages.length;
String[] new_languages = new String[n + 2];
String[] new_exportvalues = new String[n + 2];
for (int i = 0; i < n; i++) {
    new_languages[i] = languages[i];
    new_exportvalues[i] = exportvalues[i];
}
new_languages[n] = "Chinese";
new_exportvalues[n] = "CN";
new_languages[n + 1] = "Japanese";
new_exportvalues[n + 1] = "JP";
form.setListOption("choice_3", new_exportvalues, new_languages);
form.setField("choice_3", "CN");
form.setField("choice_4", "Japanese");

上面的代码是Java代码,但是如果你做的话,你也可以把它解释为伪代码我不想读Java。

The code above is Java code, but you could also interpret it as "pseudo code" if you don't want to read Java.

iText in Action一书中的所有例子都转换为C#。请参阅 ChoiceFields.cs 完整的iText 5示例。

All the examples from the iText in Action book are converted to C#. See ChoiceFields.cs for the full iText 5 example.

重要:我假设您已使用Acrobat创建表单,并且您的表单基于AcroForm技术。如果您使用LiveCycle创建了表单,则表示您拥有XFA表单。此答案中共享的代码不适用于XFA表单。

Important: I am assuming that you have created the form with Acrobat and that your form is based on AcroForm technology. If you have created your form with LiveCycle, you have an XFA form. The code shared in this answer will not work for XFA forms.

这篇关于使用c#代码将listchoices添加到Pdfform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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