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