使用ASP.NET MVC在html表单中动态生成可编辑PDF

时间:2015-10-28 18:12:41

标签: html asp.net-mvc pdf itextsharp

我正致力于动态生成Web表单,我正在使用iTextSharp生成可编辑的PDF。

以下是表单的屏幕截图:

enter image description here

使用各种 复选框,单选按钮组,表格 ,表单可能会更加冗长。

如果html表单中的字段顺序没有任何变化,是否可以使用iTextSharp创建可编辑的表单?

我已经在PDF文档中生成了可编辑字段,我的代码是:

//Creating a document

    Document document = new Document(PageSize.A4, 50, 50, 25, 25);

    FileStream pdfFileStream = new FileStream("D:\\new.pdf", FileMode.Create);

//Writing to PDF
    using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream))
    {
        //Opening the document for writing
        document.Open();
        PdfContentByte cb = pdfWriter.DirectContent;

        Paragraph para = new Paragraph("Name");
        document.Add(para);

        //Creating the text box starts here --->
        TextField _text = new TextField(pdfWriter, new Rectangle(100, 806, 170, 790), "Name");
        _text.BackgroundColor = BaseColor.LIGHT_GRAY;
        _text.Alignment = Element.ALIGN_CENTER;
        //_text.Options = TextField.MULTILINE;
        _text.Text = "";
        pdfWriter.AddAnnotation(_text.GetTextField());
        cb = pdfWriter.DirectContent;

        //Creating the text box ends here ---<

        //Creating the RadioButton group starts here ---->

        PdfFormField _radioGroup = PdfFormField.CreateRadioButton(pdfWriter, true);
        _radioGroup.FieldName = "Gender";

        string[] genders = { "Male", "Female" };

        RadioCheckField genderRadioCheckField;
        PdfFormField radioGField;

        for (int i = 0; i < genders.Length; i++)
        {
            genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(40, 806 - i * 40, 60, 788 - i * 40), null, genders[i]);

            genderRadioCheckField.BackgroundColor = BaseColor.LIGHT_GRAY;
            genderRadioCheckField.CheckType = RadioCheckField.TYPE_CIRCLE;

            radioGField = genderRadioCheckField.RadioField;
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(genders[i], new Font(Font.FontFamily.HELVETICA, 12)), 70, 790 - i * 40, 0);
            _radioGroup.AddKid(radioGField);

        }

        pdfWriter.AddAnnotation(_radioGroup);
        cb = pdfWriter.DirectContent;
        //Creating the RadioButton group ends here ----<



        //Closing the document for writing
        document.Close();

        Console.WriteLine("Completed");
        Console.ReadLine();

    }

现在,代码生成带有文本字段和单选按钮的可编辑PDF。

我的问题:

我是否可以动态生成可编辑的PDF表单,其中的字段具有确切的顺序和位置,因为html表单有?

因此,无论表单字段在哪里,都可以获得 确切的顺序&amp; html表单有哪些字段的位置(宽度和高度)

注意:我正在动态创建表单。

1 个答案:

答案 0 :(得分:0)

你应该能够做到这一点。将视图渲染为字符串变量中的html,然后将html字符串转换为pdf。我正在使用EO.PDF,它提供了一种从html转换为pdf的方法。

相关问题