diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter.sln b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter.sln new file mode 100644 index 00000000..740f8b7e --- /dev/null +++ b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter.sln @@ -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 diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/Input.html b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/Input.html new file mode 100644 index 00000000..a0626e34 --- /dev/null +++ b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/Input.html @@ -0,0 +1,62 @@ + + + + + + + +
+

Welcome, {{name}}

+

Date: {{date}}

+

Signature: {{signature}}

+

Thank you for using our service.

+
+ + \ No newline at end of file diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/PDF.pfx b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/PDF.pfx new file mode 100644 index 00000000..b50733ed Binary files /dev/null and b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/PDF.pfx differ diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/signature.png b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/signature.png new file mode 100644 index 00000000..e700dd88 Binary files /dev/null and b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Data/signature.png differ diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Fillable-form-fields-from-HTML-to-PDF-Converter.csproj b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Fillable-form-fields-from-HTML-to-PDF-Converter.csproj new file mode 100644 index 00000000..391fd257 --- /dev/null +++ b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Fillable-form-fields-from-HTML-to-PDF-Converter.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + Fillable_form_fields_from_HTML_to_PDF_Converter + enable + enable + + + + + + + + diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Output/gitkeep.txt b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Program.cs b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Program.cs new file mode 100644 index 00000000..94805675 --- /dev/null +++ b/HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Program.cs @@ -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); + } + } +} \ No newline at end of file