Skip to content

Commit 53c1a12

Browse files
Merge pull request #151 from SyncfusionExamples/948077
948077 Added signature field in the PDF converted from HTML
2 parents e58574f + d6a1bed commit 53c1a12

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adding-signature-field-to-PDF-converted-from-HTML", "Adding-signature-field-to-PDF-converted-from-HTML\Adding-signature-field-to-PDF-converted-from-HTML.csproj", "{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Adding_signature_field_to_PDF_converted_from_HTML</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Form Template - Signature</title>
6+
</head>
7+
<body>
8+
<form method="POST">
9+
<h3>Sample Form Data</h3>
10+
<table style="border: solid 1px red; margin: 5px" cellpadding="5px">
11+
<tr>
12+
<td>Signature:</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>
18+
</tr>
19+
</table>
20+
</form>
21+
</body>
22+
</html>

HTML to PDF/Blink/Adding-signature-field-to-PDF-converted-from-HTML/.NET/Adding-signature-field-to-PDF-converted-from-HTML/Output/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)