为了账号安全,请及时绑定邮箱和手机立即绑定

C#:使用PDFSharp创建PDF表单(AcroForm)

C#:使用PDFSharp创建PDF表单(AcroForm)

C#
小怪兽爱吃肉 2022-08-20 16:19:24
如何将PDF表单元素添加到PDF摘要对象?PdfPage我知道AcroForm是表单可填写的PDF元素的最佳格式,但是PDFsharp库似乎不允许您创建AcroForm对象的实例。我已经能够使用PDFsharp来生成简单的文档,如下所示:static void Main(string[] args) {    PdfDocument document = new PdfDocument();    document.Info.Title = "Created with PDFsharp";    // Create an empty page    PdfPage page = document.AddPage();    // Draw Text    XGraphics gfx = XGraphics.FromPdfPage(page);    XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);    gfx.DrawString("Hello, World!", font, XBrushes.Black,        new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);    // Save document    const string filename = "HelloWorld.pdf";    document.Save(filename);}但是我无法弄清楚如何添加可填写的表单元素。我收集它可能会使用这种方法,但是你如何制作AcroForm?(由于像这样的类似乎没有公共构造函数)page.Elements.Add(string key, PdfItem item)PdfItemPdfTextFieldPDFHarp论坛和文档对此没有帮助,我在Stack Overflow上找到的最接近的答案是这个,它用错误的库回答。那么,简而言之:如何将上面的文本转换为文本字段?"Hello World"是否可以在PDFsharp中执行此操作,或者我应该使用其他C#PDF库?(我非常想坚持使用免费的 - 最好是开源的 - 库)
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

PdfSharp中的大多数类构造函数都是密封的,这使得创建新的pdf对象变得有点困难。但是,您可以使用它的类创建对象以添加低级pdf元素。


下面是创建文本字段的示例。


请参阅pdf技术规范,从第432页开始,关于关键元素的定义 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf


        public static void AddTextBox()

        {


            using (PdfDocument pdf = new PdfDocument())

            {

                PdfPage page1 = pdf.AddPage();


                double left = 50;

                double right = 200;

                double bottom = 750;

                double top = 725;


                PdfArray 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);


                PdfDictionary form = new PdfDictionary(pdf);

                form.Elements.Add("/Filter", new PdfName("/FlateDecode"));

                form.Elements.Add("/Length", new PdfInteger(20));

                form.Elements.Add("/Subtype", new PdfName("/Form"));

                form.Elements.Add("/Type", new PdfName("/XObject"));

                pdf.Internals.AddObject(form);


                PdfDictionary appearanceStream = new PdfDictionary(pdf);

                appearanceStream.Elements.Add("/N", form);

                pdf.Internals.AddObject(appearanceStream);


                PdfDictionary textfield = new PdfDictionary(pdf);

                textfield.Elements.Add("/FT", new PdfName("/Tx"));

                textfield.Elements.Add("/Subtype", new PdfName("/Widget"));

                textfield.Elements.Add("/T", new PdfString("fldHelloWorld"));

                textfield.Elements.Add("/V", new PdfString("Hello World!"));

                textfield.Elements.Add("/Type", new PdfName("/Annot"));

                textfield.Elements.Add("/AP", appearanceStream);

                textfield.Elements.Add("/Rect", rect);

                textfield.Elements.Add("/P", page1);

                pdf.Internals.AddObject(textfield);


                PdfArray annotsArray = new PdfArray(pdf);

                annotsArray.Elements.Add(textfield);

                pdf.Internals.AddObject(annotsArray);


                page1.Elements.Add("/Annots", annotsArray);


                // draw rectangle around text field

                //XGraphics gfx = XGraphics.FromPdfPage(page1);

                //gfx.DrawRectangle(new XPen(XColors.DarkOrange, 2), left, 40, right, bottom - top);


                // Save document

                const string filename = @"C:\Downloads\HelloWorld.pdf";

                pdf.Save(filename);

                pdf.Close();


                Process.Start(filename);

            }

        }


查看完整回答
反对 回复 2022-08-20
  • 1 回答
  • 0 关注
  • 294 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信