From e510a8518ccd9c1179810f5dda0995efee1b47e3 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Mon, 13 Oct 2025 11:38:30 +0530 Subject: [PATCH] 986214: Added sample project for HTML to PDF Hyperlink. --- .../.NET/HTMLtoPDF_Hyperlink.sln | 25 ++++ .../HTMLtoPDF_Hyperlink.csproj | 14 ++ .../HTMLtoPDF_Hyperlink/Output/gitkeep.txt | 0 .../.NET/HTMLtoPDF_Hyperlink/Program.cs | 124 ++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink.sln create mode 100644 HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj create mode 100644 HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Output/gitkeep.txt create mode 100644 HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Program.cs diff --git a/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink.sln b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink.sln new file mode 100644 index 00000000..9fb45621 --- /dev/null +++ b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36429.23 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HTMLtoPDF_Hyperlink", "HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj", "{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E7181677-046A-49B5-890B-C3910F8B8278} + EndGlobalSection +EndGlobal diff --git a/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj new file mode 100644 index 00000000..b6c32751 --- /dev/null +++ b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Output/gitkeep.txt b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Program.cs b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Program.cs new file mode 100644 index 00000000..f85d2a09 --- /dev/null +++ b/HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Program.cs @@ -0,0 +1,124 @@ +using Syncfusion.HtmlConverter; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Drawing; + +class Program +{ + static void Main() + { + // HTML content to be converted + string htmlContent = @" + + + Text Formatting Example + + +

Generic items.

+

Bold

+

Italic and Bold

+

Italic and Bold And Strikethrough

+

Italic and Bold and strikethrough and underlined

+ Google + +"; + + // Initialize the HTML to PDF converter + HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); + + // Configure Blink converter settings + BlinkConverterSettings blinkSettings = new BlinkConverterSettings + { + PdfPageSize = PdfPageSize.A4, + ViewPortSize = new Syncfusion.Drawing.Size(1280, 0), + Margin = new PdfMargins { Top = 0, Bottom = 0, Left = 0, Right = 0 }, + Orientation = PdfPageOrientation.Portrait, + EnableHyperLink = true, + EnableJavaScript = false, + EnableForm = false, + EnableOfflineMode = true, + EnableLocalFileAccess = true, + AdditionalDelay = 0 + }; + + htmlConverter.ConverterSettings = blinkSettings; + + // Convert HTML string to PDF document + using (PdfDocument document = htmlConverter.Convert(htmlContent, string.Empty)) + { + using (MemoryStream memoryStream = new MemoryStream()) + { + // Save the document to memory stream + document.Save(memoryStream); + document.Close(true); // Close and dispose the original document + + memoryStream.Position = 0; // Reset stream position for reading + + // Load the saved PDF from memory stream + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(memoryStream)) + { + loadedDocument.EnableMemoryOptimization = true; + + // Dictionary to store hyperlink annotations by page index + Dictionary> hyperlinkAnnotations = new Dictionary>(); + + for (int i = 0; i < loadedDocument.Pages.Count; i++) + { + PdfLoadedAnnotationCollection annotations = loadedDocument.Pages[i].Annotations; + List pageLinks = new List(); + + foreach (PdfLoadedAnnotation annotation in annotations) + { + if (annotation.Type == PdfLoadedAnnotationType.TextWebLinkAnnotation) + { + pageLinks.Add(annotation); + } + } + + hyperlinkAnnotations[i] = pageLinks; + } + + // Create a new PDF document to copy pages and annotations + using (PdfDocument finalDocument = new PdfDocument()) + { + for (int i = 0; i < loadedDocument.Pages.Count; i++) + { + PdfPageBase sourcePage = loadedDocument.Pages[i]; + PdfPage newPage = finalDocument.Pages.Add(); + + // Copy content from source page + newPage.Graphics.DrawPdfTemplate(sourcePage.CreateTemplate(), PointF.Empty); + + // Reapply hyperlink annotations + if (hyperlinkAnnotations.TryGetValue(i, out var annotations)) + { + foreach (PdfLoadedAnnotation annotation in annotations) + { + if (annotation is PdfLoadedTextWebLinkAnnotation linkAnnotation) + { + PdfUriAnnotation uriAnnotation = new PdfUriAnnotation(annotation.Bounds, linkAnnotation.Url) + { + Text = linkAnnotation.Text, + Border = new PdfAnnotationBorder + { + Width = 0, + VerticalRadius = 0, + HorizontalRadius = 0 + } + }; + + newPage.Annotations.Add(uriAnnotation); + } + } + } + } + // Save the final PDF to file + finalDocument.Save(Path.GetFullPath(@"Output/Output.pdf")); + } + } + } + } + } +} \ No newline at end of file