I using Spire.PDF to read all fields from a *.pdf file, for .NET application.
I faced with a problem when determine the page for the PdfTextBoxFieldWidget which have the same name, and it belong to multiple page on the document.
Example: On page 2 of the document, it contains a textbox with named is "Month".
On page 4, it also contains a textbox with named is "Month".
And so on for another pages.
I traverse PdfFormWidget.FieldsWidget.List of the document. And see that all PdfTextBoxFieldWidget which have the same name just loaded all into a PdfField.WidgetItems.List (PdfField = PdfFormWidget.FieldsWidget.List[index]).
Each item on PdfField.WidgetItems.List is PdfTexBoxWidgetItem. And I using this method(doc.Pages.IndexOf(item page)) to get the page of each item in this list PdfField.WidgetItems.List. But the "IndexOf" method always return the page of first item in PdfField.WidgetItems.List list; even if the item is on another page, it always return the first page.
Could you help me to find out any solution for this problem.
It is my code snippet.
This line: fieldDTO.Page = doc.Pages.IndexOf(tmpTextBox.Page) + 1; // It always return the page of widgetItems[0]
- Code: Select all
var doc = new PdfDocument();
doc.LoadFromBytes(System.IO.File.ReadAllBytes(@"\file.pdf"));
var formWidget = doc.Form as PdfFormWidget;
//List<PDFFormField> lstFieldPDF = new List<PDFFormField>();
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
PDFFormField fieldDTO = new PDFFormField();
PdfField fieldPDF = formWidget.FieldsWidget.List[i] as PdfField;
if (fieldPDF is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textBoxField = fieldPDF as PdfTextBoxFieldWidget;
if (textBoxField.WidgetItems != null && textBoxField.WidgetItems.List != null && textBoxField.WidgetItems.List.Count > 0)
{
var widgetItems = textBoxField.WidgetItems.List;
for (int j = 0; j < widgetItems.Count; j++)
{
fieldDTO.FieldPDFName = textBoxField.Name + " " + j;
var tmpTextBox = (widgetItems[j] as PdfTexBoxWidgetItem);
fieldDTO.Page = doc.Pages.IndexOf(tmpTextBox.Page) + 1; // It always return the page of widgetItems[0]
//lstFieldPDF.Add(new PDFFormField(fieldDTO));
}
}
}
}
Updated: so sorry for "fieldDTO", please comment out what related to this variable, and just focus on this doc.Pages.IndexOf(tmpTextBox.Page) + 1; to detect the issue. Many thanks