获取使用iTextSharp的复选框的出口值 [英] Get the export value of a checkbox using iTextSharp

查看:141
本文介绍了获取使用iTextSharp的复选框的出口值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作动态上填写使用iTextSharp的pdf文件中的字段。我希望能够确定的出口创汇的复选框从codebehind以确定送什么价值,此复选框是否应该进行检查。最让我已经在过去使用过的文件。假如每次复选框相同的出口值,但一个我目前正与从复选框复选框变化。我可以通过所有的文本框,使它们一致的,但它可以节省大量的时间,将来如果我能确定这些复选框的出口值是在运行时,并相应设置。

I'm working on dynamically filling in the fields on a pdf document using ITextSharp. I'd like to be able to determine the "export value" of the checkbox is from the codebehind in order to determine what value to send to this checkbox if it should be checked. Most of the documents I've worked with in the past had the same export value for every check box but the one I'm currently working with varies from checkbox to checkbox. I could go through all of the text boxes and make them consistent but it would save a lot of time in the future if I could just determine what the export value of these checkboxes are at runtime and set them accordingly.

在此先感谢!

我试图实现在C#下面的解决方案,并结束了与以下code:

I tried to implement the solution below in C# and ended up with the following code:

 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#或如果我只是缺少一些不同的东西。

This just returns "/D" every single time. I'm not sure if the approach needs to be different in C# or if I'm just missing something.

推荐答案

好吧,你需要检查低级PDF对象为适当的值。你可以看一下说,在 PDF参考(章值12:互动功能,一节7:交互式表单)

Okay, you need to check the low-level PDF objects for the appropriate values. You can look up said values in the PDF Reference (chapter 12: Interactive Features, section 7: Interactive Forms).

在特定(和Java中):

In particular (and in 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 code的。

Something like that. The usual "I just wrote this in the edit box here" provisions apply, but that should be good to go. I write a distressingly large amount of low level iText code.

这篇关于获取使用iTextSharp的复选框的出口值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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