How to get all fields when there might be multiple instances of a field?

How can I get the values, coordinates, and dimensions of all fields in a PDF file INCLUDING multiple instances of fields that have the same name? The code below errors out when I hit a field that is a second instance of a field.

public List GetFieldPlacements(string docPath)
{
var fields = new List();
var doc = new Document(docPath);
var pdfForm = new Aspose.Pdf.Facades.Form(docPath);

foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in doc.Form)
{
//get field value
Console.WriteLine("PartialName : {0} ", formField.PartialName);
Console.WriteLine("Value : {0} ", formField.Value);
Console.WriteLine("PageNumber : {0} ", formField.PageIndex);
}

foreach (Field ff in doc.Form)
{
var txt = doc.Form[ff.Name] as TextBoxField;
var f = new PdfFieldDisplayModel();
f.Name = ff.Name;
f.PartialName = ff.PartialName;
f.Value = ff.Value;
f.Width = txt.Rect.Width;
f.Height = txt.Rect.Height;
f.Left = txt.Rect.LLX;
f.Bottom = txt.Rect.LLY;
fields.Add(f);
}
return fields;
}

Hi there,


Thanks for your inquiry. Please check following code snippet, it will help you to get all fields of PDF form. Moreover, please note the fields with same name will have same value.

Document doc = new
Document(myDir + “TestFile.pdf”);<o:p></o:p>

// Get values from all fields

foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in doc.Form.Fields)

{

if (formField.Count > 1)

{

foreach (Aspose.Pdf.InteractiveFeatures.Annotations.Annotation annot in formField)

{

System.Console.WriteLine(annot.FullName);

System.Console.WriteLine(annot.Rect);

}

}

else

{

System.Console.WriteLine(formField.FullName);

System.Console.WriteLine(formField.Rect);

}

}


Best Regards,

Using your code sample I changed mine to the below but each of the annotations that it loops through all have the exact same properties and what I’ve also noticed is that the coordinates are the combined coordinates of all the fields with the same name. So, that means if I have one small text field named “TestField1” at the top right of the page and another small text field named “TestField1” at the bottom left of the page, the reported coordinates provided are one giant field that covers the entire page. I’m not able to get the unique position and pageIndex for each duplicate field.


public List GetFields(Document doc)
{
var fields = new List();
foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field field in doc.Form.Fields)
{
if (field.Count > 1)
{
foreach (Aspose.Pdf.InteractiveFeatures.Annotations.Annotation annot in field)
{
fields.Add(GetFieldProperties(field));
}
}
else
{
fields.Add(GetFieldProperties(field));
}
}
return fields;
}

public PdfFieldDisplayModel GetFieldProperties(Field field)
{
var f = new PdfFieldDisplayModel();
//var txt = doc.Form[field.PartialName] as TextBoxField;
f.Name = field.FullName;
f.PartialName = field.PartialName;
f.Value = field.PartialName;
f.PageNumber = field.PageIndex;
f.Width = field.Rect.Width * dpi;
f.Height = field.Rect.Height * dpi;
f.Left = field.Rect.LLX * dpi;
f.Bottom = field.Rect.LLY * dpi;

return f;
}



Hi there,


We are sorry for the delayed response. I am afraid I am unable to test your code, we will appreciate it if you please share a sample console project to replicate the issue at our end, so we will test the scenario and provide you information accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Sure well, let’s just use your own code you provided against the TestFile.pdf file in my initial post. If you run your own code and list out all of the fields with their coordinates, you’ll see that there are two fields with the same name that also have the exact same Rect properties when they should not because they are in different locations on the page. Also, what I noticed is that the Rect properties are combined into one giant Rect covering both field locations.


Document doc = new Document(myDir + “TestFile.pdf”);<o:p></o:p>

// Get values from all fields<o:p></o:p>

foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in doc.Form.Fields)<o:p></o:p>

{<o:p></o:p>

if (formField.Count > 1)<o:p></o:p>

{<o:p></o:p>

foreach (Aspose.Pdf.InteractiveFeatures.Annotations.Annotation annot in formField)<o:p></o:p>

{<o:p></o:p>

System.Console.WriteLine(annot.FullName);<o:p></o:p>

System.Console.WriteLine(annot.Rect);<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>

System.Console.WriteLine(formField.FullName);<o:p></o:p>

System.Console.WriteLine(formField.Rect);<o:p></o:p>

}<o:p></o:p>

}

Hi there,


Thanks for your feedback. Please note your fields with same name on same horizontal line so sharing some y coordinates and making confusion. I have made a slight change in code and draw rectangles on the fields coordinate. Hopefully it will remove the confusion.

Document doc = new
Document(myDir + “TestFile.pdf”);<o:p></o:p>

var editor = new PdfContentEditor(doc);

// Get values from all fields

foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in doc.Form.Fields)

{

if (formField.Count > 1)

{

foreach (Aspose.Pdf.InteractiveFeatures.Annotations.Annotation annot in formField)

{

System.Console.WriteLine(annot.FullName);

System.Console.WriteLine(annot.Rect);

DrawBox(editor, 1, annot.Rect, System.Drawing.Color.Red);

}

}

else

{

System.Console.WriteLine(formField.FullName);

System.Console.WriteLine(formField.Rect);

DrawBox(editor, 1, formField.Rect, System.Drawing.Color.Red);

}

}

doc.Save(myDir+"TestFile_out.pdf");

private static void DrawBox(PdfContentEditor editor, int page, Aspose.Pdf.Rectangle rect, System.Drawing.Color color)

{

var lineInfo = new LineInfo();

lineInfo.VerticeCoordinate = new[] {

(float)rect.LLX, (float)rect.LLY,

(float)rect.LLX, (float)rect.URY,

(float)rect.URX, (float)rect.URY,

(float)rect.URX, (float)rect.LLY

};

lineInfo.Visibility = true;

lineInfo.LineColor = color;

editor.CreatePolygon(lineInfo, page, new System.Drawing.Rectangle(0, 0, 0, 0), null);

}

Please feel free to contact us for any further assistance.


Best Regards,

Ah - that helped. I wasn’t passing my “annot” into my function when there were more than one field. I was accidentally passing in the field in either condition result. It’s working for me now. Thanks for all your assistance!

Hi there,


Thanks for your feedback. It is good to know that you have managed to resolve the issue.

Please keep using our API and feel free to contact us for any question or concern, we will be more than happy to extend our support.

Best Regards,