Skip to content

Commit d6a1bed

Browse files
committed
948077 Resolved given feedback.
1 parent 3d8838e commit d6a1bed

File tree

3 files changed

+63
-88
lines changed
  • HTML to PDF/Blink/Adding-signature-field-to-PDF-converted-from-HTML/.NET/Adding-signature-field-to-PDF-converted-from-HTML

3 files changed

+63
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
<!DOCTYPE html>
2-
32
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
43
<head>
54
<meta charset="utf-8" />
6-
<title>Test Document</title>
5+
<title>Form Template - Signature</title>
76
</head>
87
<body>
98
<form method="POST">
10-
Sample Report Data: <br />
9+
<h3>Sample Form Data</h3>
1110
<table style="border: solid 1px red; margin: 5px" cellpadding="5px">
12-
<tr>
13-
<td>Field 1:</td>
14-
<td><input type="text" id="field1" name="field1" value="FIELD1VALUE" /></td>
15-
</tr>
16-
<tr>
17-
<td>Field 2:</td>
18-
<td><input type="text" id="Text2" value="FIELD2VALUE" /></td>
19-
</tr>
20-
<tr>
21-
<td>Field 3:</td>
22-
<td><input type="text" id="Text3" value="FIELD3VALUE" /></td>
23-
</tr>
24-
<tr>
25-
<td>Field 4:</td>
26-
<td><input type="text" id="Text4" value="FIELD4VALUE" /></td>
27-
</tr>
2811
<tr>
2912
<td>Signature:</td>
30-
<td><textarea id="ClientSignature" style="background-color:LightCyan;border-color:Gray;border-width:1px;border-style:Solid;height:50px;width:200px;abcpdf-tag-visible: true"
31-
rows="2" cols="20"></textarea></td>
13+
<td>
14+
<textarea id="signature" name="signature" placeholder="Sign here..."
15+
style="background-color:LightCyan;border:1px solid Gray;height:50px;width:200px;">
16+
</textarea>
17+
</td>
3218
</tr>
33-
</table>
19+
</table>
3420
</form>
3521
</body>
3622
</html>

HTML to PDF/Blink/Adding-signature-field-to-PDF-converted-from-HTML/.NET/Adding-signature-field-to-PDF-converted-from-HTML/Program.cs

+55-66
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,78 @@
1-
using Syncfusion.Pdf.Graphics;
2-
using Syncfusion.Pdf;
3-
using Syncfusion.Drawing;
1+
using Syncfusion.Drawing;
42
using Syncfusion.HtmlConverter;
5-
using Syncfusion.Pdf.Interactive;
3+
using Syncfusion.Pdf;
64
using Syncfusion.Pdf.Parsing;
5+
using Syncfusion.Pdf.Interactive;
76

8-
namespace Create_PDF
7+
internal class Program
98
{
10-
internal class Program
9+
static void Main(string[] args)
1110
{
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
1316
{
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;
1620

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"));
2223

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
3433

34+
// Replace the "signature" textarea with an actual signature field
35+
AddPdfSignatureField(stream);
3536
}
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))
4147
{
42-
//Load the PDF document.
43-
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
44-
//Get the loaded form.
4548
PdfLoadedForm loadedForm = loadedDocument.Form;
4649

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)
5052
{
53+
// Get the original field's position and page
54+
RectangleF bounds = signatureTextBox.Bounds;
55+
PdfPageBase page = signatureTextBox.Page;
5156

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);
6759

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+
};
7365

74-
loadedForm.Fields.Remove(loadedTextBoxField);
75-
}
76-
}
66+
// Add the new signature field to the form
67+
loadedForm.Fields.Add(signatureField);
7768
}
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))
8472
{
85-
loadedDocument.Save(outputStream1);
73+
loadedDocument.Save(outputStream);
8674
}
75+
// Close the document and release resources
8776
loadedDocument.Close(true);
8877
}
8978
}

0 commit comments

Comments
 (0)