ItextSharp - 使用C#自动填写pdf表单 - 复选框问题 [英] ItextSharp - AutoFill a pdf form using C# - Issues with Checkboxes

查看:209
本文介绍了ItextSharp - 使用C#自动填写pdf表单 - 复选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填写使用Acrobat pro创建的pdf表单,其中包含iTextSharp& C#,并且在尝试勾选一个复选框字段时发现自己卡住了。



我让它适用于单选按钮和文本框,但似乎无法使复选框工作。
我还确认了这个案例中的复选框名称Q7b在acrobat文档中是正确的,并且可以使用以下代码在表单上找到它

 私有字符串getfieldnames(AcroFields字段)
{
StringBuilder sb = new StringBuilder();

foreach(fields.Fields.Keys中的字符串键)
{

sb.Append(key + Environment.NewLine);
}
返回sb.ToString();

}

我用来更新复选框的代码位于<使用(MemoryStream pdfFlat = new MemoryStream())
{

PdfReader pdfReader = new PdfReader(strPath的);
PdfStamper pdfStamp = new PdfStamper(pdfReader,pdfFlat);
AcroFields fields = pdfStamp.AcroFields;

// textfields
fields.SetField(Initiating_Doctor,Doctor A);
fields.SetField(专业,外科医生);

// Radiobuttons
fields.SetField(PRELIM_Q1,是);
fields.SetField(PRELIM_Q2,No);
fields.SetField(PRELIM_Q3,No);
fields.SetField(PRELIM_Q4,No);

//复选框 - 设置复选框以选中,但这不起作用。
fields.SetField(Q7b,是);

pdfReader.Close();
pdfStamp.FormFlattening = true;
pdfStamp.FreeTextFlattening = true;
pdfStamp.Writer.CloseStream = false;
pdfStamp.Close();
}

任何帮助都将不胜感激。



Brad

解决方案

将字段值设置为复选框的导出值会导致它被检查。因此,如果导出值为是,则将字段值设置为是将勾选复选框。如果导出值是其他值(例如开),则需要将字段值设置为该值以便勾选该框。


I am in the process of filling in a pdf form created using Acrobat pro with iTextSharp & C#, and have found myself stuck when attempting to tick a checkbox field.

I have it working for radio buttons and textboxes but cannot seem to get the checkbox working. I have also confirmed the checkbox name in this case "Q7b" is correct in the acrobat document and can find it on the form using the following code

private string getfieldnames(AcroFields fields)
{
    StringBuilder sb = new StringBuilder();

    foreach (string key in fields.Fields.Keys)
    {

        sb.Append(key + Environment.NewLine);
    }
    return sb.ToString();

}

The code I am using to update the checkbox is below

using (MemoryStream pdfFlat = new MemoryStream())
{

    PdfReader pdfReader = new PdfReader(strPath);
    PdfStamper pdfStamp = new PdfStamper(pdfReader, pdfFlat);
    AcroFields fields = pdfStamp.AcroFields;

    //textfields
    fields.SetField("Initiating_Doctor", "Doctor A");
    fields.SetField("Speciality", "Surgeon");

    //Radiobuttons
    fields.SetField("PRELIM_Q1", "Yes");
    fields.SetField("PRELIM_Q2", "No");
    fields.SetField("PRELIM_Q3", "No");
    fields.SetField("PRELIM_Q4", "No");

    //checkbox - Set the checkbox to checked but this does not work.
    fields.SetField("Q7b", "Yes");

    pdfReader.Close();
    pdfStamp.FormFlattening = true;
    pdfStamp.FreeTextFlattening = true;
    pdfStamp.Writer.CloseStream = false;
    pdfStamp.Close();
}

Any help would be greatly appreciated.

Brad

解决方案

Setting the field value to the export value of the checkbox will cause it to be checked. So if the export value is "Yes", then setting the value of the field to "Yes" will tick the checkbox. If the export value is something else (e.g. "On"), you will need to set the field value to that in order to tick the box.

这篇关于ItextSharp - 使用C#自动填写pdf表单 - 复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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