PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDF Sharp modifying document by adding text
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2142
Page 1 of 1

Author:  josephj1989 [ Sat Sep 15, 2012 6:27 pm ]
Post subject:  PDF Sharp modifying document by adding text

Hi All

I searched through the FAQS and features list and I cannot find a solution to my problem - may be because I know very little about PDFs.

I am doing a project for a small client using C#, ASP.NET MVC3 and here you have to dynamically modify PDF documents (can be called template) by inserting text at specific positions within specific pages.

In another project I was using HTMLTOPDF to write HTML to the document. Can PDF sharp accomplish this ?

Maybe if I use PDF forms as "templates" then I can put values into the fields (if the templates version is less than 5?)

Not sure what approach is best here-could you enlighten me ?

Thanks
John

Author:  davidh [ Tue Sep 18, 2012 1:29 pm ]
Post subject:  Re: PDF Sharp modifying document by adding text

Hi,

I have the same problem. I use PDF templates that have certain fields, e.g. product no. and description. The templates are created with Acrobat Reader.
These fields are supposed to be filled with information from a database automatically. I am as far as open the PDF by using...

PdfDocument template =PdfReader.Open(Path);

Now there should be something like...

template.field1.content = "blablabalbal";

template.save();

But I cannot find a way to reach the fields properties.

I hope I could clarify the problem.

Best regards,

D :lol:

Author:  Thomas Hoevel [ Tue Sep 18, 2012 1:35 pm ]
Post subject:  Re: PDF Sharp modifying document by adding text

Hi!

Maybe this post can help:
http://stackoverflow.com/a/6672303/162529

Author:  davidh [ Wed Sep 19, 2012 8:35 am ]
Post subject:  Re: PDF Sharp modifying document by adding text

Thanks for that post!

I just found the solution. There were two problems. First open your PDF with Adobe Acrobat and look in the form fields properties.
There you find its name. In the source code you will find it as "Text5".
The second problem was a NullReferenceException, which comes up in the "set the value of this field" code. The solution is by implementing the form field in Adobe Acrobat to give the field in its properties menu a
defaul value. That does the trick! Now I can fill the fields using C# and PDFSharp.

best regards,

David

Code:
PdfDocument template = PdfReader.Open(strPDFName, PdfDocumentOpenMode.Modify);

                    if (template.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
                        template.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));

                PdfTextField currentField = (PdfTextField)(template.AcroForm.Fields["Text5"]);
               
                //const 
                string caseName = "Hallo";
                PdfString caseNamePdfStr = new PdfString(caseName);


                //set the value of this field
               
                currentField.Value = caseNamePdfStr;

               
                // Save the document...
                template.Save(strPDFName);

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