PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

PdfSharp / .NET Core: Updating a text field in a form.
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3831
Page 1 of 1

Author:  goosh [ Wed Aug 22, 2018 8:38 pm ]
Post subject:  PdfSharp / .NET Core: Updating a text field in a form.

Hi there -

I have spent the day working through getting a simple .NET Core application setup with pdfsharp. The goal of the application is to simply open a PDF document that contains text boxes and populate those text boxes with specific values.

I've worked through a number of blockers, including:
1) Installing Microsoft.Windows.Compatibility pack (to provide System.Drawing support)
2) Installing the System.Text.Encoding.CodePages to get the proper Encoding type available (1252).

Now, I'm able to open my PDF and navigate to a text field. I can see the objects of the Text Field are fully populated. However, when I try to write a new value the following error gets thrown:

System.NullReferenceException: Object reference not set to an instance of an object.at PdfSharp.Pdf.AcroForms.PdfTextField.RenderAppearance()

I'm not sure what I am missing, code below:

Code:
                // Open the file
                PdfDocument document = PdfReader.Open(@"C:\5.0\Provider\Templates\5.0.pdf", PdfDocumentOpenMode.Modify);

                // Get the root object of all interactive form fields
                PdfAcroForm form = document.AcroForm;


                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var enc1252 = Encoding.GetEncoding(1252);

                PdfTextField testField = (PdfTextField)(form.Fields["body"]);
                testField.Text  = "012345";


Thanks in advance!

Cheers,
Jason

Author:  TH-Soft [ Wed Aug 22, 2018 8:46 pm ]
Post subject:  Re: PdfSharp / .NET Core: Updating a text field in a form.

Hi!

Does your code work when used with the full .NET Framework, e.g. 4.7.1?

Author:  goosh [ Thu Aug 23, 2018 3:32 am ]
Post subject:  Re: PdfSharp / .NET Core: Updating a text field in a form.

Thanks for the reply. This evening I tried to create a simple console app in .NET 4.6.1 and seeing the same behavior. I'm a bit perplexed because I can see the form field object it just fails when I go to update the text. .NET console code below:

Code:
 static void Main(string[] args)
        {
            Console.WriteLine("Starting...");

            try
            {

                // Open the file
                PdfDocument document = PdfReader.Open(@"C:\Temp\PDFSharp\5.0.pdf", PdfDocumentOpenMode.Modify);

                // Get the root object of all interactive form fields
                PdfAcroForm form = document.AcroForm;

                PdfTextField testField = (PdfTextField)(form.Fields["body"]);
                testField.Text = "012345";

                document.Save(@"C:\Temp\PDFSharp\5.0-net.pdf");

                Console.Write("Document Saved");
            }
            catch (Exception ex)
            {
                Console.Write("Error: " + ex.Message);
            }

            Console.Read();
        }


Perhaps it's my template? I tried to attach but it's too large (900kb). However, I created via Adobe Acrobat as a new PDF with a single Text Box field (screen shot attached).

Attachments:
File comment: Screen shot of template.
2018-08-22_20-32-55.png
2018-08-22_20-32-55.png [ 48.72 KiB | Viewed 3559 times ]

Author:  goosh [ Thu Aug 23, 2018 4:32 am ]
Post subject:  Re: PdfSharp / .NET Core: Updating a text field in a form.

I figured it out - I need to use a PdfString rather than a normal c# string:

Code:
               // Open the file
                PdfDocument document = PdfReader.Open(@"C:\Temp\PDFSharp\5.0.pdf", PdfDocumentOpenMode.Modify);

                // Get the root object of all interactive form fields
                PdfAcroForm form = document.AcroForm;


                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var enc1252 = Encoding.GetEncoding(1252);

                PdfString test = new PdfString("012345-core");
                PdfTextField testField = (PdfTextField)(form.Fields["body"]);
                testField.Value = test;
                testField.ReadOnly = true;

                document.Save(@"C:\Temp\PDFSharp\5.0-core.pdf");

                Console.Write("Document Saved");

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/