Ben*_*Ben 8 c# pdf asp.net 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;
}
Run Code Online (Sandbox Code Playgroud)
这只是每次返回"/ D".我不确定C#中的方法是否需要不同,或者我是否只是缺少某些东西.
Mar*_*rer 12
好的,您需要检查低级PDF对象以获取适当的值.您可以在PDF参考中查找所述值(第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();
}
Run Code Online (Sandbox Code Playgroud)
这样的事情.通常的"我刚刚在编辑框中写了这个"条款适用,但这应该是好的.我写了一个令人沮丧的大量低级iText代码.
小智 5
我找到设置复选框的最佳方法是:
void SetCB(AcroFields fields, string F)
{
try
{
fields.SetField(F, fields.GetFieldItem(F).GetValue(0).GetAsDict(PdfName.AP).GetAsDict(PdfName.N).Keys.Single().ToString().TrimStart('/'));
} catch { }
}
Run Code Online (Sandbox Code Playgroud)
错误:序列包含多个元素
例如:
PdfReader reader = new PdfReader("c:\\qqq\\fl100Y2.pdf");// formFile);
using (PdfStamper stamper = new PdfStamper(reader, new FileStream("c:\\qqq\\fl100Ynew.pdf", FileMode.Create)))
{
AcroFields fields = stamper.AcroFields;
bool set = fields.SetFieldProperty("FillText156", "textsize", 10.0f, null);
SetCB(fields, "CheckBox24");
SetCB(fields, "CheckBox24by");
fields.SetField("FillText156", "John Doe");
// flatten form fields and close document
stamper.FormFlattening = true;
stamper.Close();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11312 次 |
最近记录: |