Skip to content

Commit 9cb20a8

Browse files
committed
968996: Added filled form fields from HTML to PDF.
1 parent 6a9a85f commit 9cb20a8

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
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+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FC5888A1-82B1-4C5C-AA60-36BBCD924490}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!-- template.html -->
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<style>
6+
body {
7+
font-size: 18px; /* Increase base font size */
8+
min-height: 100vh;
9+
margin: 0;
10+
display: flex;
11+
align-items: center;
12+
justify-content: center;
13+
background: #fff; /* Set background to white */
14+
font-family: Arial, sans-serif;
15+
}
16+
.center-rectangle {
17+
background: #fff;
18+
border: 2.5px solid #313163;
19+
border-radius: 12px;
20+
padding: 48px 62px 46px 62px;
21+
min-width: 420px;
22+
max-width: 650px;
23+
box-shadow: 0 4px 30px rgba(49,49,99,0.10);
24+
display: flex;
25+
flex-direction: column;
26+
align-items: center;
27+
}
28+
.center-rectangle h1 {
29+
font-size: 2.8em; /* Big headline */
30+
font-weight: 800;
31+
margin-top: 0;
32+
margin-bottom: 38px;
33+
color: #313163;
34+
letter-spacing: 1.4px;
35+
}
36+
.center-rectangle p {
37+
font-size: 1.5em; /* Larger paragraph text */
38+
margin: 22px 0 0 0;
39+
width: 100%;
40+
text-align: left;
41+
color: #222;
42+
font-weight: 500;
43+
letter-spacing: 0.7px;
44+
}
45+
.center-rectangle p:last-child {
46+
margin-top: 56px;
47+
text-align: center;
48+
color: #757575;
49+
font-size: 1.23em;
50+
font-weight: 400;
51+
}
52+
</style>
53+
</head>
54+
<body>
55+
<div class="center-rectangle">
56+
<h1>Welcome, {{name}}</h1>
57+
<p>Date: {{date}}</p>
58+
<p>Signature: {{signature}}</p>
59+
<p>Thank you for using our service.</p>
60+
</div>
61+
</body>
62+
</html>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Fillable_form_fields_from_HTML_to_PDF_Converter</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
13+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
14+
</ItemGroup>
15+
16+
</Project>

HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Output/gitkeep.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using Syncfusion.HtmlConverter;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Interactive;
5+
using Syncfusion.Pdf.Parsing;
6+
using Syncfusion.Pdf.Redaction;
7+
using Syncfusion.Pdf.Security;
8+
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
// Initialize HTML to PDF converter and load HTML
14+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
15+
string htmlFilePath = Path.GetFullPath(@"Data/Input.html");
16+
PdfDocument document = htmlConverter.Convert(htmlFilePath);
17+
18+
// Save the PDF to a memory stream
19+
using (MemoryStream memoryStream = new MemoryStream())
20+
{
21+
document.Save(memoryStream);
22+
document.Close(true);
23+
24+
// Load back the PDF for further processing
25+
memoryStream.Position = 0;
26+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(memoryStream);
27+
28+
// This will collect (pageIndex, word) for each form field
29+
List<(int pageIdx, TextWord word)> fieldData = new List<(int pageIdx, TextWord word)>();
30+
31+
// Pass 1: Locate each placeholder and add a redaction on its bound
32+
for (int i = 0; i < loadedDocument.Pages.Count; i++)
33+
{
34+
PdfLoadedPage page = loadedDocument.Pages[i] as PdfLoadedPage;
35+
page.ExtractText(out TextLineCollection lines);
36+
if (lines == null) continue;
37+
foreach (TextLine line in lines.TextLine)
38+
{
39+
foreach (TextWord word in line.WordCollection)
40+
{
41+
if (word == null) continue;
42+
if (word.Text == "{{name}}" ||
43+
word.Text == "{{date}}" ||
44+
word.Text == "{{signature}}")
45+
{
46+
page.AddRedaction(new PdfRedaction(word.Bounds));
47+
fieldData.Add((i, word));
48+
}
49+
}
50+
}
51+
}
52+
loadedDocument.Redact();
53+
54+
// Pass 2: Add form fields exactly over the bounds
55+
foreach (var (pageIdx, word) in fieldData)
56+
{
57+
PdfPageBase page = loadedDocument.Pages[pageIdx];
58+
59+
if (word.Text == "{{name}}")
60+
{
61+
PdfTextBoxField textBox = new PdfTextBoxField(page, "FirstName")
62+
{
63+
Bounds = word.Bounds,
64+
ToolTip = "First Name",
65+
Text = "John"
66+
};
67+
loadedDocument.Form.Fields.Add(textBox);
68+
}
69+
else if (word.Text == "{{date}}")
70+
{
71+
PdfTextBoxField dateField = new PdfTextBoxField(page, "DateField")
72+
{
73+
Bounds = word.Bounds
74+
};
75+
dateField.Actions.KeyPressed = new PdfJavaScriptAction("AFDate_KeystrokeEx(\"m/d/yy\")");
76+
dateField.Actions.Format = new PdfJavaScriptAction("AFDate_FormatEx(\"m/d/yy\")");
77+
loadedDocument.Form.Fields.Add(dateField);
78+
}
79+
else if (word.Text == "{{signature}}")
80+
{
81+
PdfSignatureField sigField = new PdfSignatureField(page, "SignatureField")
82+
{
83+
Bounds = word.Bounds,
84+
Signature = new PdfSignature()
85+
};
86+
// Optionally draw a signature image in the field area
87+
FileStream imageStream = new FileStream(Path.GetFullPath("Data/signature.png"), FileMode.Open, FileAccess.Read);
88+
PdfBitmap image = new PdfBitmap(imageStream);
89+
(page as PdfLoadedPage).Graphics.DrawImage(image, word.Bounds);
90+
imageStream.Dispose();
91+
92+
// Optional: add digital certificate
93+
using (FileStream certStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read))
94+
{
95+
sigField.Signature.Certificate = new PdfCertificate(certStream, "syncfusion");
96+
sigField.Signature.Reason = "I am author of this document";
97+
}
98+
loadedDocument.Form.Fields.Add(sigField);
99+
}
100+
}
101+
//Create file stream.
102+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
103+
{
104+
//Save the PDF document to file stream.
105+
loadedDocument.Save(outputFileStream);
106+
}
107+
108+
//Close the document.
109+
loadedDocument.Close(true);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)