PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Nov 12, 2024 10:22 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Oct 16, 2024 6:17 pm 
Offline

Joined: Thu Jan 04, 2024 12:03 pm
Posts: 3
Good Day,

Thank you for the best open-source PDF Library.

We use PDFsharp and need to create fillable (AcroForm) pdf documents, also a way to read and/or update such forms using PDFsharp.

To create PDF Forms from what I have found it seems possible in a way, but due to Most of the classes constructors in PDFsharp for AcroForms being sealed, and internal - it makes this difficult to work with Acroforms.

We have been trying to Create these forms using code like this:

Code:
.....
 var rect = new PdfArray(pdf);
 rect.Elements.Add(new PdfReal(left));
 rect.Elements.Add(new PdfReal(bottom));
 rect.Elements.Add(new PdfReal(right));
 rect.Elements.Add(new PdfReal(top));
 pdf.Internals.AddObject(rect);

 var form = new PdfDictionary(pdf);
 form.Elements.Add("/Filter", new PdfName("/FlateDecode"));
 form.Elements.Add("/Length", new PdfInteger(fieldLength));
 form.Elements.Add("/Subtype", new PdfName("/Form"));
 form.Elements.Add("/Type", new PdfName("/XObject"));
 pdf.Internals.AddObject(form);

 var appearanceStream = new PdfDictionary(pdf);
 appearanceStream.Elements.Add("/N", form);
 pdf.Internals.AddObject(appearanceStream);

 var textField = new PdfDictionary(pdf);
 textField.Elements.Add("/FT", new PdfName("/Tx"));
 textField.Elements.Add("/Subtype", new PdfName("/Widget"));
 textField.Elements.Add("/T", new PdfString(fieldName));
 textField.Elements.Add("/V", new PdfString(fieldValue));
 textField.Elements.Add("/Type", new PdfName("/Annot"));
 textField.Elements.Add("/AP", appearanceStream);
 textField.Elements.Add("/Rect", rect);
 textField.Elements.Add("/P", page);

 // Add appearance properties for background color and border
 var mk = new PdfDictionary(pdf);
 var bgColor = ConvertXColorToPdfArray(ConvertColorNameToXColor(backgroundColor), pdf);
 mk.Elements.Add("/BG", bgColor);
 textField.Elements.Add("/MK", mk);

 var bs = new PdfDictionary(pdf);
 bs.Elements.Add("/W", new PdfReal(borderWidth)); // Border width
 bs.Elements.Add("/S", new PdfName("/S")); // Solid border
 var bdrColor = ConvertXColorToPdfArray(ConvertColorNameToXColor(borderColor), pdf);
 bs.Elements.Add("/C", bdrColor);
 textField.Elements.Add("/BS", bs);

 // Set quading (text alignment) and default appearance
 textField.Elements.Add("/Q", new PdfInteger((int)textAlignment)); // 0 = Left, 1 = Center, 2 = Right
 var color = ConvertColorNameToXColor(textColor);
 textField.Elements.Add("/DA",
   new PdfString(
     $"/Helv {fontSize} Tf {color.R / 255.0} {color.G / 255.0} {color.B / 255.0} rg {borderWidth} w {bdrColor.Elements[0]} {bdrColor.Elements[1]} {bdrColor.Elements[2]} RG"));


 pdf.Internals.AddObject(textField);
...


But was really hoping there is an easier way as we don't really want to get into the internals of how PDF works, but be able to use the functionality :oops:

I see there is a Fork that tries to cater for this:

https://github.com/packdat/PDFsharp-net6/tree/AcroForms
https://github.com/empira/PDFsharp/issu ... 2007996778

which has some good features, like the 14 Standard fonts I really think should be included to the new version of PDFSharp, and then the changes withAcroForms part that seems to be going in the right direction...

I also see that there was a mention that PDFSharp 1.31 was able to do this, not sure, as I have not found any samples.

We also see that the digital signatures, is one of the new features that have come in to the latest vertsion, very exciting! And was hoping that the Acroforms could be re-assest and looked at to make this functionality easier to use, also if there was perhaps some samples of using the way we are trying now, to make things easier perhaps?


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 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