如何获取XFA表单中的字段列表? [英] How to get a list of the fields in an XFA form?
问题描述
我正在尝试获取XFA表单中所有字段的简单列表。我正在使用此代码:
private void ListFieldNames()
{
string pdfTemplate = @C :\Projects\iTextSharp\SReport.pdf;
MemoryStream m = new MemoryStream();
//标题形式
this.Text + = - + pdfTemplate;
//基于PDF模板文档创建一个新的PDF阅读器
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader,m);
AcroFields formFields = pdfStamper.AcroFields;
AcroFields form = pdfReader.AcroFields;
XfaForm xfa = form.Xfa;
StringBuilder sb = new StringBuilder();
sb.Append(xfa.XfaPresent?XFA form:AcroForm);
sb.Append(Environment.NewLine);
foreach(form.Fields.Keys中的字符串键)
{
sb.Append(key);
sb.Append(Environment.NewLine);
txtFields.Text = sb.ToString();
}
txtFields.Text = sb.ToString();
}
但我得到的只是XFA表格,而不是任何字段。知道我做错了什么吗?
提前致谢
您已经采用了代码示例从我的书iText in Action的第8章开始。该代码示例的结果与我在第273页上所写的内容一致:
使用此运行清单8.18 form as resource将给你以下结果:
AcroForm
如果你的问题是我知道我做错了吗?那么答案很简单:你停止阅读页面270,或者您使用了代码示例而未阅读随附的文档。如何解决这个问题?阅读文档!
如果您的问题是为什么我没有获得有关这些字段的任何信息?(这不是您的问题,但我们假设它是),答案是:您使用代码来检索AcroForm字段,但您的表单不包含任何此类字段。您的表单是纯XFA表单,这意味着所有字段信息仅存储为XML和XML!
假设您现在想知道:怎么能我提取了那些XML?然后你应该去你找到你复制/粘贴的例子的地方。
这可能在这里:
http://itextpdf.com/examples/iia.php?id=164
甚至在这里: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter08&ex=XfaMovie
此代码段将返回完整的XFA流:
public string ReadXfa(PdfReader reader){
XfaForm xfa = new XfaForm(读取器);
XmlDocument doc = xfa.DomDocument;
reader.Close();
if(!string.IsNullOrEmpty(doc.DocumentElement.NamespaceURI)){
doc.DocumentElement.SetAttribute(xmlns,);
XmlDocument new_doc = new XmlDocument();
new_doc.LoadXml(doc.OuterXml);
doc = new_doc;
}
var sb = new StringBuilder(4000);
var Xsettings = new XmlWriterSettings(){Indent = true};
using(var writer = XmlWriter.Create(sb,Xsettings)){
doc.WriteTo(writer);
}
返回sb.ToString();
}
现在查找< xfa:datasets>
标签;它将有一个子标签< xfa:data>
(如果表格为空,则可能为空)和子标签< dd:dataDescription>
。在 dataDescription
标记内,您会发现类似于XSD的内容。这就是你需要知道表格中的字段是什么。
我可以继续猜测问题,例如:我如何填写这样的form?使用方法 fillXfaForm()
; 如何展平这样的表单?通过使用XFA Worker(这是一个在iTextSharp上编写的封闭源库),但让我们将这些问题留给另一个线程; - )
I am trying to get a simple list of all the fields in my XFA form. I am using this code:
private void ListFieldNames()
{
string pdfTemplate = @"C:\Projects\iTextSharp\SReport.pdf";
MemoryStream m = new MemoryStream();
// title the form
this.Text += " - " + pdfTemplate;
// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, m);
AcroFields formFields = pdfStamper.AcroFields;
AcroFields form = pdfReader.AcroFields;
XfaForm xfa = form.Xfa;
StringBuilder sb = new StringBuilder();
sb.Append(xfa.XfaPresent ? "XFA form" : "AcroForm");
sb.Append(Environment.NewLine);
foreach (string key in form.Fields.Keys)
{
sb.Append(key);
sb.Append(Environment.NewLine);
txtFields.Text = sb.ToString();
}
txtFields.Text = sb.ToString();
}
But all I am getting is the XFA Form and not any fields. Any idea what I am doing wrong?
Thanks in advance
You've taken a code sample from chapter 8 of my book "iText in Action." The result of that code sample is consistent with what I wrote on page 273:
Running Listing 8.18 with this form as resource will give you the following result:
AcroForm
If your question is Any idea what I am doing wrong? then the answer is simple: you stopped reading on page 270, or you used a code sample without reading the accompanying documentation. How to fix this? Read the documentation!
If your question is Why don't I get any info about the fields? (which isn't your question, but let's assume it is), the answer is: you're using code to retrieve AcroForm fields, but your form doesn't contain any such fields. Your form is a pure XFA form, which means that all field information is stored as XML and XML only!
Suppose that you now want to know: How can I extract that XML? then you should go to the place where you found the example you copy/pasted.
That could be here: http://itextpdf.com/examples/iia.php?id=164
Or maybe here: http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter08/XfaMovie.cs
Or even here: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter08&ex=XfaMovie
This code snippet will return the complete XFA stream:
public string ReadXfa(PdfReader reader) {
XfaForm xfa = new XfaForm(reader);
XmlDocument doc = xfa.DomDocument;
reader.Close();
if (!string.IsNullOrEmpty(doc.DocumentElement.NamespaceURI)) {
doc.DocumentElement.SetAttribute("xmlns", "");
XmlDocument new_doc = new XmlDocument();
new_doc.LoadXml(doc.OuterXml);
doc = new_doc;
}
var sb = new StringBuilder(4000);
var Xsettings = new XmlWriterSettings() {Indent = true};
using (var writer = XmlWriter.Create(sb, Xsettings)) {
doc.WriteTo(writer);
}
return sb.ToString();
}
Now look for the <xfa:datasets>
tag; it will have a subtag <xfa:data>
(probably empty if the form is empty) and a subtag <dd:dataDescription>
. Inside the dataDescription
tag, you'll find something that resembles XSD. That's what you need to know what the fields in the form are about.
I could go on guessing questions, such as: How do I fill out such a form? By using the method fillXfaForm()
; How can I flatten such a form? By using XFA Worker (which is a closed source library written on top of iTextSharp), but let's keep those questions for another thread ;-)
这篇关于如何获取XFA表单中的字段列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!