Skip to content

Commit fd65386

Browse files
committed
Added comments
1 parent 014c277 commit fd65386

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

FormFieldSample/FormFieldSample/Program.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,97 @@ static void Main(string[] args) {
1313
FillForm();
1414
}
1515
static void CreateForm() {
16+
//Create a new PDF document.
1617
PdfDocument document = new PdfDocument();
18+
//Add a new page to the PDF document.
1719
PdfPage page = document.Pages.Add();
20+
//Set the font.
1821
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
22+
//Draw the text.
1923
page.Graphics.DrawString("Job Application", font,PdfBrushes.Black, new PointF(250,0));
2024

2125
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
2226
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
27+
//Create a textbox field and add the properties.
2328
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
2429
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
2530
textBoxField1.ToolTip = "Name";
31+
//Add the form field to the document.
2632
document.Form.Fields.Add(textBoxField1);
2733

2834
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
35+
//Create a textbox field and add the properties.
2936
PdfTextBoxField textBoxField2 = new PdfTextBoxField(page, "Email address");
3037
textBoxField2.Bounds = new RectangleF(10, 100, 200, 20);
3138
textBoxField2.ToolTip = "Email address";
39+
//Add the form field to the document.
3240
document.Form.Fields.Add(textBoxField2);
3341

3442
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
43+
//Create a textbox field and add the properties.
3544
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Phone");
3645
textBoxField3.Bounds = new RectangleF(10, 160, 200, 20);
3746
textBoxField3.ToolTip = "Phone";
47+
//Add the form field to the document.
3848
document.Form.Fields.Add(textBoxField3);
3949

4050
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
51+
//Create a Radio button.
4152
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
53+
//Add the radio button into form.
4254
document.Form.Fields.Add(employeesRadioList);
55+
//Create radio button items.
4356
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
4457
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
4558
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
4659
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
4760
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
4861
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
62+
//Add the items to radio button group.
4963
employeesRadioList.Items.Add(radioButtonItem1);
5064
employeesRadioList.Items.Add(radioButtonItem2);
5165

66+
//Create file stream.
5267
using (FileStream outputFileStream= new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite)) {
68+
//Save the PDF document to file stream.
5369
document.Save(outputFileStream);
5470
}
71+
//Close the document.
5572
document.Close(true);
5673
}
5774
static void FillForm() {
75+
//Get stream from an existing PDF document.
5876
FileStream docStream = new FileStream("Output.pdf", FileMode.Open, FileAccess.Read);
77+
//Load the existing PDF document.
5978
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
79+
//Load the form from the loaded document.
6080
PdfLoadedForm form = loadedDocument.Form;
81+
//Load the form field collections from the form.
6182
PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection;
6283
PdfLoadedField loadedField = null;
6384

64-
if(fieldCollection.TryGetField("Name", out loadedField)) {
85+
//Get the field using TryGetField Method.
86+
if (fieldCollection.TryGetField("Name", out loadedField)) {
6587
(loadedField as PdfLoadedTextBoxField).Text = "Simons";
6688
}
6789
if (fieldCollection.TryGetField("Email address", out loadedField)) {
6890
(loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
6991
}
70-
7192
if (fieldCollection.TryGetField("Phone", out loadedField)) {
7293
(loadedField as PdfLoadedTextBoxField).Text = "31 12 34 56";
7394
}
74-
7595
if (fieldCollection.TryGetField("Gender", out loadedField)) {
7696
(loadedField as PdfLoadedRadioButtonListField).SelectedIndex = 0;
7797
}
98+
//Flatten the whole form.
7899
form.Flatten=true;
79100

101+
//Create file stream.
80102
using (FileStream outputFileStream = new FileStream("FilledPDF.pdf", FileMode.Create, FileAccess.ReadWrite)) {
103+
//Save the PDF document to file stream.
81104
loadedDocument.Save(outputFileStream);
82105
}
106+
//Close the document.
83107
loadedDocument.Close(true);
84108
}
85109
}

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,100 @@ We will create a new .NET console application, add the Syncfusion PDF library pa
1616
```csharp
1717
static void CreateForm()
1818
{
19+
//Create a new PDF document.
1920
PdfDocument document = new PdfDocument();
21+
//Add a new page to the PDF document.
2022
PdfPage page = document.Pages.Add();
23+
//Set the font.
2124
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
25+
//Draw the text.
2226
page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 0));
2327

2428
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
2529
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
30+
//Create a textbox field and add the properties.
2631
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
2732
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
2833
textBoxField1.ToolTip = "Name";
34+
//Add the form field to the document.
2935
document.Form.Fields.Add(textBoxField1);
3036

3137
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
38+
//Create a textbox field and add the properties.
3239
PdfTextBoxField textBoxField2 = new PdfTextBoxField(page, "Email address");
3340
textBoxField2.Bounds = new RectangleF(10, 100, 200, 20);
3441
textBoxField2.ToolTip = "Email address";
42+
//Add the form field to the document.
3543
document.Form.Fields.Add(textBoxField2);
3644

3745
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
46+
//Create a textbox field and add the properties.
3847
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Phone");
3948
textBoxField3.Bounds = new RectangleF(10, 160, 200, 20);
4049
textBoxField3.ToolTip = "Phone";
50+
//Add the form field to the document.
4151
document.Form.Fields.Add(textBoxField3);
4252

4353
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
54+
//Create a Radio button.
4455
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
56+
//Add the radio button into form.
4557
document.Form.Fields.Add(employeesRadioList);
4658
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
59+
//Create radio button items.
4760
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
4861
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
4962
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
5063
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
5164
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
65+
//Add the items to radio button group.
5266
employeesRadioList.Items.Add(radioButtonItem1);
5367
employeesRadioList.Items.Add(radioButtonItem2);
5468

55-
69+
//Create file stream.
5670
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
57-
{
71+
{
72+
//Save the PDF document to file stream.
5873
document.Save(outputFileStream);
5974
}
75+
//Close the document.
6076
document.Close(true);
6177
}
6278

6379
static void FillForm()
6480
{
81+
//Get stream from an existing PDF document.
6582
FileStream docStream = new FileStream("Output.pdf", FileMode.Open, FileAccess.Read);
83+
//Load the existing PDF document.
6684
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
85+
//Load the form from the loaded document.
6786
PdfLoadedForm form = loadedDocument.Form;
87+
//Load the form field collections from the form.
6888
PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection;
6989
PdfLoadedField loadedField = null;
7090

91+
//Get the field using TryGetField Method.
7192
if (fieldCollection.TryGetField("Name", out loadedField)) {
7293
(loadedField as PdfLoadedTextBoxField).Text = "Simons";
7394
}
7495
if (fieldCollection.TryGetField("Email address", out loadedField)) {
7596
(loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
7697
}
77-
7898
if (fieldCollection.TryGetField("Phone", out loadedField)) {
7999
(loadedField as PdfLoadedTextBoxField).Text = "31 12 34 56";
80100
}
81-
82101
if (fieldCollection.TryGetField("Gender", out loadedField)) {
83102
(loadedField as PdfLoadedRadioButtonListField).SelectedIndex = 0;
84103
}
104+
//Flatten the whole form.
85105
form.Flatten = true;
86-
106+
107+
//Create file stream.
87108
using (FileStream outputFileStream = new FileStream("FilledPDF.pdf", FileMode.Create, FileAccess.ReadWrite)) {
109+
//Save the PDF document to file stream.
88110
loadedDocument.Save(outputFileStream);
89111
}
112+
//Close the document.
90113
loadedDocument.Close(true);
91114
}
92115
```

0 commit comments

Comments
 (0)