|
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