2015-10-28 19 views
0

我正在动态生成Web表单,并使用iTextSharp生成可编辑的PDF。使用ASP.NET MVC在html表单中动态生成可编辑的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。

我的问题:

我可以动态生成具有确切顺序和位置作为HTML表单有字段编辑PDF形式?

所以,无论表单域,是有可能得到的字段(宽&高度)作为HTML形式具有的确切顺序&位置?

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

+1

我真的不知道HTML字段的“确切位置”是什么意思,除非你的位置固定。大多数现代浏览器都会同样呈现HTML,但我不认为你的'x'和'y'坐标可以匹配Firefox,Chrome,IE,Opera,Edge等等。所以你将不得不做出这个部分,选择一个浏览器或建立你自己的解决方案。一旦你有了,那么你应该能够将这些转换为PDF坐标空间,尽管你还需要处理HTML的“无限页长”并将其转换为PDF的固定页面大小。 –

+0

请问你能解释一下“无限页长”吗? – Vikash

+0

如果你想出一个装饰你的元素的方案是非常稳固的,那么你可以使用你的方案解析,而不是试图通过html来确定布局?例如:class =“row1 column2 checkbox”。你可以做一个非常简单的解析器。 –

回答

0

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

+0

是否可以生成可编辑的PDF – Vikash

相关问题