简体   繁体   English

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

[英]C#: Create PDF Form (AcroForm) using PDFsharp

How does one add a PDF Form element to a PDFsharp PdfPage object?如何将 PDF 表单元素添加到 PDFsharp PdfPage对象?

I understand that AcroForm is the best format for form-fillable PDF elements, but the PDFsharp library doesn't seem to allow you to create instances of the AcroForm objects.我知道 AcroForm 是可填写表单的 PDF 元素的最佳格式,但 PDFsharp 库似乎不允许您创建 AcroForm 对象的实例。

I have been able to use PDFsharp to generate simple documents, as here:我已经能够使用 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);
}

But I cannot work out how to add a fillable form element.但我不知道如何添加可填写的表单元素。 I gather it would likely use the page.Elements.Add(string key, PdfItem item) method, but how do you make an AcroForm PdfItem ?我认为它可能会使用page.Elements.Add(string key, PdfItem item)方法,但是如何制作 AcroForm PdfItem (As classes like PdfTextField do not seem to have a public constructor) (因为像PdfTextField这样的类似乎没有公共构造函数)

The PDFsharp forums and documentation have not helped with this, and the closest answer I found on Stack Overflow was this one , which is answering with the wrong library. PDFsharp 论坛和文档对此没有帮助,我在 Stack Overflow 上找到的最接近的答案是this one ,它使用错误的库进行回答。

So, in short: How would I convert the "Hello World" text above into a text field?所以,简而言之:如何将上面的"Hello World"文本转换为文本字段?

Is it possible to do this in PDFsharp, or should I be using a different C# PDF library?是否可以在 PDFsharp 中执行此操作,还是应该使用不同的 C# PDF 库? (I would very much like to stick with free - and preferably open-source - libraries) (我非常想坚持使用免费的 - 最好是开源的 - 库)

Most of the classes constructors in PdfSharp are sealed which makes it kind of difficult to create new pdf objects. PdfSharp 中的大多数类构造函数都是密封的,这使得创建新的 pdf 对象有点困难。 However, you can create objects using it's classes to add low-level pdf elements.但是,您可以使用它的类创建对象来添加低级 pdf 元素。

Below is an example of creating a text field.下面是创建文本字段的示例。

Please refer to the pdf tech specs starting on page 432 on definition of key elements https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf请参阅从第 432 页开始的关于关键元素定义的 pdf 技术规范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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM