Add simple html text to PDF
Add simple html text to PDF
The SimpleXhtmlShape lets you add text with simple HTML mark-up to a PDF document.
Here is the code that uses the SimpleXhtmlShape:
// create a new document
Document document = new Document();
// append a new page (portrait Letter)
bool landscape = false;
Page page = new Page(PageSize.Letter, landscape);
document.Pages.Add(page);
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/\">";
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>. ";
xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>,";
xhtml += "or a different <span style='font-family:courier'>font</span>, ";
xhtml += "<span style ='text-decoration:underline'>underline</span>, or ";
xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> ";
xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>";
xhtml += "<p>Note furthermore that <p> tags can be used to generate separate paragraphs.</p>";
xhtml += "</body>";
// stamp a text paragraph at the center
TranslateTransform translate = new TranslateTransform();
SimpleXhtmlShape xhtmlShape = new SimpleXhtmlShape();
xhtmlShape.Text = xhtml;
xhtmlShape.Transform = translate;
xhtmlShape.DefaultTextIndent = 20;
// center horizontally
xhtmlShape.Width = page.Width / 2;
translate.X = (page.Width - xhtmlShape.Width) / 2;
// Y corresponds to the top of the paragraph
// Translate the shape so it is centered vertically
translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2;
page.VisualOverlay.Add(xhtmlShape);
using (FileStream file = new FileStream(@"..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write))
{
document.Write(file);
}
' create a new document
Dim document As New Document()
' append a new page (portrait Letter)
Dim landscape As Boolean = False
Dim page As New Page(PageSize.Letter, landscape)
document.Pages.Add(page)
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/"">"
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>. "
xhtml += "It is also possible to specify a different <span style='color:#ff0000'>color</span>,"
xhtml += "or a different <span style='font-family:courier'>font</span>, "
xhtml += "<span style ='text-decoration:underline'>underline</span>, or "
xhtml += "<span style ='text-decoration:line-through'>line-through</span>.</p> "
xhtml += "<p>Of course, text can also be <span style='font-size:72'>big</span>.</p>"
xhtml += "<p>Note furthermore that <p> tags can be used to generate separate paragraphs.</p>"
xhtml += "</body>"
' stamp a text paragraph at the center
Dim translate As New TranslateTransform()
Dim xhtmlShape As New Shapes.SimpleXhtmlShape
xhtmlShape.Text = xhtml
xhtmlShape.Transform = translate
xhtmlShape.DefaultTextIndent = 20
' center horizontally
xhtmlShape.Width = page.Width / 2
translate.X = (page.Width - xhtmlShape.Width) / 2
' Y corresponds to the top of the paragraph
' Translate the shape so it is centered vertically
translate.Y = page.Height / 2 + xhtmlShape.MeasuredHeight / 2
page.VisualOverlay.Add(xhtmlShape)
Using file As New FileStream("..\..\formattedtext.pdf", FileMode.Create, FileAccess.Write)
document.Write(file)
End Using