There are many ways to draw texts with the ComponentSoft 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 Ultimate Pdf 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 in the UltimatePdf library.
Recent Comments