【问题标题】:Dynamically Generate Editable PDF exactly as same in the html form using ASP.NET MVC使用 ASP.NET MVC 在 html 表单中动态生成完全相同的可编辑 PDF
【发布时间】:2016-01-28 16:04:39
【问题描述】:

我正在研究动态生成网络表单,并且正在使用 iTextSharp 生成可编辑的 PDF。

以下是表单的截图:

各种复选框、单选按钮组、表格可以使表单更加冗长。

在不改变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。

我的问题:

我能否像 html 表单一样动态生成具有确切顺序和位置的字段的可编辑 PDF 表单?

那么,无论表单字段在哪里,是否都可以获得 字段的确切顺序和位置(宽度和高度),就像 html 表单一样? p>

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

【问题讨论】:

  • 我真的不知道 HTML 字段的“确切位置”是什么意思,除非你定位东西 fixed。大多数现代浏览器都会以类似的方式呈现 HTML,但我认为您的 xy 坐标不会在 Firefox、Chrome、IE、Opera、Edge 等平台上匹配。因此,您将不得不制作该部分,选择浏览器或构建自己的浏览器并解决该问题。一旦你有了它,那么你应该能够将它们转换为 PDF 坐标空间,尽管你还需要处理 HTML 的“无限页面长度”并将其转换为 PDF 的固定页面大小。
  • 你能解释一下什么是“无限页长”吗?
  • 如果您想出一个非常可靠的元素装饰方案,那么您可以使用您的方案进行解析,而不是尝试通过 html 确定布局?例如:class="row1 column2 checkbox"。你可以制作一个非常简单的解析器。
  • 你是说每一个东西都应该用PDF的表格格式生成?
  • 在某些时候,您将不得不限制可以在 pdf 中复制的范围。如果您将 html 表单布局限制为表格或“类似网格”的结构,那么整个导出为 pdf 将变得非常困难。

标签: html asp.net-mvc pdf itextsharp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2012-06-10
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多