PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Oct 15, 2019 6:09 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Aug 22, 2018 8:38 pm 
Offline

Joined: Wed Aug 22, 2018 8:30 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 22, 2018 8:46 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 481
Location: CCAA
Hi!

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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2018 3:32 am 
Offline

Joined: Wed Aug 22, 2018 8:30 pm
Posts: 3
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 1717 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2018 4:32 am 
Offline

Joined: Wed Aug 22, 2018 8:30 pm
Posts: 3
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");


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 371 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group