1
+ using Syncfusion . Drawing ;
2
+ using Syncfusion . HtmlConverter ;
3
+ using Syncfusion . Pdf ;
4
+ using Syncfusion . Pdf . Parsing ;
5
+ using Syncfusion . Pdf . Interactive ;
6
+
7
+ internal class Program
8
+ {
9
+ static void Main ( string [ ] args )
10
+ {
11
+ // Initialize the HTML to PDF converter using the Blink rendering engine
12
+ HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter ( ) ;
13
+
14
+ // Configure the converter to preserve form fields in the PDF
15
+ BlinkConverterSettings settings = new BlinkConverterSettings
16
+ {
17
+ EnableForm = true // Ensures form elements like <input>, <textarea> are converted to PDF fields
18
+ } ;
19
+ htmlConverter . ConverterSettings = settings ;
20
+
21
+ // Convert the HTML file to a PDF document
22
+ PdfDocument document = htmlConverter . Convert ( Path . GetFullPath ( @"Data/Test.html" ) ) ;
23
+
24
+ // Optional: Remove default appearances for form fields to match the page style
25
+ document . Form . SetDefaultAppearance ( false ) ;
26
+
27
+ // Save the PDF to a memory stream for further processing
28
+ using ( MemoryStream stream = new MemoryStream ( ) )
29
+ {
30
+ document . Save ( stream ) ; // Save converted PDF to memory
31
+ stream . Position = 0 ; // Reset stream position
32
+ document . Close ( true ) ; // Close the original document
33
+
34
+ // Replace the "signature" textarea with an actual signature field
35
+ AddPdfSignatureField ( stream ) ;
36
+ }
37
+ }
38
+
39
+ /// <summary>
40
+ /// Finds the "signature" field in the form, removes it, and replaces it with a true PDF signature field.
41
+ /// </summary>
42
+ /// <param name="stream">MemoryStream containing the PDF document</param>
43
+ static void AddPdfSignatureField ( MemoryStream stream )
44
+ {
45
+ // Load the PDF document from memory stream
46
+ using ( PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( stream ) )
47
+ {
48
+ PdfLoadedForm loadedForm = loadedDocument . Form ;
49
+
50
+ // Check for a field named "signature"
51
+ if ( loadedForm . Fields [ "signature" ] is PdfLoadedTextBoxField signatureTextBox )
52
+ {
53
+ // Get the original field's position and page
54
+ RectangleF bounds = signatureTextBox . Bounds ;
55
+ PdfPageBase page = signatureTextBox . Page ;
56
+
57
+ // Remove the original textbox field
58
+ loadedForm . Fields . Remove ( signatureTextBox ) ;
59
+
60
+ // Create a new signature field at the same location
61
+ PdfSignatureField signatureField = new PdfSignatureField ( page , "ClientSignature" )
62
+ {
63
+ Bounds = bounds
64
+ } ;
65
+
66
+ // Add the new signature field to the form
67
+ loadedForm . Fields . Add ( signatureField ) ;
68
+ }
69
+
70
+ // Save the modified document to disk
71
+ using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Output.pdf" ) , FileMode . Create , FileAccess . Write ) )
72
+ {
73
+ loadedDocument . Save ( outputStream ) ;
74
+ }
75
+ // Close the document and release resources
76
+ loadedDocument . Close ( true ) ;
77
+ }
78
+ }
79
+ }
0 commit comments