Skip to content

Commit b5c8d74

Browse files
authored
Merge pull request #205 from SyncfusionExamples/986214
986214: Added sample project for HTML to PDF Hyperlink.
2 parents 34156ec + e510a85 commit b5c8d74

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 25 additions & 0 deletions
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.36429.23 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HTMLtoPDF_Hyperlink", "HTMLtoPDF_Hyperlink/HTMLtoPDF_Hyperlink.csproj", "{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}"
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+
{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{CB0E8CB0-3C8E-4785-B13C-49B29C1696E5}.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 = {E7181677-046A-49B5-890B-C3910F8B8278}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>

HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Syncfusion.HtmlConverter;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Parsing;
5+
using Syncfusion.Pdf.Interactive;
6+
using Syncfusion.Drawing;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
// HTML content to be converted
13+
string htmlContent = @"<!DOCTYPE html>
14+
<html>
15+
<head>
16+
<title>Text Formatting Example</title>
17+
</head>
18+
<body>
19+
<p>Generic items.</p>
20+
<p><b>Bold</b></p>
21+
<p><i><b>Italic and Bold</b></i></p>
22+
<p><i><b><s>Italic and Bold And Strikethrough</s></b></i></p>
23+
<p><i><b><s><u>Italic and Bold and strikethrough and underlined</u></s></b></i></p>
24+
<a href=""https://www.google.com/"">Google</a>
25+
</body>
26+
</html>";
27+
28+
// Initialize the HTML to PDF converter
29+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
30+
31+
// Configure Blink converter settings
32+
BlinkConverterSettings blinkSettings = new BlinkConverterSettings
33+
{
34+
PdfPageSize = PdfPageSize.A4,
35+
ViewPortSize = new Syncfusion.Drawing.Size(1280, 0),
36+
Margin = new PdfMargins { Top = 0, Bottom = 0, Left = 0, Right = 0 },
37+
Orientation = PdfPageOrientation.Portrait,
38+
EnableHyperLink = true,
39+
EnableJavaScript = false,
40+
EnableForm = false,
41+
EnableOfflineMode = true,
42+
EnableLocalFileAccess = true,
43+
AdditionalDelay = 0
44+
};
45+
46+
htmlConverter.ConverterSettings = blinkSettings;
47+
48+
// Convert HTML string to PDF document
49+
using (PdfDocument document = htmlConverter.Convert(htmlContent, string.Empty))
50+
{
51+
using (MemoryStream memoryStream = new MemoryStream())
52+
{
53+
// Save the document to memory stream
54+
document.Save(memoryStream);
55+
document.Close(true); // Close and dispose the original document
56+
57+
memoryStream.Position = 0; // Reset stream position for reading
58+
59+
// Load the saved PDF from memory stream
60+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(memoryStream))
61+
{
62+
loadedDocument.EnableMemoryOptimization = true;
63+
64+
// Dictionary to store hyperlink annotations by page index
65+
Dictionary<int, List<PdfLoadedAnnotation>> hyperlinkAnnotations = new Dictionary<int, List<PdfLoadedAnnotation>>();
66+
67+
for (int i = 0; i < loadedDocument.Pages.Count; i++)
68+
{
69+
PdfLoadedAnnotationCollection annotations = loadedDocument.Pages[i].Annotations;
70+
List<PdfLoadedAnnotation> pageLinks = new List<PdfLoadedAnnotation>();
71+
72+
foreach (PdfLoadedAnnotation annotation in annotations)
73+
{
74+
if (annotation.Type == PdfLoadedAnnotationType.TextWebLinkAnnotation)
75+
{
76+
pageLinks.Add(annotation);
77+
}
78+
}
79+
80+
hyperlinkAnnotations[i] = pageLinks;
81+
}
82+
83+
// Create a new PDF document to copy pages and annotations
84+
using (PdfDocument finalDocument = new PdfDocument())
85+
{
86+
for (int i = 0; i < loadedDocument.Pages.Count; i++)
87+
{
88+
PdfPageBase sourcePage = loadedDocument.Pages[i];
89+
PdfPage newPage = finalDocument.Pages.Add();
90+
91+
// Copy content from source page
92+
newPage.Graphics.DrawPdfTemplate(sourcePage.CreateTemplate(), PointF.Empty);
93+
94+
// Reapply hyperlink annotations
95+
if (hyperlinkAnnotations.TryGetValue(i, out var annotations))
96+
{
97+
foreach (PdfLoadedAnnotation annotation in annotations)
98+
{
99+
if (annotation is PdfLoadedTextWebLinkAnnotation linkAnnotation)
100+
{
101+
PdfUriAnnotation uriAnnotation = new PdfUriAnnotation(annotation.Bounds, linkAnnotation.Url)
102+
{
103+
Text = linkAnnotation.Text,
104+
Border = new PdfAnnotationBorder
105+
{
106+
Width = 0,
107+
VerticalRadius = 0,
108+
HorizontalRadius = 0
109+
}
110+
};
111+
112+
newPage.Annotations.Add(uriAnnotation);
113+
}
114+
}
115+
}
116+
}
117+
// Save the final PDF to file
118+
finalDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)