|
1 |
| -using Syncfusion.Pdf.Graphics; |
2 |
| -using Syncfusion.Pdf; |
3 |
| -using Syncfusion.Drawing; |
| 1 | +using Syncfusion.Drawing; |
4 | 2 | using Syncfusion.HtmlConverter;
|
5 |
| -using Syncfusion.Pdf.Interactive; |
| 3 | +using Syncfusion.Pdf; |
6 | 4 | using Syncfusion.Pdf.Parsing;
|
| 5 | +using Syncfusion.Pdf.Interactive; |
7 | 6 |
|
8 |
| -namespace Create_PDF |
| 7 | +internal class Program |
9 | 8 | {
|
10 |
| - internal class Program |
| 9 | + static void Main(string[] args) |
11 | 10 | {
|
12 |
| - static void Main(string[] args) |
| 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 |
13 | 16 | {
|
14 |
| - //Initialize the HTML to PDF converter. |
15 |
| - HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); |
| 17 | + EnableForm = true // Ensures form elements like <input>, <textarea> are converted to PDF fields |
| 18 | + }; |
| 19 | + htmlConverter.ConverterSettings = settings; |
16 | 20 |
|
17 |
| - BlinkConverterSettings settings = new BlinkConverterSettings(); |
18 |
| - //Set enable form |
19 |
| - settings.EnableForm = true; |
20 |
| - //Assign Blink converter settings to HTML converter |
21 |
| - htmlConverter.ConverterSettings = settings; |
| 21 | + // Convert the HTML file to a PDF document |
| 22 | + PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Test.html")); |
22 | 23 |
|
23 |
| - //Convert HTML to PDF |
24 |
| - PdfDocument document = htmlConverter.Convert(Path.GetFullPath("Data/Test.html")); |
25 |
| - document.Form.SetDefaultAppearance(false); |
26 |
| - using (MemoryStream stream = new MemoryStream()) |
27 |
| - { |
28 |
| - document.Save(stream); |
29 |
| - stream.Position = 0; |
30 |
| - document.Close(true); |
31 |
| - //Add signature field in PDF. |
32 |
| - AddSignature(stream); |
33 |
| - } |
| 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 |
34 | 33 |
|
| 34 | + // Replace the "signature" textarea with an actual signature field |
| 35 | + AddPdfSignatureField(stream); |
35 | 36 | }
|
36 |
| - /// <summary> |
37 |
| - /// Adds signature field in PDF |
38 |
| - /// </summary> |
39 |
| - /// <param name="stream"></param> |
40 |
| - static void AddSignature(MemoryStream stream) |
| 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)) |
41 | 47 | {
|
42 |
| - //Load the PDF document. |
43 |
| - PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); |
44 |
| - //Get the loaded form. |
45 | 48 | PdfLoadedForm loadedForm = loadedDocument.Form;
|
46 | 49 |
|
47 |
| - List<PdfSignatureField> signatureFields = new List<PdfSignatureField>(); |
48 |
| - |
49 |
| - for (int i = loadedForm.Fields.Count - 1; i >= 0; i--) |
| 50 | + // Check for a field named "signature" |
| 51 | + if (loadedForm.Fields["signature"] is PdfLoadedTextBoxField signatureTextBox) |
50 | 52 | {
|
| 53 | + // Get the original field's position and page |
| 54 | + RectangleF bounds = signatureTextBox.Bounds; |
| 55 | + PdfPageBase page = signatureTextBox.Page; |
51 | 56 |
|
52 |
| - if (loadedForm.Fields[i] is PdfLoadedTextBoxField) |
53 |
| - { |
54 |
| - //Get the loaded text box field and fill it. |
55 |
| - PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField; |
56 |
| - |
57 |
| - if (loadedTextBoxField.Name.Contains("textarea")) |
58 |
| - { |
59 |
| - //Get bounds from an existing textbox field. |
60 |
| - RectangleF bounds = loadedTextBoxField.Bounds; |
61 |
| - |
62 |
| - //Get page. |
63 |
| - PdfPageBase loadedPage = loadedTextBoxField.Page; |
64 |
| - |
65 |
| - //Create PDF Signature field. |
66 |
| - PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Name.Trim()); |
| 57 | + // Remove the original textbox field |
| 58 | + loadedForm.Fields.Remove(signatureTextBox); |
67 | 59 |
|
68 |
| - //Set properties to the signature field. |
69 |
| - signatureField.Bounds = bounds; |
70 |
| - |
71 |
| - //Add the form field to the document. |
72 |
| - signatureFields.Add(signatureField); |
| 60 | + // Create a new signature field at the same location |
| 61 | + PdfSignatureField signatureField = new PdfSignatureField(page, "ClientSignature") |
| 62 | + { |
| 63 | + Bounds = bounds |
| 64 | + }; |
73 | 65 |
|
74 |
| - loadedForm.Fields.Remove(loadedTextBoxField); |
75 |
| - } |
76 |
| - } |
| 66 | + // Add the new signature field to the form |
| 67 | + loadedForm.Fields.Add(signatureField); |
77 | 68 | }
|
78 |
| - foreach (PdfSignatureField signature in signatureFields) |
79 |
| - { |
80 |
| - loadedForm.Fields.Add(signature); |
81 |
| - } |
82 |
| - //Save the document. |
83 |
| - using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) |
| 69 | + |
| 70 | + // Save the modified document to disk |
| 71 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) |
84 | 72 | {
|
85 |
| - loadedDocument.Save(outputStream1); |
| 73 | + loadedDocument.Save(outputStream); |
86 | 74 | }
|
| 75 | + // Close the document and release resources |
87 | 76 | loadedDocument.Close(true);
|
88 | 77 | }
|
89 | 78 | }
|
|
0 commit comments