Add simple html text to PDF

Generate PDF, Manipulate PDF
11/2/2011

Downloads

The SimpleXhtmlShape lets you add text with simple HTML mark-up to a PDF document.

add-html-text-to-PDF.png

Here is the code that uses the SimpleXhtmlShape:

C# code sample

1 // create a new document 2 Document document = new Document(); 3 4 // append a new page (portrait Letter) 5 bool landscape = false; 6 7 Page page = new Page(PageSize.Letter, landscape); 8 document.Pages.Add(page); 9 10 string xhtml = "<?xml version='1.0'?><body xfa:APIVersion=\"PDFKit:3.0.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">"; 11 xhtml += "<p>With a simple Xhtml shape you can easily specify <b>bold</b> text, <i>italic</i> text, or <b><i>both</i></b>. "; 12 xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>,"; 13 xhtml += "or a different <span style='font-family:courier'>font</span>, "; 14 xhtml += "<span style ='text-decoration:underline'>underline</span>, or "; 15 xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> "; 16 xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>"; 17 xhtml += "<p>Note furthermore that &lt;p&gt; tags can be used to generate separate paragraphs.</p>"; 18 xhtml += "</body>"; 19 20 // stamp a text paragraph at the center 21 22 TranslateTransform translate = new TranslateTransform(); 23 SimpleXhtmlShape xhtmlShape = new SimpleXhtmlShape(); 24 xhtmlShape.Text = xhtml; 25 xhtmlShape.Transform = translate; 26 27 xhtmlShape.DefaultTextIndent = 20; 28 // center horizontally 29 xhtmlShape.Width = page.Width / 2; 30 translate.X = (page.Width - xhtmlShape.Width) / 2; 31 32 // Y corresponds to the top of the paragraph 33 // Translate the shape so it is centered vertically 34 translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2; 35 36 page.VisualOverlay.Add(xhtmlShape); 37 38 using (FileStream file = new FileStream(@"..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write)) 39 { 40 document.Write(file); 41 }

VB.NET code sample

1 ' create a new document 2 Dim document As New Document() 3 4 ' append a new page (portrait Letter) 5 Dim landscape As Boolean = False 6 7 Dim page As New Page(PageSize.Letter, landscape) 8 document.Pages.Add(page) 9 10 Dim xhtml As String = "<?xml version='1.0'?><body xfa:APIVersion=""PDFKit:3.0.0.0"" xfa:spec=""2.1"" xmlns=""http://www.w3.org/1999/xhtml"" xmlns:xfa=""http://www.xfa.org/schema/xfa-data/1.0/"">" 11 xhtml += "<p>With a simple Xhtml shape you can easily specify <b>bold</b> text, <i>italic</i> text, or <b><i>both</i></b>. " 12 xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>," 13 xhtml += "or a different <span style='font-family:courier'>font</span>, " 14 xhtml += "<span style ='text-decoration:underline'>underline</span>, or " 15 xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> " 16 xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>" 17 xhtml += "<p>Note furthermore that &lt;p&gt; tags can be used to generate separate paragraphs.</p>" 18 xhtml += "</body>" 19 20 ' stamp a text paragraph at the center 21 22 Dim translate As New TranslateTransform() 23 Dim xhtmlShape As New Shapes.SimpleXhtmlShape 24 xhtmlShape.Text = xhtml 25 xhtmlShape.Transform = translate 26 27 xhtmlShape.DefaultTextIndent = 20 28 ' center horizontally 29 xhtmlShape.Width = page.Width / 2 30 translate.X = (page.Width - xhtmlShape.Width) / 2 31 32 ' Y corresponds to the top of the paragraph 33 ' Translate the shape so it is centered vertically 34 translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2 35 36 page.VisualOverlay.Add(xhtmlShape) 37 38 Using file As New FileStream("..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write) 39 document.Write(file) 40 End Using