Skip to content

968996: Added filled form fields from HTML to PDF. #189

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 1 commit 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,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36221.1 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fillable-form-fields-from-HTML-to-PDF-Converter", "Fillable-form-fields-from-HTML-to-PDF-Converter\Fillable-form-fields-from-HTML-to-PDF-Converter.csproj", "{70A501A7-093A-4E77-AE84-1EDFE2A5243F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FC5888A1-82B1-4C5C-AA60-36BBCD924490}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!-- template.html -->
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-size: 18px; /* Increase base font size */
min-height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
background: #fff; /* Set background to white */
font-family: Arial, sans-serif;
}
.center-rectangle {
background: #fff;
border: 2.5px solid #313163;
border-radius: 12px;
padding: 48px 62px 46px 62px;
min-width: 420px;
max-width: 650px;
box-shadow: 0 4px 30px rgba(49,49,99,0.10);
display: flex;
flex-direction: column;
align-items: center;
}
.center-rectangle h1 {
font-size: 2.8em; /* Big headline */
font-weight: 800;
margin-top: 0;
margin-bottom: 38px;
color: #313163;
letter-spacing: 1.4px;
}
.center-rectangle p {
font-size: 1.5em; /* Larger paragraph text */
margin: 22px 0 0 0;
width: 100%;
text-align: left;
color: #222;
font-weight: 500;
letter-spacing: 0.7px;
}
.center-rectangle p:last-child {
margin-top: 56px;
text-align: center;
color: #757575;
font-size: 1.23em;
font-weight: 400;
}
</style>
</head>
<body>
<div class="center-rectangle">
<h1>Welcome, {{name}}</h1>
<p>Date: {{date}}</p>
<p>Signature: {{signature}}</p>
<p>Thank you for using our service.</p>
</div>
</body>
</html>
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;
using Syncfusion.Pdf.Security;

class Program
{
static void Main(string[] args)
{
// Initialize HTML to PDF converter and load HTML
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
string htmlFilePath = Path.GetFullPath(@"Data/Input.html");
PdfDocument document = htmlConverter.Convert(htmlFilePath);

// Save the PDF to a memory stream
using (MemoryStream memoryStream = new MemoryStream())
{
document.Save(memoryStream);
document.Close(true);

// Load back the PDF for further processing
memoryStream.Position = 0;
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(memoryStream);

// This will collect (pageIndex, word) for each form field
List<(int pageIdx, TextWord word)> fieldData = new List<(int pageIdx, TextWord word)>();

// Pass 1: Locate each placeholder and add a redaction on its bound
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
PdfLoadedPage page = loadedDocument.Pages[i] as PdfLoadedPage;
page.ExtractText(out TextLineCollection lines);
if (lines == null) continue;
foreach (TextLine line in lines.TextLine)
{
foreach (TextWord word in line.WordCollection)
{
if (word == null) continue;
if (word.Text == "{{name}}" ||
word.Text == "{{date}}" ||
word.Text == "{{signature}}")
{
page.AddRedaction(new PdfRedaction(word.Bounds));
fieldData.Add((i, word));
}
}
}
}
loadedDocument.Redact();

// Pass 2: Add form fields exactly over the bounds
foreach (var (pageIdx, word) in fieldData)
{
PdfPageBase page = loadedDocument.Pages[pageIdx];

if (word.Text == "{{name}}")
{
PdfTextBoxField textBox = new PdfTextBoxField(page, "FirstName")
{
Bounds = word.Bounds,
ToolTip = "First Name",
Text = "John"
};
loadedDocument.Form.Fields.Add(textBox);
}
else if (word.Text == "{{date}}")
{
PdfTextBoxField dateField = new PdfTextBoxField(page, "DateField")
{
Bounds = word.Bounds
};
dateField.Actions.KeyPressed = new PdfJavaScriptAction("AFDate_KeystrokeEx(\"m/d/yy\")");
dateField.Actions.Format = new PdfJavaScriptAction("AFDate_FormatEx(\"m/d/yy\")");
loadedDocument.Form.Fields.Add(dateField);
}
else if (word.Text == "{{signature}}")
{
PdfSignatureField sigField = new PdfSignatureField(page, "SignatureField")
{
Bounds = word.Bounds,
Signature = new PdfSignature()
};
// Optionally draw a signature image in the field area
FileStream imageStream = new FileStream(Path.GetFullPath("Data/signature.png"), FileMode.Open, FileAccess.Read);
PdfBitmap image = new PdfBitmap(imageStream);
(page as PdfLoadedPage).Graphics.DrawImage(image, word.Bounds);
imageStream.Dispose();

// Optional: add digital certificate
using (FileStream certStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read))
{
sigField.Signature.Certificate = new PdfCertificate(certStream, "syncfusion");
sigField.Signature.Reason = "I am author of this document";
}
loadedDocument.Form.Fields.Add(sigField);
}
}
//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
loadedDocument.Save(outputFileStream);
}

//Close the document.
loadedDocument.Close(true);
}
}
}
Loading