Skip to content

948077 Added signature field in the PDF converted from HTML #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A85F0E8-3D3C-4F45-8184-FB3B76BBC9BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Adding_signature_field_to_PDF_converted_from_HTML</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Form Template - Signature</title>
</head>
<body>
<form method="POST">
<h3>Sample Form Data</h3>
<table style="border: solid 1px red; margin: 5px" cellpadding="5px">
<tr>
<td>Signature:</td>
<td>
<textarea id="signature" name="signature" placeholder="Sign here..."
style="background-color:LightCyan;border:1px solid Gray;height:50px;width:200px;">
</textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Interactive;

internal class Program
{
static void Main(string[] args)
{
// Initialize the HTML to PDF converter using the Blink rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

// Configure the converter to preserve form fields in the PDF
BlinkConverterSettings settings = new BlinkConverterSettings
{
EnableForm = true // Ensures form elements like <input>, <textarea> are converted to PDF fields
};
htmlConverter.ConverterSettings = settings;

// Convert the HTML file to a PDF document
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Test.html"));

// Optional: Remove default appearances for form fields to match the page style
document.Form.SetDefaultAppearance(false);

// Save the PDF to a memory stream for further processing
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream); // Save converted PDF to memory
stream.Position = 0; // Reset stream position
document.Close(true); // Close the original document

// Replace the "signature" textarea with an actual signature field
AddPdfSignatureField(stream);
}
}

/// <summary>
/// Finds the "signature" field in the form, removes it, and replaces it with a true PDF signature field.
/// </summary>
/// <param name="stream">MemoryStream containing the PDF document</param>
static void AddPdfSignatureField(MemoryStream stream)
{
// Load the PDF document from memory stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream))
{
PdfLoadedForm loadedForm = loadedDocument.Form;

// Check for a field named "signature"
if (loadedForm.Fields["signature"] is PdfLoadedTextBoxField signatureTextBox)
{
// Get the original field's position and page
RectangleF bounds = signatureTextBox.Bounds;
PdfPageBase page = signatureTextBox.Page;

// Remove the original textbox field
loadedForm.Fields.Remove(signatureTextBox);

// Create a new signature field at the same location
PdfSignatureField signatureField = new PdfSignatureField(page, "ClientSignature")
{
Bounds = bounds
};

// Add the new signature field to the form
loadedForm.Fields.Add(signatureField);
}

// Save the modified document to disk
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
{
loadedDocument.Save(outputStream);
}
// Close the document and release resources
loadedDocument.Close(true);
}
}
}
Loading