1- using Syncfusion . HtmlConverter ;
1+ using Syncfusion . Drawing ;
2+ using Syncfusion . HtmlConverter ;
23using Syncfusion . Pdf ;
3- using Syncfusion . Pdf . Graphics ;
44using Syncfusion . Pdf . Interactive ;
55using Syncfusion . Pdf . Parsing ;
6- using Syncfusion . Pdf . Redaction ;
7- using Syncfusion . Pdf . Security ;
86
9- class Program
7+ // Initialize the HTML to PDF converter
8+ HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter ( ) ;
9+ // Enable form conversion
10+ BlinkConverterSettings settings = new BlinkConverterSettings
1011{
11- static void Main ( string [ ] args )
12- {
13- // Initialize HTML to PDF converter and load HTML
14- HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter ( ) ;
15- string htmlFilePath = Path . GetFullPath ( @"Data/Input.html" ) ;
16- PdfDocument document = htmlConverter . Convert ( htmlFilePath ) ;
12+ EnableForm = true
13+ } ;
14+ htmlConverter . ConverterSettings = settings ;
15+ // Convert the HTML file to a PDF document
16+ using ( PdfDocument document = htmlConverter . Convert ( Path . GetFullPath ( @"Data/Input.html" ) ) )
17+ {
18+ // Set default appearance for form fields
19+ document . Form . SetDefaultAppearance ( false ) ;
1720
18- // Save the PDF to a memory stream
19- using ( MemoryStream memoryStream = new MemoryStream ( ) )
21+ // Save the PDF document to a memory stream
22+ using ( MemoryStream stream = new MemoryStream ( ) )
23+ {
24+ // Load the saved PDF document
25+ document . Save ( stream ) ;
26+ // Load the PDF document containing form fields
27+ PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( stream ) ;
28+ // Fill the form fields
29+ PdfLoadedForm form = loadedDocument . Form ;
30+ // Fill the "name" field
31+ if ( form . Fields [ "name" ] is PdfLoadedTextBoxField nameField )
2032 {
21- document . Save ( memoryStream ) ;
22- document . Close ( true ) ;
23-
24- // Load back the PDF for further processing
25- memoryStream . Position = 0 ;
26- PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( memoryStream ) ;
27-
28- // This will collect (pageIndex, word) for each form field
29- List < ( int pageIdx , TextWord word ) > fieldData = new List < ( int pageIdx , TextWord word ) > ( ) ;
30-
31- // Pass 1: Locate each placeholder and add a redaction on its bound
32- for ( int i = 0 ; i < loadedDocument . Pages . Count ; i ++ )
33- {
34- PdfLoadedPage page = loadedDocument . Pages [ i ] as PdfLoadedPage ;
35- page . ExtractText ( out TextLineCollection lines ) ;
36- if ( lines == null ) continue ;
37- foreach ( TextLine line in lines . TextLine )
38- {
39- foreach ( TextWord word in line . WordCollection )
40- {
41- if ( word == null ) continue ;
42- if ( word . Text == "{{name}}" ||
43- word . Text == "{{date}}" ||
44- word . Text == "{{signature}}" )
45- {
46- page . AddRedaction ( new PdfRedaction ( word . Bounds ) ) ;
47- fieldData . Add ( ( i , word ) ) ;
48- }
49- }
50- }
51- }
52- loadedDocument . Redact ( ) ;
53-
54- // Pass 2: Add form fields exactly over the bounds
55- foreach ( var ( pageIdx , word ) in fieldData )
56- {
57- PdfPageBase page = loadedDocument . Pages [ pageIdx ] ;
58-
59- if ( word . Text == "{{name}}" )
60- {
61- PdfTextBoxField textBox = new PdfTextBoxField ( page , "FirstName" )
62- {
63- Bounds = word . Bounds ,
64- ToolTip = "First Name" ,
65- Text = "John"
66- } ;
67- loadedDocument . Form . Fields . Add ( textBox ) ;
68- }
69- else if ( word . Text == "{{date}}" )
70- {
71- PdfTextBoxField dateField = new PdfTextBoxField ( page , "DateField" )
72- {
73- Bounds = word . Bounds
74- } ;
75- dateField . Actions . KeyPressed = new PdfJavaScriptAction ( "AFDate_KeystrokeEx(\" m/d/yy\" )" ) ;
76- dateField . Actions . Format = new PdfJavaScriptAction ( "AFDate_FormatEx(\" m/d/yy\" )" ) ;
77- loadedDocument . Form . Fields . Add ( dateField ) ;
78- }
79- else if ( word . Text == "{{signature}}" )
80- {
81- PdfSignatureField sigField = new PdfSignatureField ( page , "SignatureField" )
82- {
83- Bounds = word . Bounds ,
84- Signature = new PdfSignature ( )
85- } ;
86- // Optionally draw a signature image in the field area
87- FileStream imageStream = new FileStream ( Path . GetFullPath ( "Data/signature.png" ) , FileMode . Open , FileAccess . Read ) ;
88- PdfBitmap image = new PdfBitmap ( imageStream ) ;
89- ( page as PdfLoadedPage ) . Graphics . DrawImage ( image , word . Bounds ) ;
90- imageStream . Dispose ( ) ;
91-
92- // Optional: add digital certificate
93- using ( FileStream certStream = new FileStream ( Path . GetFullPath ( @"Data/PDF.pfx" ) , FileMode . Open , FileAccess . Read ) )
94- {
95- sigField . Signature . Certificate = new PdfCertificate ( certStream , "syncfusion" ) ;
96- sigField . Signature . Reason = "I am author of this document" ;
97- }
98- loadedDocument . Form . Fields . Add ( sigField ) ;
99- }
100- }
101- //Create file stream.
102- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Output.pdf" ) , FileMode . Create , FileAccess . ReadWrite ) )
33+ nameField . Text = "XYZ" ;
34+ }
35+ // Fill the "email" field
36+ if ( form . Fields [ "email" ] is PdfLoadedTextBoxField emailField )
37+ {
38+ emailField . Text = "[email protected] " ; 39+ }
40+ // Select "Male" in the "gender" dropdown
41+ if ( form . Fields [ "gender" ] is PdfLoadedComboBoxField genderField )
42+ {
43+ genderField . SelectedValue = "Male" ;
44+ }
45+ // Fill the "signature" field
46+ if ( form . Fields [ "signature" ] is PdfLoadedTextBoxField signatureTextBox )
47+ {
48+ // Get the original field's position and page
49+ RectangleF bounds = signatureTextBox . Bounds ;
50+ PdfPageBase page = signatureTextBox . Page ;
51+ // Remove the original textbox field
52+ form . Fields . Remove ( signatureTextBox ) ;
53+ // Create a new signature field at the same location
54+ PdfSignatureField signatureField = new PdfSignatureField ( page , "ClientSignature" )
10355 {
104- //Save the PDF document to file stream.
105- loadedDocument . Save ( outputFileStream ) ;
106- }
107-
108- //Close the document.
109- loadedDocument . Close ( true ) ;
56+ Bounds = bounds
57+ } ;
58+ // Add the new signature field to the form
59+ form . Fields . Add ( signatureField ) ;
11060 }
61+ // Save the PDF document
62+ loadedDocument . Save ( Path . GetFullPath ( @"Output/Output2.pdf" ) ) ;
11163 }
11264}
0 commit comments