Reputation: 1989
I have a sample pdf, that I am filling out programmatically as such (using iText7):-
string name = TextBox1.Text.ToString();
string pdfTemplate = @"..\WebApplication1\Sample.pdf";
string newFile = @"..\WebApplication1\completed_sample.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(pdfTemplate).SetUnethicalReading(true), new PdfWriter(newFile));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
// This doesn't work //
fields.TryGetValue("TypeofApplication.1", out toSet);
toSet.SetValue("/On");
// This works //
fields.TryGetValue("FullName", out toSet);
toSet.SetValue(name);
form.FlattenFields();
pdf.Close();
I am able to fill out the text boxes in the pdf, but not to fill out the radio buttons/checkboxes.
Type of Application has two options in my cshtml
page:
@Html.RadioButton("Application_Type", "New") New
@Html.RadioButton("Application_Type", "Renew") Renewal
and when I look through my values dictionary, I see that there are 3 options:
How do I set the checkboxes as checked = true
.
My logic is as follow:
if(dr.Application_Type == "New"){
fields.TryGetValue("TypeofApplication.1", out toSet);
toSet.SetValue("/On");}
But this obviously doesn't work.
Upvotes: 2
Views: 1445
Reputation: 1989
So the way I figured this out:-
1) I opened the pdf, set the checkbox as checked and saved it in my workspace folder.
2) Then, I programmatically opened the file and inspected the field value as such:-
string pdfTemplate = @"..\WebApplication1\Sample.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(pdfTemplate).SetUnethicalReading(true), new PdfWriter(newFile));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("TypeofApplication", out toSet);
var x = toSet.GetValueAsString();
Now, I know the value of x, which is the checked field. (New or Renew). I used this technique to find all values possible for any given checkbox, radiobutton list, et cetera.
Upvotes: 2