How to convert and draw HTML objects at a specific position

You might already know how to convert HTML to PDF with the ImportHtml method of the PdfDocument class. In some case, you may need to not only convert HTML to PDF but also draw the resulting HTML at a specific position on the PDF page or even rotate the HTML 90 degrees. It’s very easy with the TranslateTransform and RotateTransform. Please see the following example code:

// Create a new PDF document. This object represents the PDF document.
// This document has one page, by default. Additional pages have to be added.
PdfDocument doc = new PdfDocument();

// Add a page to the document
PdfPage page = doc.Pages.Add();

// Options
bool keepHeight = false;
bool keepWidth = false;
bool javaScript = false;
bool autoDetectPageBreak = false;
bool hyperLink = false;
bool splitTextLines = false;
string url = "http://www.google.co.uk";

SizeF pageSize = page.GetClientSize();

PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = -1;
float height = -1;

//Calculates the height and width of the pdf image
AspectRatio dimension = AspectRatio.None;
if (keepHeight)
{
dimension = AspectRatio.KeepHeight;
height = convertor.ConvertToPixels(page.GetClientSize().Height, PdfGraphicsUnit.Point);
}
else if (keepWidth)
{
dimension = AspectRatio.KeepWidth;
width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
}

Html2PdfOptions options = new Html2PdfOptions();
options.EnableJavaScript = javaScript;
options.AutoDetectPageBreak = autoDetectPageBreak;
options.EnableHyperlinks = hyperLink;
options.Width = (int)width;
options.Height = (int)height;
options.SplitTextLines = splitTextLines;
options.AspectRatio = dimension;

PdfGraphics g = page.Graphics;

// Draw rotated objects
g.RotateTransform(45.0f);

// Translate to 20, 20
g.TranslateTransform(20, 20);

doc.ImportHtml(url, options);

// Save the PDF document to disk.
doc.Save("Html2Pdf.pdf");

// Use the following code to stream the document to the browser.
// pdfDoc.Save("Html2Pdf.pdf", Response, HttpResponseType.OpenInsideBrowser); // Response is an HttpResponse object.

// Close the document.
doc.Close();

Open a PDF document and create booklet

Booklets are documents with multiple pages arranged on sheets of paper that, when folded, presents the correct page order. For example, assume that you have a 13 page document. Creating a booklet of the document will result in a PDF file with 7 pages( (page 1, null), (page2, page13), (page3, page12), …((page7, page8).

Ultimate PDF provides support for creating booklets, which produces the resulting PDF document that can be printed and stapled in the center to make a booklet. PdfBookletUtil is used for creating Booklets.

The following code example illustrates how to create the Booklet.

C#:

// Load a PDF document.
PdfImportedDocument ldoc = new PdfImportedDocument("Sample.pdf");
// Specify the margin.
PdfMargins margin = new PdfMargins();
margin.All = 10;
// Create booklet with two sides.
PdfDocument doc = PdfBookletUtil.CreateBooklet(ldoc, new SizeF(500, 500), true, margin);
// Save the document.
doc.Save("Booklet.pdf");

VB.NET:

' Load a PDF document.
Dim ldoc As New PdfImportedDocument("Sample.pdf")
' Specify the margin.
Dim margin As New PdfMargins()
margin.All = 10
' Create booklet with two sides.
Dim doc As PdfDocument = PdfBookletUtil.CreateBooklet(ldoc, New SizeF(500, 500), True, margin)
' Save the document.
doc.Save("Booklet.pdf")

Last chance to order the Ultimate PDF with the competitive price

We are thrilled to annouce that a new version of the Ultimate PDF component is out in July with many improvements and new features. We have worked hard to deliver the high-quality products to our ATP, Inc.‘s division – ComponentPro customers. The new version is coming with a new pricing model in which the base price of the Ultimate PDF component is increased. Hurry up, it’s your last chance to order the component at an affordable price. Order now and enjoy your FREE Upgrade when we release the new version in July. The trial version of the component can be downloaded at our Download Page.

Drawing Text in PDF Document

There are many ways to draw texts with the UltimatePdf component. The following code example illustrates how to draw texts with each method.

1. Using DrawString

The format of the DrawString methods of the PdfGraphics are similar to the System.Drawing.Graphics.DrawString methods. It allows you to draw the specified text with the specified color, size, and font family.

The following code example illustrates this:

C#  
PdfImportedDocument doc = new PdfImportedDocument(fileName);
PdfPage page = doc.Pages.Add()
as PdfPage;
PdfGraphics g = page.Graphics;
PdfFont font =
new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
g.DrawString(
“My Text”, font, PdfBrushes.DarkBlue, new PointF(50, 0));
doc.Save(fileName);
doc.Close();
VB.NET  
Dim fileName As String = “c:test pdf.pdf”
Dim doc As New PdfImportedDocument(fileName)
Dim page As PdfPage = TryCast(doc.Pages.Add(), PdfPage)
Dim g As PdfGraphics = page.Graphics
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
g.DrawString(“My Text”, font, PdfBrushes.DarkBlue, New PointF(50, 0))
doc.Save(fileName)
doc.Close()

2. Paginating the text Area

To paginate a text, simply create a new instance of the PdfTextElement class and draw it in the PDF document. To configure the layout for your spanning text, use the PdfLayoutSettings class. For more details on spanning other elements, see this topic.

C#  
// Create a new PDF document. This object represents the PDF document.
// This document has one page, by default. Additional pages have to be added.
PdfDocument pdfDoc = new PdfDocument();
// Add a page to the document
PdfPage page = pdfDoc.Pages.Add();
PdfFont font =
new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
// Create a text element with large amount of text.
PdfTextElement element = new PdfTextElement(“Text Element”, font);
// Set the properties for the text element.
element.Brush = new PdfSolidBrush(Color.Black);
PdfStringFormat format =
new PdfStringFormat(PdfTextAlignment.Right);
// Set the string format. This can be used for setting unicode text.
element.StringFormat = format;
// Set the layout format properties to make the text flow through multiple pages.
PdfLayoutSettings layoutFormat = new PdfLayoutSettings();
layoutFormat.Break = PdfLayoutBreakType.FitPage;
layoutFormat.Layout = PdfLayoutType.Paginate;
// Set the bounds.
RectangleF bounds = new RectangleF(PointF.Empty, page.Graphics.ClientSize);
// Draw the text element.
element.Draw(page, bounds, layoutFormat);
// Save the PDF document to disk.
pdfDoc.Save(“Test.pdf”);
// Use the following code to stream the document to the browser.
// pdfDoc.Save(“Test.pdf”, Response, HttpResponseType.OpenInsideBrowser); // Response is an HttpResponse object.
// Close the document.
pdfDoc.Close();
VB.NET  
‘ Create a new PDF document. This object represents the PDF document.
‘ This document has one page, by default. Additional pages have to be added.
Dim pdfDoc As New PdfDocument()
‘ Add a page to the document
Dim page As PdfPage = pdfDoc.Pages.Add()
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
‘ Create a text element with large amount of text.
Dim element As New PdfTextElement(“Text Element”, font)
‘ Set the properties for the text element.
element.Brush = New PdfSolidBrush(Color.Black)
Dim format As New PdfStringFormat(PdfTextAlignment.Right)
‘ Set the string format. This can be used for setting unicode text.
element.StringFormat = format
‘ Set the layout format properties to make the text flow through multiple pages.
Dim layoutFormat As New PdfLayoutSettings()
layoutFormat.Break = PdfLayoutBreakType.FitPage
layoutFormat.Layout = PdfLayoutType.Paginate
‘ Set the bounds.
Dim bounds As New RectangleF(PointF.Empty, page.Graphics.ClientSize)
‘ Draw the text element.
element.Draw(page, bounds, layoutFormat)
‘ Save the PDF document to disk.
pdfDoc.Save(“Test.pdf”)
‘ Use the following code to stream the document to the browser.
‘ pdfDoc.Save(“Test.pdf”, Response, HttpResponseType.OpenInsideBrowser); // Response is an HttpResponse object.
‘ Close the document.
pdfDoc.Close()

3. String Formatting

For dedicated presentation of text in a PDF document, use a PdfStringFormat object. PdfStringFormat class provides support for the following features:

Formatting a text in a PDF document is very easy with the UltimatePdf component. By using the PdfStringFormat class, you can quickly set character spacing, word spacing, and line spacing. In addition, it also lets you format your text area with Center, Left, Right and Justify text alignments. You can also set Right-To-Left text align for languages such as Arabic, Hebrew, Urdu, and so on.

4. RTF Support

The UltimatePdf component fully supports converting RTF document to PDF document. A RTF text can either be converted to metafile image or raster image. To convert a RTF text, simply use the FromRtf methods of the PdfImage class.

Adding and removing a new page

Creating a page

Firstly, you need to load an existing document by using the PdfImportedDocument class and then add a new page to the loaded document by calling doc.Pages.Add method. The following example illustrates how to do that.

C#

PdfImportedDocument lDoc = new PdfImportedDocument("myfile.pdf");
PdfPage page = lDoc.Pages.Add() as PdfPage;
// Use the predefined fonts to draw the text.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10.0f, PdfFontStyle.Regular);
PdfGraphics g = page.Graphics;
g.DrawString("My Text", font, PdfBrushes.Black, PointF.Empty);
lDoc.Save("AddNewPages.pdf");
lDoc.Close();

VB.NET

Dim lDoc As New PdfImportedDocument("myfile.pdf")
Dim page As PdfPage = TryCast(lDoc.Pages.Add(), PdfPage)
' Use the predefined fonts to draw the text.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10.0F, PdfFontStyle.Regular)
Dim g As PdfGraphics = page.Graphicsg.DrawString("My Text", font, PdfBrushes.Black, PointF.Empty)
lDoc.Save("AddNewPages.pdf")lDoc.Close()

Removing a page

You can also remove pages from the existing PDF document by using the Remove or RemoveAt methods of the PdfImportedPageCollection class.

The following code example illustrates this.

C#:

PdfImportedDocument lDoc = new PdfImportedDocument("myfile.pdf");
// Remove the first
page.lDoc.Pages.RemoveAt(0);
lDoc.Pages.Remove(lDoc.Pages[0]);
lDoc.Save("AddNewPages.pdf");
lDoc.Close();

VB.NET

Dim lDoc As New PdfImportedDocument("myfile.pdf")
' Remove the first page.
lDoc.Pages.RemoveAt(0)
lDoc.Pages.Remove(lDoc.Pages(0))
lDoc.Save("AddNewPages.pdf")
lDoc.Close()