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
I see there is a Fork that tries to cater for this:
https://github.com/packdat/PDFsharp-net6/tree/AcroFormshttps://github.com/empira/PDFsharp/issu ... 2007996778which 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 with
AcroForms 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?