-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListFormFields.aspx.cs
executable file
·70 lines (57 loc) · 2.33 KB
/
ListFormFields.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text.pdf;
public partial class ListFormFields : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var pdfs = Directory.GetFiles(Server.MapPath("~/PDFTemplates"), "*.pdf");
foreach (var pdf in pdfs)
ddlPDFs.Items.Add(Path.GetFileName(pdf));
}
}
protected void btnShowFields_Click(object sender, EventArgs e)
{
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates"), ddlPDFs.SelectedValue);
var fieldInfo = new List<string>();
var reader = new PdfReader(pdfPath);
var formFields = reader.AcroFields;
foreach (DictionaryEntry entry in formFields.Fields)
{
var formFieldType = PDFFieldType.GetPDFFieldType(formFields.GetFieldType(entry.Key.ToString()));
if (formFieldType is PDFCheckBoxFieldType)
fieldInfo.Add(string.Format("{0} - {1} - Export Value: {2}",
entry.Key,
formFieldType,
PDFHelper.GetExportValue(entry.Value as AcroFields.Item)));
else
fieldInfo.Add(string.Format("{0} - {1}", entry.Key, formFieldType));
}
reader.Close();
// Get the form fields for this PDF and bind them to the BulletedList control
blFields.DataSource = fieldInfo;
blFields.DataBind();
}
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates"), ddlPDFs.SelectedValue);
// Get the form fields for this PDF and give them increasing values
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
var counter = 1;
foreach (var key in new List<string>(formFieldMap.Keys))
{
formFieldMap[key] = counter.ToString();
counter++;
}
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap, null);
PDFHelper.ReturnPDF(pdfContents, ddlPDFs.SelectedValue + "-Sample.pdf");
}
}