Hi there,
Thanks for your inquiry. Please check the following code snippet; it will help you to get all fields of a PDF form. Moreover, please note that fields with the same name will have the same value.
Document doc = new Document(myDir + "TestFile.pdf");
// 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.
Hi there,
Sure, let’s 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");
// 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);
}
}
Hi there,
Thanks for your feedback. Please note your fields with the same name on the same horizontal line share some y coordinates and make 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");
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[]
{
(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,