2

How to I retrieve the page number of a Form Field using Aspose PDF? I'm trying to recreate the PDF form in HTML by placing an image of each page and overlaying each field using the page number, coordinates, and dimensions.

Here's my current code:

public static List<PdfFieldDisplayModel> GetFieldPlacements(Stream stream)
    {
        var fields = new List<PdfFieldDisplayModel>();
        var doc = new Document(stream);
        var pdfForm = new Aspose.Pdf.Facades.Form(stream);

        foreach (Field ff in doc.Form)
        {
            var txt = doc.Form[ff.Name] as TextBoxField;
            var f = new PdfFieldDisplayModel();
            f.PageNumber = ??????????????
            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;
    }
RichC
  • 7,829
  • 21
  • 85
  • 149
  • You're aware that a single AcroForm form field may have appearances on multiple pages? – mkl Jul 05 '15 at 23:39
  • Yes, I am. I would assume I could get the instance of the field too all with their own unique location: Page number, coordinates, and dimensions. – RichC Jul 05 '15 at 23:48

1 Answers1

2

My name is Nayyer and I am developer Evangelist at Aspose. In order to get page index for form field, please try using PageIndex property of Aspose.Pdf.InteractiveFeatures.Forms.Field instance.

[C#]

//open document
Document pdfDocument = new Document("c:/pdftest/SingleField_output.pdf","password");
//get values from all fields

foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in pdfDocument.Form)
{
    //get field value
    Console.WriteLine("PartialName : {0} ", formField.PartialName);
    Console.WriteLine("Value : {0} ", formField.Value);
    Console.WriteLine("Value : {0} ", formField.PageIndex);
}
codewarior
  • 441
  • 2
  • 9
  • When was this feature implemented? I must be using an older version because I don't have the PageIndex property. I believe I'm using v6.9. – RichC Jul 06 '15 at 12:55
  • I am afraid I might not be able to share the exact version in which this property was added. However I recommend you to please try using latest release of Aspose.Pdf for .NET 10.5.0 (http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/entry632625.aspx) as during my testing, I can see this property in this release version. – codewarior Jul 07 '15 at 09:46
  • Can the same be done with `TextFragment`s and while you add them in the document? E.g. by using `Document.ProcessParagraphs()` aftre the fragment insertion ? I try to do this, but the `txtFragment.Page.Number` has `Page=null`. – user2173353 Sep 22 '15 at 08:50
  • I see now that the `Document.Pages.Count` gets updated, but I am not sure if it is reliable enough... – user2173353 Sep 22 '15 at 09:26
  • Once you have called the ProcessParagraphs() method, you should get the accurate page count using Document.Pages.Count. However in order to get the page number for particular TextFragment, the document needs to be saved, so that the Fragment actually resides inside inside the document. – codewarior Nov 04 '15 at 09:15