Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Problem with filling form (c#)

Help
2012-02-09
2013-01-26
  • David Lawler
    David Lawler
    2012-02-09

    The following code works well on the 1040ez form that you can download from the IRS (US), except that the second field
    (Last Name) does not display the value set until you click on the field.  I'm sure the problem has to do with the Appearance
    Stream not getting updated correctly…but I'm too new to the mysteries of 'PDF' to figure out the issue:

        public class Program
        {
            public static void Main(string args)
            {
                string src = "..\\..\\f1040ez.pdf";
                string dst = "..\\..\\f1040ez_altered.pdf";

                // 1. Opening the PDF file…
                File file = new File(src);
                Document document = file.Document;
               
                // 2. Get the acroform!
                Form form = document.Form;
                if(form == null)
                {
                    Console.WriteLine("\nNo acroform available (AcroForm dictionary not found).");
                }
                else
                {
                    Console.WriteLine("\nIterating through the fields collection…\n");
                   
                    // 3. Showing the acroform fields…
                    Dictionary<string,int> objCounters = new Dictionary<string,int>();
                    foreach(Field field in form.Fields.Values)
                    {
                        Console.WriteLine("* Field '" + field.FullName + "' (" + field.BaseObject + ")");
                       
                        string typeName = field.GetType().Name;
                        Console.WriteLine("    Type: " + typeName);
                        Console.WriteLine("    Value: " + field.Value);
                        Console.WriteLine("    Data: " + field.BaseDataObject.ToString());
                       
                        int widgetIndex = 0;
                        foreach(Widget widget in field.Widgets)
                        {
                            Console.WriteLine("    Widget " + (++widgetIndex) + ":");
                            Page widgetPage = widget.Page;
                            Console.WriteLine("      Page: " + (widgetPage == null ? "undefined" : (widgetPage.Index + 1) + " (" + widgetPage.BaseObject + ")"));
                           
                            RectangleF widgetBox = widget.Box;
                            Console.WriteLine("      Coordinates: {x:" + Math.Round(widgetBox.X) + "; y:" + Math.Round(widgetBox.Y) + "; width:" + Math.Round(widgetBox.Width) + "; height:" + Math.Round(widgetBox.Height) + "}");
                            // Console.ReadKey();
                        }
                       
                        objCounters = (objCounters.ContainsKey(typeName) ? objCounters : 0) + 1;
                    }
                   
                    int fieldCount = form.Fields.Count;
                    if(fieldCount == 0)
                    {
                        Console.WriteLine("No field available.");
                    }
                    else
                    {
                        Console.WriteLine("\nFields partial counts (grouped by type):");
                        foreach(KeyValuePair<string,int> entry in objCounters)
                        {
                            Console.WriteLine(" " + entry.Key + ": " + entry.Value);
                        }
                        Console.WriteLine("Fields total count: " + fieldCount);
                    }
                }

                // Change two fields
               
                form.Fields["topmostSubform.Page1.Entity.p1-t4"].Value = "Firstname";
                form.Fields["topmostSubform.Page1.Entity.p1-t5"].Value = "Lastname";

                // 4. Serialize the PDF file!

                SerializationModeEnum serializationMode = SerializationModeEnum.Incremental;
                try
                {
                    file.Save(dst, serializationMode);
                }
                catch(Exception e)
                {
                    Console.WriteLine("File writing failed: " + e.Message);
                    Console.WriteLine(e.StackTrace);
                }

                Console.WriteLine("done");
                Console.ReadKey();
    }

    Please let me know if there is anything I can do, debugging-wise, to help resolve this problem.  It's odd that the First
    Name field is displayed properly!

    Thanks,

    David

     
  • I have fixed it on 0.1.1 Fix branch and trunk of SVN repo.

    thank you
    Stefano