Extract PDF form field names from a PDF form

前端 未结 6 1077
旧巷少年郎
旧巷少年郎 2020-12-29 05:24

I\'m using pdftk to fill in a PDF form with an XFDF file. However, for this project I do not know in advance what fields will be present, so I need to analyse the PDF itself

6条回答
  •  粉色の甜心
    2020-12-29 05:45

    I used the following code, using ABCpdf from WebSupergoo, but I imagine most libraries have comparable classes:

    protected void Button1_Click(object sender, EventArgs e)
        {
            Doc thedoc = new Doc();
            string saveFile = "~/docs/f1_filled.pdf";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            thedoc.Read(Server.MapPath("~/docs/F1_2010.pdf"));
            foreach (Field fld in thedoc.Form.Fields)
            {
                if (!(fld.Page == null))
                {
                    sb.AppendFormat("Field: {0}, Type: {1},page: {4},x: {2},y: {3}\n", fld.Name, fld.FieldType.ToString(), fld.Rect.Left, fld.Rect.Top, fld.Page.PageNumber);
                }
                else
                {
                    sb.AppendFormat("Field: {0}, Type: {1},page: {4},x: {2},y: {3}\n", fld.Name, fld.FieldType.ToString(), fld.Rect.Left, fld.Rect.Top, "None");
                }
                if (fld.FieldType == FieldType.Text)
                {
                    fld.Value = fld.Name;
                }
    
            }
    
            this.TextBox1.Text = sb.ToString();
            this.TextBox1.Visible = true;
            thedoc.Save(Server.MapPath(saveFile));
            Response.Redirect(saveFile);
        }
    

    This does 2 things: 1) Populates a textbox with the inventory of all Form Fields, showing their name, fieldtype, and their page number and position on the page (0,0 is lower left, by the way). 2) Populates all the textfields with their field name in an output file - print the output file, and all of your text fields will be labelled.

提交回复
热议问题