PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 6:16 pm

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: Sat Sep 15, 2012 6:27 pm 
Offline

Joined: Sat Sep 15, 2012 6:04 pm
Posts: 1
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 18, 2012 1:29 pm 
Offline

Joined: Tue Sep 18, 2012 1:15 pm
Posts: 2
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:


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 18, 2012 1:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Hi!

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 19, 2012 8:35 am 
Offline

Joined: Tue Sep 18, 2012 1:15 pm
Posts: 2
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);


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 58 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