itextpdf将带复选框的html_使用iText Sharp阅读复选框,单选按钮的名称和值PDF格式...

该博客详细介绍了如何使用C#和iTextSharp库在PDF文档中区分单选按钮、复选框和普通按钮。通过解析PDF字段的Widget,并检查其/Ft和/Ff标志来判断字段类型。示例代码展示了如何遍历AcroFields并检查每个字段的属性,从而确定其属于哪一类按钮。
摘要由CSDN通过智能技术生成

单选按钮,复选框和按钮都实际上是同一类型的字段,但与设置不同的标志。你可以看到国旗在PDF规格部分12.7.4.2.1表226

int ffRadio = 1 << 15; //Per spec, 16th bit is radio button

int ffPushbutton = 1 << 16; //17th bit is push button

对于要获得与此相关的Widget个给出Field。通常这只是一个,但可以更多,所以你应该解释这一点。

PdfDictionary w = f.Value.GetWidget(0);

键字段将具有设置为/Btn他们的字段类型(/Ft),以便检查该

if (!w.Contains(PdfName.FT) || !w.Get(PdfName.FT).Equals(PdfName.BTN)) {continue;/*Skipping non-buttons*/ }

对于当前Widget获得可选字段标志(/Ff)值或使用零,如果它不不存在。

int ff = (w.Contains(PdfName.FF) ? w.GetAsNumber(PdfName.FF).IntValue : 0);

然后,只需一些简单的数学:

if ((ff & ffRadio) == ffRadio) {

//Is Radio

} else if (((ff & ffRadio) != ffRadio) && ((ff & ffPushbutton) != ffPushbutton)) {

//Is Checkbox

} else {

//Regular button

}

下面是一个完整的工作C#WinForm的2011应用定位iTextSharp的5.2.0,显示了上述所有寻找一个文件名为Test.pdf生活费你的桌面。只需在条件中添加一些逻辑即可处理每种按钮类型。

using System;

using System.IO;

using System.Windows.Forms;

using iTextSharp.text.pdf;

namespace WindowsFormsApplication3 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e) {

var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

PdfReader reader = new PdfReader(testFile);

var fields = reader.AcroFields;

int ffRadio = 1 << 15; //Per spec, 16th bit is radio button

int ffPushbutton = 1 << 16; //17th bit is push button

int ff;

//Loop through each field

foreach (var f in fields.Fields) {

//Get the widgets for the field (note, this could return more than 1, this should be checked)

PdfDictionary w = f.Value.GetWidget(0);

//See if it is a button-like object (/Ft == /Btn)

if (!w.Contains(PdfName.FT) || !w.Get(PdfName.FT).Equals(PdfName.BTN)) { continue;/*Skipping non-buttons*/ }

//Get the optional field flags, if they don't exist then just use zero

ff = (w.Contains(PdfName.FF) ? w.GetAsNumber(PdfName.FF).IntValue : 0);

if ((ff & ffRadio) == ffRadio) {

//Is Radio

} else if (((ff & ffRadio) != ffRadio) && ((ff & ffPushbutton) != ffPushbutton)) {

//Is Checkbox

} else {

//Regular button

}

}

this.Close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值