前面我们介绍了如何使用spire.pdf 及 ,该文将详细介绍如何使用spire.pdf读取pdf表单域及获取域里面的值。
读取pdf表单域
c#
//创建pdfdocument实例并加载示例文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
//遍历fieldswidget.list获取所有表单域,并打印出表单域名称
pdfformwidget formwidget = pdf.form as pdfformwidget;
for (int i = 0; i < formwidget.fieldswidget.list.count; i )
{
pdffield field = formwidget.fieldswidget.list[i] as pdffield;
string fieldname = field.name;
console.writeline(fieldname);
}
console.writeline("--------------------------------------");
//遍历fieldswidget.list获取所有表单域,并打印出导出value为yes的表单域名称
list list = formwidget.fieldswidget.getfieldsbyexportvalue("yes");
for (int i = 0; i < list.count; i )
{
string fieldname = list[i].name;
console.writeline(fieldname);
}
console.read();
vb.net
'创建pdfdocument实例并加载示例文档
dim pdf as new pdfdocument()
pdf.loadfromfile("sample.pdf")
'遍历fieldswidget.list获取所有表单域,并打印出表单域名称
dim formwidget as pdfformwidget = trycast(pdf.form, pdfformwidget)
for i as integer = 0 to formwidget.fieldswidget.list.count - 1
dim field as pdffield = trycast(formwidget.fieldswidget.list(i), pdffield)
dim fieldname as string = field.name
console.writeline(fieldname)
next
console.writeline("--------------------------------------")
'遍历fieldswidget.list获取所有表单域,并打印出导出value为yes的表单域名称
dim list as list(of pdffield) = formwidget.fieldswidget.getfieldsbyexportvalue("yes")
for i as integer = 0 to list.count - 1
dim fieldname as string = list(i).name
console.writeline(fieldname)
next
console.read()
读取域里面选择的值:
c#
//创建pdfdocument实例并加载示例文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
//获取pdf文档第一页
pdfpagebase page = pdf.pages[0];
//遍历fieldswidget.list获取所有表单域
pdfformwidget formwidget = pdf.form as pdfformwidget;
for (int i = 0; i < formwidget.fieldswidget.list.count; i )
{
pdffield field = formwidget.fieldswidget.list[i] as pdffield;
//获取单选钮表单域并输出所选择的按钮值
if (field is pdfradiobuttonlistfieldwidget)
{
pdfradiobuttonlistfieldwidget radiobutton = field as pdfradiobuttonlistfieldwidget;
foreach (pdfradiobuttonwidgetitem item in radiobutton.widgetitems)
{
if (item.selected)
{
console.writeline(item.value);
}
}
}
}
console.readkey();
vb.net
'创建pdfdocument实例并加载示例文档
dim pdf as new pdfdocument()
pdf.loadfromfile("sample.pdf")
'获取pdf文档第一页
dim page as pdfpagebase = pdf.pages(0)
'遍历fieldswidget.list获取所有表单域
dim formwidget as pdfformwidget = trycast(pdf.form, pdfformwidget)
for i as integer = 0 to formwidget.fieldswidget.list.count - 1
dim field as pdffield = trycast(formwidget.fieldswidget.list(i), pdffield)
'获取单选钮表单域并输出所选择的按钮值
if typeof field is pdfradiobuttonlistfieldwidget then
dim radiobutton as pdfradiobuttonlistfieldwidget = trycast(field, pdfradiobuttonlistfieldwidget)
for each item as pdfradiobuttonwidgetitem in radiobutton.widgetitems
if item.selected then
console.writeline(item.value)
end if
next
end if
next
console.readkey()