c# - 使用 iTextSharp 获取复选框的导出值

标签 c# asp.net pdf itextsharp

我正在使用 ITextSharp 动态填充 pdf 文档中的字段。我希望能够确定复选框的“导出值”来自代码隐藏,以便确定在应该选中时发送到该复选框的值。我过去使用过的大多数文档的每个复选框都具有相同的导出值,但我目前使用的文档因复选框而异。我可以遍历所有文本框并使它们保持一致,但如果我可以在运行时确定这些复选框的导出值并相应地设置它们,将来会节省很多时间。

提前致谢!

我尝试在 C# 中实现下面的解决方案并最终得到以下代码:

 public string GetCheckBoxExportValue(AcroFields pdfDocument, string checkBoxFieldName)
    {
        AcroFields.Item item = pdfDocument.GetFieldItem(checkBoxFieldName);
        if (item.values.Count > 0)
        {
            PdfDictionary valueDict = item.GetValue(0);

            PdfDictionary appearanceDict = valueDict.GetAsDict(PdfName.AP);

            // if there's an appearance dict at all, one key will be "Off", and the other
            // will be the export value... there should only be two.
            if (appearanceDict != null)
            {


                foreach (PdfName curKey in appearanceDict.Keys)
                {
                    if (!PdfName.OFF.Equals(curKey))
                    {
                        return curKey.ToString(); // string will have a leading '/' character
                    }
                }
            }

            // if that doesn't work, there might be an /AS key, whose value is a name with 
            // the export value, again with a leading '/'
            PdfName curVal = valueDict.GetAsName(PdfName.AS);
            if (curVal != null)
            {
                return curVal.ToString();
            }

        }
        //return null if you get this far
            return null;

    }

这只会每次都返回“/D”。我不确定 C# 中的方法是否需要不同,或者我是否只是遗漏了一些东西。

最佳答案

好的,您需要检查低级 PDF 对象的适当值。您可以在 PDF Reference 中查找所述值(第 12 章:交互功能,第 7 节:交互表单)。

特别是(在 Java 中):

AcroFields.Item item = acroFields.getFieldItem(fldName);
PdfDictionary valueDict = item.getValue(0);

PdfDictionary appearanceDict = valueDict .getAsDict(PdfName.AP);

if (appearanceDict != null) {
  PdfDictionary normalAppearances = appearanceDict.getAsDict(PdfName.N);
  // /D is for the "down" appearances.

  // if there are normal appearances, one key will be "Off", and the other
  // will be the export value... there should only be two.
  if (normalAppearances != null) {
    Set<PdfName> keys = normalAppearances .getKeys();
    for (PdfName curKey : keys) {
      if (!PdfName.OFF.equals(curKey)) {
        return curKey.toString(); // string will have a leading '/' character
      }
    }
  }


}
// if that doesn't work, there might be an /AS key, whose value is a name with 
// the export value, again with a leading '/'
PdfName curVal = valueDict.getAsName(PdfName.AS);
if (curVal != null) {
  return curVal.toString();
}

类似的东西。通常的“我刚刚在这里的编辑框中写了这个”的规定适用,但这应该很好。我编写了大量令人苦恼的低级 iText 代码。

关于c# - 使用 iTextSharp 获取复选框的导出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491156/

相关文章:

c# - 从 xml 文件中删除除根元素 c# 之外的所有元素

c# - 将 TextBox.Text 绑定(bind)到 DataSet.DataSetName

jquery - 使用 Jquery 将选项组添加到选项列表

linux - 使用 shell 在 pdf 中查找字符串

c# - 如何 : Improve the PDF- quality before OCR using C#

c# - 在 Crystal Reports 中动态更改数据库类型、源等(适用于 Visual Studio 2012)

c# - 添加相对于 TreeView 选定节点的新节点

asp.net - 在 IHttpAsyncHandler 中使用 Task 或 async/await

javascript - 在依赖 MasterPage 的 aspx 页面中,在哪里引用 javascript 文件?

pdf - 如何运行imagemagick将多个PDF文件仅第一页批量转换为JPEG?