@@ -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 }
0 commit comments