Skip to content
Merged
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
25 changes: 25 additions & 0 deletions HTML to PDF/Blink/HTMLtoPDF_Hyperlink/.NET/HTMLtoPDF_Hyperlink.sln
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.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>
Original file line number Diff line number Diff line change
@@ -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 = @"<!DOCTYPE html>
<html>
<head>
<title>Text Formatting Example</title>
</head>
<body>
<p>Generic items.</p>
<p><b>Bold</b></p>
<p><i><b>Italic and Bold</b></i></p>
<p><i><b><s>Italic and Bold And Strikethrough</s></b></i></p>
<p><i><b><s><u>Italic and Bold and strikethrough and underlined</u></s></b></i></p>
<a href=""https://www.google.com/"">Google</a>
</body>
</html>";

// 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<int, List<PdfLoadedAnnotation>> hyperlinkAnnotations = new Dictionary<int, List<PdfLoadedAnnotation>>();

for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
PdfLoadedAnnotationCollection annotations = loadedDocument.Pages[i].Annotations;
List<PdfLoadedAnnotation> pageLinks = new List<PdfLoadedAnnotation>();

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"));
}
}
}
}
}
}
Loading