Skip to content

Commit a118288

Browse files
Merge pull request #1 from SyncfusionExamples/Form-Field
834148 Added Sample and README.md file for Form Field
2 parents 1bea0db + fd65386 commit a118288

File tree

6 files changed

+299
-2
lines changed

6 files changed

+299
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33417.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FormFieldSample", "FormFieldSample\FormFieldSample.csproj", "{3E766E28-20B2-46B2-B843-B1422A5D0466}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3E766E28-20B2-46B2-B843-B1422A5D0466}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3E766E28-20B2-46B2-B843-B1422A5D0466}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3E766E28-20B2-46B2-B843-B1422A5D0466}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3E766E28-20B2-46B2-B843-B1422A5D0466}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {13028B57-84D9-4AD1-8DC7-AF875EC170E3}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.2.4" />
12+
</ItemGroup>
13+
14+
</Project>
73.5 KB
Loading
77.3 KB
Loading
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.Pdf.Interactive;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Drawing;
5+
using Syncfusion.Pdf.Parsing;
6+
7+
namespace FormFieldSample {
8+
internal class Program {
9+
static void Main(string[] args) {
10+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2VFhhQlVEfV5AQmBIYVp/TGpJfl96cVxMZVVBJAtUQF1hSn5Qd0FjUH5fdX1RR2ZZ ");
11+
12+
CreateForm();
13+
FillForm();
14+
}
15+
static void CreateForm() {
16+
//Create a new PDF document.
17+
PdfDocument document = new PdfDocument();
18+
//Add a new page to the PDF document.
19+
PdfPage page = document.Pages.Add();
20+
//Set the font.
21+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
22+
//Draw the text.
23+
page.Graphics.DrawString("Job Application", font,PdfBrushes.Black, new PointF(250,0));
24+
25+
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
26+
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
27+
//Create a textbox field and add the properties.
28+
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
29+
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
30+
textBoxField1.ToolTip = "Name";
31+
//Add the form field to the document.
32+
document.Form.Fields.Add(textBoxField1);
33+
34+
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
35+
//Create a textbox field and add the properties.
36+
PdfTextBoxField textBoxField2 = new PdfTextBoxField(page, "Email address");
37+
textBoxField2.Bounds = new RectangleF(10, 100, 200, 20);
38+
textBoxField2.ToolTip = "Email address";
39+
//Add the form field to the document.
40+
document.Form.Fields.Add(textBoxField2);
41+
42+
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
43+
//Create a textbox field and add the properties.
44+
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Phone");
45+
textBoxField3.Bounds = new RectangleF(10, 160, 200, 20);
46+
textBoxField3.ToolTip = "Phone";
47+
//Add the form field to the document.
48+
document.Form.Fields.Add(textBoxField3);
49+
50+
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
51+
//Create a Radio button.
52+
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
53+
//Add the radio button into form.
54+
document.Form.Fields.Add(employeesRadioList);
55+
//Create radio button items.
56+
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
57+
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
58+
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
59+
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
60+
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
61+
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
62+
//Add the items to radio button group.
63+
employeesRadioList.Items.Add(radioButtonItem1);
64+
employeesRadioList.Items.Add(radioButtonItem2);
65+
66+
//Create file stream.
67+
using (FileStream outputFileStream= new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite)) {
68+
//Save the PDF document to file stream.
69+
document.Save(outputFileStream);
70+
}
71+
//Close the document.
72+
document.Close(true);
73+
}
74+
static void FillForm() {
75+
//Get stream from an existing PDF document.
76+
FileStream docStream = new FileStream("Output.pdf", FileMode.Open, FileAccess.Read);
77+
//Load the existing PDF document.
78+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
79+
//Load the form from the loaded document.
80+
PdfLoadedForm form = loadedDocument.Form;
81+
//Load the form field collections from the form.
82+
PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection;
83+
PdfLoadedField loadedField = null;
84+
85+
//Get the field using TryGetField Method.
86+
if (fieldCollection.TryGetField("Name", out loadedField)) {
87+
(loadedField as PdfLoadedTextBoxField).Text = "Simons";
88+
}
89+
if (fieldCollection.TryGetField("Email address", out loadedField)) {
90+
(loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
91+
}
92+
if (fieldCollection.TryGetField("Phone", out loadedField)) {
93+
(loadedField as PdfLoadedTextBoxField).Text = "31 12 34 56";
94+
}
95+
if (fieldCollection.TryGetField("Gender", out loadedField)) {
96+
(loadedField as PdfLoadedRadioButtonListField).SelectedIndex = 0;
97+
}
98+
//Flatten the whole form.
99+
form.Flatten=true;
100+
101+
//Create file stream.
102+
using (FileStream outputFileStream = new FileStream("FilledPDF.pdf", FileMode.Create, FileAccess.ReadWrite)) {
103+
//Save the PDF document to file stream.
104+
loadedDocument.Save(outputFileStream);
105+
}
106+
//Close the document.
107+
loadedDocument.Close(true);
108+
}
109+
}
110+
}

README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,150 @@
1-
# how-to-create-fill-and-flatten-PDF-form-fields-in-net
2-
How to Create, Fill, and Flatten PDF Form Fields in .NET using the PDF Library
1+
# How to Create, Fill, and Flatten PDF Form Fields in .NET using the Syncfusion PDF Library
2+
3+
## Introduction
4+
A quick start .NET console project that shows how to create, fill, and flatten PDF form fields using the Syncfusion PDF Library.
5+
6+
## System requirement
7+
**Framework and SDKs**
8+
* .NET SDK (version 5.0 or later)
9+
10+
**IDEs**
11+
* Visual Studio 2019/ Visual Studio 2022
12+
13+
## Code snippet for Create, Fill, and Flatten PDF Form Fields
14+
We will create a new .NET console application, add the Syncfusion PDF library package, and write the code
15+
16+
```csharp
17+
static void CreateForm()
18+
{
19+
//Create a new PDF document.
20+
PdfDocument document = new PdfDocument();
21+
//Add a new page to the PDF document.
22+
PdfPage page = document.Pages.Add();
23+
//Set the font.
24+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
25+
//Draw the text.
26+
page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 0));
27+
28+
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
29+
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
30+
//Create a textbox field and add the properties.
31+
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
32+
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
33+
textBoxField1.ToolTip = "Name";
34+
//Add the form field to the document.
35+
document.Form.Fields.Add(textBoxField1);
36+
37+
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
38+
//Create a textbox field and add the properties.
39+
PdfTextBoxField textBoxField2 = new PdfTextBoxField(page, "Email address");
40+
textBoxField2.Bounds = new RectangleF(10, 100, 200, 20);
41+
textBoxField2.ToolTip = "Email address";
42+
//Add the form field to the document.
43+
document.Form.Fields.Add(textBoxField2);
44+
45+
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
46+
//Create a textbox field and add the properties.
47+
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Phone");
48+
textBoxField3.Bounds = new RectangleF(10, 160, 200, 20);
49+
textBoxField3.ToolTip = "Phone";
50+
//Add the form field to the document.
51+
document.Form.Fields.Add(textBoxField3);
52+
53+
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
54+
//Create a Radio button.
55+
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
56+
//Add the radio button into form.
57+
document.Form.Fields.Add(employeesRadioList);
58+
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
59+
//Create radio button items.
60+
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
61+
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
62+
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
63+
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
64+
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
65+
//Add the items to radio button group.
66+
employeesRadioList.Items.Add(radioButtonItem1);
67+
employeesRadioList.Items.Add(radioButtonItem2);
68+
69+
//Create file stream.
70+
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
71+
{
72+
//Save the PDF document to file stream.
73+
document.Save(outputFileStream);
74+
}
75+
//Close the document.
76+
document.Close(true);
77+
}
78+
79+
static void FillForm()
80+
{
81+
//Get stream from an existing PDF document.
82+
FileStream docStream = new FileStream("Output.pdf", FileMode.Open, FileAccess.Read);
83+
//Load the existing PDF document.
84+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
85+
//Load the form from the loaded document.
86+
PdfLoadedForm form = loadedDocument.Form;
87+
//Load the form field collections from the form.
88+
PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection;
89+
PdfLoadedField loadedField = null;
90+
91+
//Get the field using TryGetField Method.
92+
if (fieldCollection.TryGetField("Name", out loadedField)) {
93+
(loadedField as PdfLoadedTextBoxField).Text = "Simons";
94+
}
95+
if (fieldCollection.TryGetField("Email address", out loadedField)) {
96+
(loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
97+
}
98+
if (fieldCollection.TryGetField("Phone", out loadedField)) {
99+
(loadedField as PdfLoadedTextBoxField).Text = "31 12 34 56";
100+
}
101+
if (fieldCollection.TryGetField("Gender", out loadedField)) {
102+
(loadedField as PdfLoadedRadioButtonListField).SelectedIndex = 0;
103+
}
104+
//Flatten the whole form.
105+
form.Flatten = true;
106+
107+
//Create file stream.
108+
using (FileStream outputFileStream = new FileStream("FilledPDF.pdf", FileMode.Create, FileAccess.ReadWrite)) {
109+
//Save the PDF document to file stream.
110+
loadedDocument.Save(outputFileStream);
111+
}
112+
//Close the document.
113+
loadedDocument.Close(true);
114+
}
115+
```
116+
117+
**Output Image**
118+
119+
**CreateFormPDF**
120+
<img src="FormFieldSample/FormFieldSample/Output_images/CreateFormPDF.png" alt="Output_Image" width="100%" Height="Auto"/>
121+
122+
**FilledPDF**
123+
<img src="FormFieldSample/FormFieldSample/Output_images/FilledPDF.png" alt="output_image" width="100%" Height="Auto"/>
124+
125+
## How to run the examples
126+
* Download this project to a location in your disk.
127+
* Open the solution file using Visual Studio.
128+
* Rebuild the solution to install the required NuGet package.
129+
* Run the application.
130+
131+
## Resources
132+
* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net)
133+
* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview)
134+
* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5)
135+
* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf)
136+
* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf)
137+
* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks)
138+
* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/)
139+
140+
## Support and feedback
141+
* For any other queries, reach our [Syncfusion support team](https://www.syncfusion.com/support/directtrac/incidents/newincident?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
142+
* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
143+
144+
## License
145+
This is a commercial product and requires a paid license for possession or use. Syncfusion’s licensed software, including this component, is subject to the terms and conditions of [Syncfusion's EULA](https://www.syncfusion.com/eula/es/?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). You can purchase a licnense [here](https://www.syncfusion.com/sales/products?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or start a free 30-day trial [here](https://www.syncfusion.com/account/manage-trials/start-trials?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
146+
147+
## About Syncfusion
148+
Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 26,000+ customers and more than 1 million users, including large financial institutions, Fortune 500 companies, and global IT consultancies.
149+
150+
Today, we provide 1600+ components and frameworks for web ([Blazor](https://www.syncfusion.com/blazor-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET WebForms](https://www.syncfusion.com/jquery/aspnet-webforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Angular](https://www.syncfusion.com/angular-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [React](https://www.syncfusion.com/react-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Vue](https://www.syncfusion.com/vue-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), mobile ([Xamarin](https://www.syncfusion.com/xamarin-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), and desktop development ([WinForms](https://www.syncfusion.com/winforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WPF](https://www.syncfusion.com/wpf-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WinUI(Preview)](https://www.syncfusion.com/winui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)). We provide ready-to-deploy enterprise software for dashboards, reports, data integration, and big data processing. Many customers have saved millions in licensing fees by deploying our software.

0 commit comments

Comments
 (0)