Working with text in the PDF document
5 Dec 202424 minutes to read
Drawing text in a new document
You can add text in the new PDF document by using DrawString method of PdfGraphics class as shown in the following code sample.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Drawing text in an existing document
The following code snippet illustrates how to add text in the existing PDF document by using DrawString method.
//Load the PDF document.
FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument doc = new PdfLoadedDocument(docStream);
//Get first page from document.
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
doc.Save(stream);
//Close the document.
doc.Close(true);
//Load a PDF document.
PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");
//Get first page from document.
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Load a PDF document.
Dim doc As New PdfLoadedDocument("input.pdf")
'Get first page from document
Dim page As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
You can download a complete working sample from GitHub.
Drawing text using different fonts
Essential® PDF allows you to add text to the PDF document using the following types of fonts.
- Standard fonts
- TrueType fonts
- Chinese, Japanese and Korean (CJK) fonts
Draw text using standard fonts
PDF has fourteen base fonts, also known as standard fonts which has special significance. The details can be referred from the link below.
You can add text using the standard PDF fonts, by initializing PdfFont class as PdfStandardFont class. The following code snippet illustrates this.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Draw text using TrueType fonts
You can add text using the TrueType fonts installed in the system, by initializing PdfFont class as PdfTrueTypeFont class. The following code snippet illustrates this.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Use the font installed in the machine
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14));
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Use the font installed in the machine
Dim font As PdfFont = New PdfTrueTypeFont(New Font("Arial", 14))
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can add text using the font file from local file system by providing the path of the TrueType font location. The following code snippet explains the same.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Load the TrueType font from the local *.ttf file.
FileStream fontStream = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Provide the path of the local *.ttf file
PdfFont font = new PdfTrueTypeFont(new Font("Arial.ttf", 14));
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
' Provide the path of the local *.ttf file
Dim font As PdfFont = New PdfTrueTypeFont(New Font("Arial.ttf", 14))
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Draw text using CJK fonts
You can add text using CJK fonts, initializing PdfFont class as PdfCjkStandardFont class. The following code sample illustrates this.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfCjkStandardFont(PdfCjkFontFamily.HeiseiMinchoW3, 20);
//Draw the text.
graphics.DrawString("こんにちは世界", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfCjkStandardFont(PdfCjkFontFamily.HeiseiMinchoW3, 20);
//Draw the text.
graphics.DrawString("こんにちは世界", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfCjkStandardFont(PdfCjkFontFamily.HeiseiMinchoW3, 20)
'Draw the text.
graphics.DrawString("こんにちは世界", font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Measuring a string
The Essential® PDF allows you to measure the size of a string which uses the PdfFont
through MeasureString method of it and returns the size. Refer to the following code sample.
//Create the new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a new PDF font instance
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
string text = "Hello World!";
//Measure the text
SizeF size = font.MeasureString(text);
//Draw string to th ePDF page
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(PointF.Empty, size));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
document.Save(stream);
//Close the document
document.Close(true);
//Create the new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a new PDF font instance
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
string text = "Hello World!";
//Measure the text
SizeF size = font.MeasureString(text);
//Draw string to the PDF page
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(PointF.Empty, size));
//Save the document
document.Save("Output.pdf");
//Close the document
document.Close(true);
'Create the new PDF document
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Create a new PDF font instance
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
Dim text As String = "Hello World!"
'Measure the text
Dim size As SizeF = font.MeasureString(text)
'Draw string to the PDF page
graphics.DrawString(text, font, PdfBrushes.Black, New RectangleF(PointF.Empty, size))
'Save the document
document.Save("Output.pdf")
'Close the document
document.Close(True)
You can download a complete working sample from GitHub.
Measure tilting space
The Syncfusion® PDF library enables users to accurately measure tilted spaces when drawing or measuring text in italic styles. This feature significantly improves measurement precision in PDF documents, especially when dealing with italicized fonts. To utilize this functionality, you can use the MeasureTiltingSpace property available in the PdfStringFormat class.
Refer to the following code example for further information.
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
// Load the font file from the stream
FileStream fontStream = new FileStream(@"../../../arial.ttf", FileMode.Open, FileAccess.Read);
//Create a new PDF font instance
PdfFont font = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Italic);
//Create a new PDF string format instance
PdfStringFormat format = new PdfStringFormat();
//Enable the measure tilting space
format.MeasureTiltingSpace = true;
string text = "Hello World!";
//Measure the tilted text
SizeF size = font.MeasureString(text, format);
//Draw the text to the PDF document.
page.Graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 0, size.Width, size.Height));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
document.Save(stream);
//Close the document
document.Close(true);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create a new PDF font instance
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14, PdfFontStyle.Italic),true);
//Create a new PDF string format instance
PdfStringFormat format = new PdfStringFormat();
//Enable the measure tilting space
format.MeasureTiltingSpace = true;
string text = "Hello World!";
//Measure the tilted text
SizeF size = font.MeasureString(text, format);
//Draw the text to the PDF document.
page.Graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0,0, size.Width, size.Height));
//Save the document.
document.Save("Output.pdf");
//Close the document
document.Close(true);
'Create the new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Load the font file from the stream
Dim fontStream As FileStream = New FileStream("font.ttf", FileMode.Open, FileAccess.Read)
'Create a new PDF font instance
Dim font As PdfFont = New PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Italic)
'Create a new PDF string format instance
Dim format As PdfStringFormat = New PdfStringFormat()
'Enable a measure tilting space
format.MeasureTiltingSpace = True
Dim text As String = "Hello World!"
'Measure the tilted text
Dim size As SizeF = font.MeasureString(text, format)
'Draw the text to the PDF document.
page.Graphics.DrawString(text, font, PdfBrushes.Black, New RectangleF(0, 0, size.Width, size.Height))
Dim stream As MemoryStream = New MemoryStream()
Save the document as stream
document.Save(stream)
Close the document
document.Close(True)
You can download a complete working sample from GitHub.
Embedding fonts and working with Unicode text
To embed a font or display Unicode text in the document, the ‘Unicode’ Boolean parameter of the PdfTrueTypeFont constructor has to be set to true. The following code illustrates the same.
NOTE
To render a Unicode text in the PDF document the chosen font should have the Unicode rendering capability.
//PDF supports embedding fonts or displaying a Unicode text in the PDF document by default in ASP.NET Core platform.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//use the system installed font
PdfFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 14), true);
//Read the unicode text from the text file.
StreamReader reader = new StreamReader(@"input.txt", Encoding.Unicode);
string text = reader.ReadToEnd();
reader.Close();
//Draw the text.
graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'use the system installed font
Dim font As PdfFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 14), True)
'Read the unicode text from the text file.
Dim reader As New StreamReader("input.txt", Encoding.Unicode)
Dim text As String = reader.ReadToEnd()
reader.Close()
'Draw the text.
graphics.DrawString(text, font, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Drawing Right-To-Left text
The Essential® PDF allows you to draw the right-to-left language text in a PDF document. To draw RTL scripts such as Arabic, Hebrew, Persian, and Urdu, set the value of TextDirection property in the PdfStringFormat class to RightToLeft using PdfTextDirection Enum. The languages (e.g., Sindhi and Kurdish) that have more than one script and can be written in either right-to-left or left-to-right format. The LeftToRight value of the TextDirection property is used to draw RTL text in the left-to-right format. Refer to the following code sample.
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a new PDF font instance
FileStream fontStream = new FileStream("arial.ttf", FileMode.Open, FileAccess.Read);
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set right-to-left text direction for RTL text
format.TextDirection = PdfTextDirection.RightToLeft;
//Set the alignment
format.Alignment = PdfTextAlignment.Right;
format.ParagraphIndent = 35f;
//Read the text from file
FileStream rtlText = new FileStream("Arabic.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(rtlText, Encoding.Unicode);
string text = reader.ReadToEnd();
reader.Dispose();
//Draw string with right-to-left format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Set left-to-right text direction for RTL text
format.TextDirection = PdfTextDirection.LeftToRight;
//Set the text alignment
format.Alignment = PdfTextAlignment.Left;
//Draw string with left-to-right format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 100, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
doc.Save(stream);
//Close the document
doc.Close(true);
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a new PDF font instance
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14), true);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set right-to-left text direction for RTL text
format.TextDirection = PdfTextDirection.RightToLeft;
//Set the text alignment
format.Alignment = PdfTextAlignment.Right;
format.ParagraphIndent = 35f;
//Read the text from file
StreamReader reader = new StreamReader("Arabic.txt", Encoding.Unicode);
string text = reader.ReadToEnd();
reader.Close();
//Draw string with right-to-left format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Set left-to-right text direction for RTL text
format.TextDirection = PdfTextDirection.LeftToRight;
//Set the text alignment
format.Alignment = PdfTextAlignment.Left;
//Draw string with left-to-right format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 100, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document
doc.Save("Output.pdf");
//Close the document
doc.Close(true);
'Create a new PDF document
Dim doc As PdfDocument = New PdfDocument()
'Add a page to the document
Dim page As PdfPage = doc.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Create font
Dim font As PdfFont = New PdfTrueTypeFont(New Font("Arial", 14), True)
'Set the format for string
Dim format As PdfStringFormat = New PdfStringFormat()
'Set right-to-left text direction for RTL text
format.TextDirection = PdfTextDirection.RightToLeft
'Set the alignment
format.Alignment = PdfTextAlignment.Right
format.ParagraphIndent = 35.0F
'Read the text from file
Dim reader As StreamReader = New StreamReader("Arabic.txt", Encoding.Unicode)
Dim text As String = reader.ReadToEnd()
reader.Close()
'Draw string with right-to-left format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format)
'Set left-to-right text direction for RTL text
format.TextDirection = PdfTextDirection.LeftToRight
'Set the text alignment
format.Alignment = PdfTextAlignment.Left
'Draw string with left-to-right format
graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 100, page.GetClientSize().Width, page.GetClientSize().Height), format)
'Save the document
doc.Save("Output.pdf")
'Close the document
doc.Close(True)
You can download a complete working sample from GitHub.
Adding a HTML Styled Text
Essential® PDF provides support to render simple HTML string in a PDF document that can flow through multiple pages. This can be done by using the PdfHTMLTextElement class.
Step 1: The PdfHTMLTextElement class provides support for a basic set of HTML tags, to render HTML format text in the PDF document.
Supported tags (Should be XHTML-compliant)
Supported tags | Windows-Specific | Cross-Platform |
---|---|---|
Font | Yes | Yes |
Bold (B) | Yes | Yes |
Italic (I) | Yes | Yes |
Underline (U) | Yes | Yes |
Subscript (Sub) (U) | Yes | No |
Superscript (Sup) (U) | Yes | No |
Line Break (BR) (U) | Yes | Yes |
Step 2: The PdfMetafileLayoutFormat class enables to break the HTML text into multiple pages.
Step 3: Complex HTML with CSS are not supported in this class. Please use HTML to PDF section for complex HTML with CSS and URL’s
The following code example illustrates how to render the HTML string in a PDF document.
//create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14, PdfFontStyle.Regular);
//simple HTML content
string htmlText = "<font color='#0000F8' face='TimesRoman' size='14'><i><b><u>Essential PDF</u></b></i></font> is a <u><i>.NET</i></u> library with the capability to produce Adobe PDF files";
//Render Html text
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.Black);
//Format layout
PdfLayoutFormat format = new PdfLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;
//Draw htmlString.
richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document into stream
MemoryStream stream = new MemoryStream();
doc.Save(stream);
//Close the document
doc.Close(true);
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 14);
//Simple HTML content
string htmlText = "<font color='#0000F8'>Essential PDF</font> is a <u><i>.NET</i></u> " +
"library with the capability to produce Adobe PDF files ";
//Render HtmlText.
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.Black);
richTextElement.TextAlign = TextAlign.Left;
//Format Layout.
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;
//Draw htmlString.
richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Courier, 14)
'Simple HTML content
Dim htmlText As String = "<font color='#0000F8'>Essential PDF</font> is a <u><i>.NET</i></u> " + "library with the capability to produce Adobe PDF files "
'Render HtmlText.
Dim richTextElement As New PdfHTMLTextElement(htmlText, font, PdfBrushes.Black)
richTextElement.TextAlign = TextAlign.Left
'Format Layout.
Dim format As New PdfMetafileLayoutFormat()
format.Layout = PdfLayoutType.Paginate
format.Break = PdfLayoutBreakType.FitPage
'Draw htmlString.
richTextElement.Draw(page, New RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format)
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
You can download a complete working sample from GitHub.
Creating a multicolumn PDF document
Essential® PDF allows you to create multi-column text in PDF document by using PdfTextElement class. The following code example illustrates the same.
//Create a PDF document instance
PdfDocument document = new PdfDocument();
//Add page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
//Draw the text in the first column
textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));
textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
//Draw the text in the second column
textElement.Draw(page, new RectangleF(page.GetClientSize().Width / 2, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a PDF document instance
PdfDocument document = new PdfDocument();
//Add page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
//Draw the text in the first column
textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));
textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
//Draw the text in the second column
textElement.Draw(page, new RectangleF(page.GetClientSize().Width / 2, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));
//Save and close the document
document.Save("Output.pdf");
document.Close(true);
'Create a PDF document instance
Dim document As New PdfDocument()
'Add page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
Dim text As String = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base."
'Create a text element with the text and font
Dim textElement As New PdfTextElement(text, New PdfStandardFont(PdfFontFamily.TimesRoman, 14))
'Draw the text in the first column
textElement.Draw(page, New RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height))
textElement = New PdfTextElement(text, New PdfStandardFont(PdfFontFamily.TimesRoman, 14))
'Draw the text in the second column
textElement.Draw(page, New RectangleF(page.GetClientSize().Width / 2, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height))
'Save and close the document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
The PdfLayoutFormat class helps to allow the text to flow across pages. The PdfLayoutResult class provides the rendered bounds of the previously added text which can be used to place successive elements without overlapping.
The following code snippet illustrates how to add elements relatively and also allow the text to flow across multiple pages.
//Create a PDF document instance
PdfDocument document = new PdfDocument();
//Add page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Read the long text from the text file
FileStream inputStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inputStream, Encoding.ASCII);
string text = reader.ReadToEnd();
reader.Dispose();
const int paragraphGap = 10;
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
layoutFormat.Break = PdfLayoutBreakType.FitPage;
//Draw the first paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Draw the second paragraph from the first paragraph end position
result = textElement.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a PDF document instance
PdfDocument document = new PdfDocument();
//Add page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Read the long text from the text file
StreamReader reader = new StreamReader(@"input.txt", Encoding.ASCII);
string text = reader.ReadToEnd();
reader.Close();
const int paragraphGap = 10;
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
layoutFormat.Break = PdfLayoutBreakType.FitPage;
//Draw the first paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Draw the second paragraph from the first paragraph end position
result = textElement.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Save and close the document
document.Save("Output.pdf");
document.Close(true);
'Create a PDF document instance
Dim document As New PdfDocument()
'Add page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Read the RTL text from the text file.
Dim reader As New StreamReader("input.txt", Encoding.ASCII)
Dim text As String = reader.ReadToEnd()
reader.Close()
Const paragraphGap As Integer = 10
'Create a text element with the text and font
Dim textElement As New PdfTextElement(text, New PdfStandardFont(PdfFontFamily.TimesRoman, 14))
Dim layoutFormat As New PdfLayoutFormat()
layoutFormat.Layout = PdfLayoutType.Paginate
layoutFormat.Break = PdfLayoutBreakType.FitPage
'Draw the first paragraph
Dim result As PdfLayoutResult = textElement.Draw(page, New RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat)
'Draw the second paragraph from the first paragraph end position
result = textElement.Draw(page, New RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat)
'Save and close the document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Inserting Rich Text Format contents
Essential® PDF allows you to insert a RTF text into a PDF document by converting it as bitmap or metafile image and rendering it using FromRtf method of PdfImage class.
The following code example illustrates how to insert RTF text in PDF document.
//PDF supports inserting rich text format contents only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page.
PdfPage page = doc.Pages.Add();
SizeF bounds = page.GetClientSize();
//Read RTF document.
StreamReader reader = new StreamReader(@"input.rtf", Encoding.ASCII);
string text = reader.ReadToEnd();
reader.Close();
//Convert it to Metafile.
PdfMetafile imageMetafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
//Allow text to flow multiple pages without any break.
format.SplitTextLines = true;
//Draws image.
imageMetafile.Draw(page, 0, 0, format);
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page.
Dim page As PdfPage = doc.Pages.Add()
Dim bounds As SizeF = page.GetClientSize()
'Read RTF document.
Dim reader As New StreamReader("input.rtf", Encoding.ASCII)
Dim text As String = reader.ReadToEnd()
reader.Close()
'Convert it to Metafile.
Dim imageMetafile As PdfMetafile = DirectCast(PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile), PdfMetafile)
Dim format As New PdfMetafileLayoutFormat()
'Allow text to flow multiple pages without any break.
format.SplitTextLines = True
'Draws image.
imageMetafile.Draw(page, 0, 0, format)
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
You can download a complete working sample from GitHub.
NOTE
For converting complex RTF content to PDF, refer the RTF to PDF section.
Adding an Ordered List
Essential® PDF allows you to create an ordered list in the document. Ordered List is represented by the PdfOrderedList class and can be numerical or alphabetical. The following code snippet illustrates the same.
//Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
//Add a new page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
SizeF size = page.Graphics.ClientSize;
//Create font
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Italic);
string[] products = { "Tools", "Grid", "Chart", "Edit", "Diagram", "XlsIO", "Grouping", "Calculate", "PDF", "HTMLUI", "DocIO" };
//Create string format
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 10f;
//Create Ordered list
PdfOrderedList pdfList = new PdfOrderedList();
pdfList.Marker.Brush = PdfBrushes.Black;
pdfList.Indent = 20;
//Set format for sub list
pdfList.Font = font;
pdfList.StringFormat = format;
foreach (string s in products)
{
//Add items
pdfList.Items.Add(string.Concat("Essential ", s));
}
pdfList.Draw(page, new RectangleF(0, 20, size.Width, size.Height));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
//Add a new page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
SizeF size = page.Graphics.ClientSize;
//Create font
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Italic);
string[] products = { "Tools", "Grid", "Chart", "Edit", "Diagram", "XlsIO", "Grouping", "Calculate", "PDF", "HTMLUI", "DocIO" };
//Create string format
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 10f;
//Create Ordered list
PdfOrderedList pdfList = new PdfOrderedList();
pdfList.Marker.Brush = PdfBrushes.Black;
pdfList.Indent = 20;
//Set format for sub list
pdfList.Font = font;
pdfList.StringFormat = format;
foreach (string s in products)
{
//Add items
pdfList.Items.Add(string.Concat("Essential ", s));
}
pdfList.Draw(page, new RectangleF(0, 20, size.Width, size.Height));
// Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new instance of PdfDocument class.
Dim document As New PdfDocument()
'Add a new page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
Dim size As SizeF = page.Graphics.ClientSize
'Create font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Italic)
Dim products As String() = {"Tools", "Grid", "Chart", "Edit", "Diagram", "XlsIO", _
"Grouping", "Calculate", "PDF", "HTMLUI", "DocIO"}
'Create string format
Dim format As New PdfStringFormat()
format.LineSpacing = 10.0F
'Create Ordered list
Dim pdfList As New PdfOrderedList()
pdfList.Marker.Brush = PdfBrushes.Black
pdfList.Indent = 20
'Set format for sub list
pdfList.Font = font
pdfList.StringFormat = format
For Each s As String In products
'Add items
pdfList.Items.Add(String.Concat("Essential ", s))
Next
pdfList.Draw(page, New RectangleF(0, 20, size.Width, size.Height))
'Save and close the document.
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Adding an Unordered List
Essential® PDF also provides support to create an unordered List that is represented by the PdfUnorderedList class. An Unordered list can be bullets, circle or an image. The following code snippet illustrates the same.
//Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
//Add a new page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
SizeF size = page.Graphics.ClientSize;
//Create an unordered list
PdfUnorderedList list = new PdfUnorderedList();
//Set the marker style
list.Marker.Style = PdfUnorderedMarkerStyle.Disk;
//Create font and write title
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular);
//Create string format
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 10f;
// Format list
list.Font = font;
list.StringFormat = format;
//Set list indent
list.Indent = 10;
//Add items to the list
list.Items.Add("PDF");
list.Items.Add("XlsIO");
list.Items.Add("DocIO");
list.Items.Add("PPT");
//Set text indent
list.TextIndent = 10;
//Draw list
list.Draw(page, new RectangleF(0, 10, size.Width, size.Height));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into memory stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
//Add a new page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
SizeF size = page.Graphics.ClientSize;
//Create an unordered list
PdfUnorderedList list = new PdfUnorderedList();
//Set the marker style
list.Marker.Style = PdfUnorderedMarkerStyle.Disk;
//Create font and write title
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular);
//Create string format
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 10f;
// Format list
list.Font = font;
list.StringFormat = format;
//Set list indent
list.Indent = 10;
//Add items to the list
list.Items.Add("PDF");
list.Items.Add("XlsIO");
list.Items.Add("DocIO");
list.Items.Add("PPT");
//Set text indent
list.TextIndent = 10;
//Draw list
list.Draw(page, new RectangleF(0, 10, size.Width, size.Height));
// Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new instance of PdfDocument class.
Dim document As New PdfDocument()
'Add a new page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
Dim size As SizeF = page.Graphics.ClientSize
'Create an unordered list
Dim list As New PdfUnorderedList()
'Set the marker style
list.Marker.Style = PdfUnorderedMarkerStyle.Disk
'Create font and write title
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular)
'Create string format
Dim format As New PdfStringFormat()
format.LineSpacing = 10.0F
' Format list
list.Font = font
list.StringFormat = format
'Set list indent
list.Indent = 10
'Add items to the list
list.Items.Add("PDF")
list.Items.Add("XlsIO")
list.Items.Add("DocIO")
list.Items.Add("PPT")
'Set text indent
list.TextIndent = 10
'Draw list
list.Draw(page, New RectangleF(0, 10, size.Width, size.Height))
'Save and close the document.
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Replace Fonts in an existing document
Essential® PDF allows you to replace the fonts in an existing PDF document by using the Replace method. The following code snippet illustrates the same.
//PDF supports Replace fonts in an existing document only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Creates a new PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Replace font
loadedDocument.UsedFonts[0].Replace(new PdfStandardFont(PdfFontFamily.TimesRoman, 12));
//Save the document
loadedDocument.Save("Output.pdf");
loadedDocument.Close(true);
'Creates a new PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Replace font
loadedDocument.UsedFonts(0).Replace(New PdfStandardFont(PdfFontFamily.TimesRoman, 12))
'Save the document
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)
You can download a complete working sample from GitHub.
Search and get the bounds of a text in a document
You can search for a particular text in a document and get the bounds using FindText method of PdfViewerControl class. To include this functionality, you need to add the below mentioned assemblies as reference to the project.
- Syncfusion.Compression.Base.dll
- Syncfusion.Pdf.Base.dll
- Syncfusion.PdfViewer.Windows.dll
The following code snippet illustrates how to get the bound of a text from PDF document.
PdfViewerControl documentViewer = new PdfViewerControl();
//Load the PDF document
documentViewer.Load("Input.pdf");
//Get the occurrences of the target text and location.
Dictionary<int, List<RectangleF>> textSearch = new Dictionary<int, List<RectangleF>>();
bool IsMatchFound = documentViewer.FindText("hello", out textSearch);
documentViewer.Dispose();
Dim documentViewer As New PdfViewerControl()
'Load the PDF document
documentViewer.Load("Input.pdf")
'Get the occurrences of the target text and location.
Dim textSearch As New Dictionary(Of Integer, List(Of RectangleF))()
Dim IsMatchFound As Boolean = documentViewer.FindText("hello", textSearch)
documentViewer.Dispose()
Drawing complex script language text
Essential® PDF allows you to add complex script language text in the PDF document by using the ComplexScript property available in PdfStringFormat class. The following code snippet illustrates this.
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the font with Unicode option
Font font = new Font("Tahoma", 14);
PdfFont pdfFont = new PdfTrueTypeFont(font, true);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set the format as complex script layout type
format.ComplexScript = true;
//Draw the text
graphics.DrawString("สวัสดีชาวโลก", pdfFont, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document
doc.Save("Output.pdf");
//Close the document
doc.Close(true);
'Create a new PDF document
Dim doc As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = doc.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Set the font with Unicode option
Dim font As New Font("Tahoma", 14)
Dim pdfFont As PdfFont = New PdfTrueTypeFont(font, True)
'Set the format for string
Dim format As New PdfStringFormat()
'Set the format as complex script layout type
format.ComplexScript = True
'Draw the text
graphics.DrawString("สวัสดีชาวโลก", pdfFont, PdfBrushes.Black, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format)
'Save the document
doc.Save("Output.pdf")
'Close the document
doc.Close(True)
You can download a complete working sample from GitHub.
You can add the complex script language text in an existing PDF document by using the following code sample.
//Load a PDF document
PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");
//Get first page from the document
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the font with Unicode option
Font font = new Font("Tahoma", 14);
PdfFont pdfFont = new PdfTrueTypeFont(font, true);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set the format as complex script layout type
format.ComplexScript = true;
//Draw the text
graphics.DrawString("สวัสดีชาวโลก", pdfFont, PdfBrushes.Black, new RectangleF(0, 0, page.Size.Width, page.Size.Height), format);
//Save the document
doc.Save("Output.pdf");
//Close the document
doc.Close(true);
'Load a PDF document
Dim doc As New PdfLoadedDocument("input.pdf")
'Get first page from the document
Dim page As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Set the font with Unicode option
Dim font As New Font("Tahoma", 14)
Dim pdfFont As PdfFont = New PdfTrueTypeFont(font, True)
'Set the format for string
Dim format As New PdfStringFormat()
'Set the format as complex script layout type
format.ComplexScript = True
'Draw the text
graphics.DrawString("สวัสดีชาวโลก", pdfFont, PdfBrushes.Black, New RectangleF(0, 0, page.Size.Width, page.Size.Height), format)
'Save the document
doc.Save("Output.pdf")
'Close the document
doc.Close(True)
You can download a complete working sample from GitHub.
Drawing text using OpenType font
Essential® PDF supports drawing text on a PDF document with OpenType font using PdfTrueTypeFont class, by providing the path of font file from local file system. The following code snippet illustrates this.
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();
//Create font
FileStream fontFileStream = new FileStream("Font.otf", FileMode.Open, FileAccess.Read);
PdfFont font = new PdfTrueTypeFont(fontFileStream, 14);
//Text to draw
string text = "Syncfusion Essential PDF is a.NET Core PDF library used to create, read, and edit PDF files in any application";
//Create a brush
PdfBrush brush = new PdfSolidBrush(new PdfColor(0, 0, 0));
//Create a pen
PdfPen pen = new PdfPen(new PdfColor(0, 0, 0));
//Get page client size
SizeF clipBounds = page.Graphics.ClientSize;
RectangleF rect = new RectangleF(0, 0, clipBounds.Width, clipBounds.Height);
//Draw the text
page.Graphics.DrawString(text, font, brush, rect);
//Save the PDF document
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the PDF document
document.Close(true);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create font
Stream fontStream = System.IO.File.OpenRead("Font.otf");
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
//Text to draw
string text = "Syncfusion Essential PDF is a.NET PDF library used to create, read, and edit PDF files in any application";
//Get page client size
SizeF clipBounds = page.Graphics.ClientSize;
RectangleF rect = new RectangleF(0, 0, clipBounds.Width, clipBounds.Height);
//Draw the text
page.Graphics.DrawString(text, font, PdfBrushes.Blue, rect);
//Save the document
document.Save("Output.pdf");
//Close the document
document.Close(true);
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument
'Add a page to the document
Dim page As PdfPage = document.Pages.Add
'Create font
Dim fontStream As Stream = System.IO.File.OpenRead("Font.otf")
Dim font As PdfFont = New PdfTrueTypeFont(fontStream, 14)
'Text to draw
Dim text As String = "Syncfusion Essential PDF is a.NET PDF library used to create, read, and edit PDF files in any application"
'Get page client size
Dim clipBounds As SizeF = page.Graphics.ClientSize
Dim rect As RectangleF = New RectangleF(0, 0, clipBounds.Width, clipBounds.Height)
'Draw the text
page.Graphics.DrawString(text, font, PdfBrushes.Blue, rect)
'Save the document
document.Save("Output.pdf")
'Close the document
document.Close(true)
You can download a complete working sample from GitHub.
Drawing text with baseline alignment
The Essential® PDF allows you to draw text using a different type of fonts with different sizes with the same baseline alignment in the PDF document by using the EnableBaseline property available in PdfStringFormat class. The following code sample explains this.
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
FileStream fontStream = new FileStream("tahoma.ttf", FileMode.Open, FileAccess.Read);
FileStream fontStream1 = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
FileStream fontStream2 = new FileStream("Calibri.ttf", FileMode.Open, FileAccess.Read);
//Create a new PDF font instance
PdfFont font = new PdfTrueTypeFont(fontStream, 8);
PdfFont font1 = new PdfTrueTypeFont(fontStream1, 20);
PdfFont font2 = new PdfStandardFont(PdfFontFamily.Helvetica,16);
PdfFont font3 = new PdfTrueTypeFont(fontStream2, 25);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set the line alignment
format.LineAlignment = PdfVerticalAlignment.Bottom;
//Set baseline for the line alignment
format.EnableBaseline = true;
//Draw the text
graphics.DrawString("Hello World!", font, PdfBrushes.Black, new PointF(0, 50), format);
graphics.DrawString("Hello World!", font1, PdfBrushes.Black, new PointF(65, 50), format);
graphics.DrawString("Hello World!", font2, PdfBrushes.Black, new PointF(220, 50), format);
graphics.DrawString("Hello World!", font3, PdfBrushes.Black, new PointF(320, 50), format);
//Save the PDF document
MemoryStream stream = new MemoryStream();
doc.Save(stream);
//Close the PDF document
doc.Close(true);
//Defining the content type for PDF file.
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a new PDF font instance
PdfFont font = new PdfTrueTypeFont(new Font("Tahoma",8), 8);
PdfFont font1 = new PdfTrueTypeFont(new Font("Calibri",20), 20);
PdfFont font2 = new PdfStandardFont(PdfFontFamily.Helvetica,16);
PdfFont font3 = new PdfTrueTypeFont(new Font("Arial",25), 25);
//Set the format for string
PdfStringFormat format = new PdfStringFormat();
//Set the line alignment
format.LineAlignment = PdfVerticalAlignment.Bottom;
//Set baseline for the line alignment
format.EnableBaseline = true;
//Draw the text
graphics.DrawString("Hello World!", font, PdfBrushes.Black, new PointF(0, 50), format);
graphics.DrawString("Hello World!", font1, PdfBrushes.Black, new PointF(65, 50), format);
graphics.DrawString("Hello World!", font2, PdfBrushes.Black, new PointF(220, 50), format);
graphics.DrawString("Hello World!", font3, PdfBrushes.Black, new PointF(320, 50), format);
//Save the document
document.Save("Output.pdf");
//Close the document
document.Close(true);
'Create a new PDF document
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Create a new PDF font instance
Dim font As PdfFont = New PdfTrueTypeFont(new Font("Tahoma",8), 8)
Dim font1 As PdfFont = New PdfTrueTypeFont(new Font("Calibri",20), 20)
Dim font2 As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica,16)
Dim font3 As PdfFont = New PdfTrueTypeFont(new Font("Arial",25), 25)
'Set the format for string
Dim format As New PdfStringFormat()
'Set the line alignment
format.LineAlignment = PdfVerticalAlignment.Bottom;
'Set baseline for the line alignment
format.EnableBaseline = True
'Draw the text
graphics.DrawString("Hello World!", font, PdfBrushes.Black, New PointF(0, 50), format)
graphics.DrawString("Hello World!", font1, PdfBrushes.Black, New PointF(65, 50), format)
graphics.DrawString("Hello World!", font2, PdfBrushes.Black, New PointF(220, 50), format)
graphics.DrawString("Hello World!", font3, PdfBrushes.Black, New PointF(320, 50), format)
'Save the document
document.Save("Output.pdf")
'Close the document
document.Close(True)
You can download a complete working sample from GitHub.
Adding a text encoding to the PdfStandardFont
The following code sample shows how to add a text encoding using the standard PDF fonts by initializing PdfFont class as PdfStandardFont class.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Adding a new page to the PDF document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Create a new PDF standard font instance.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
//Set the text encoding.
font.SetTextEncoding(Encoding.GetEncoding("Windows-1250"));
//Draw string to a PDF page.
graphics.DrawString("äÖíßĆŇ", font, PdfBrushes.Black, PointF.Empty);
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Adding a new page to the PDF document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Create a new PDF standard font instance.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
//Set the text encoding.
font.SetTextEncoding(Encoding.GetEncoding("Windows-1250"));
//Draw string to a PDF page.
graphics.DrawString("äÖíßĆŇ", font, PdfBrushes.Black, PointF.Empty);
//Save the PDF document.
document.Save("Output.pdf");
//Close the PDF document.
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Adding a new page to the PDF document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Create a new PDF standard font instance.
Dim font As PdfStandardFont = New PdfStandardFont(PdfFontFamily.Helvetica,12)
'Set the text encoding.
font.SetTextEncoding(Encoding.GetEncoding("Windows-1250"))
'Draw string to a PDF page.
graphics.DrawString("äÖíßCN", font, PdfBrushes.Black, PointF.Empty)
'Save the PDF document.
document.Save("Output.pdf")
'Close the PDF document.
document.Close(True)
You can download a complete working sample from GitHub.
Find Text
The code example provided below demonstrates the utilization of the FindText method from the PdfLoadedDocument class to locate text within a PDF document. This method facilitates the retrieval of both the page number and the rectangular coordinates of the identified text occurrences.
//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Returns page number and rectangle positions of the text maches.
Dictionary<int, List<Syncfusion.Drawing.RectangleF>> matchRects = new Dictionary<int, List<Syncfusion.Drawing.RectangleF>>();
loadedDocument.FindText("document", out matchRects);
//Close the document.
loadedDocument.Close(true);
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Returns page number and rectangle positions of the text maches.
Dictionary<int, List<System.Drawing.RectangleF>> matchRects = new Dictionary<int, List<System.Drawing.RectangleF>>();
loadedDocument.FindText("document", out matchRects);
//Close the document.
loadedDocument.Close(true);
'Load an existing PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Returns page number and rectangle positions of the text maches.
Dim matchRects As Dictionary(Of Integer, List(Of System.Drawing.RectangleF)) = New Dictionary(Of Integer, List(Of System.Drawing.RectangleF))()
loadedDocument.FindText("document", matchRects)
'Close the document.
loadedDocument.Close(True)
You can download a complete working sample from GitHub.
Customizing TrueType fonts in a PDF document
The Syncfusion® PDF library provides extensive options for customizing TrueType font settings in PDF documents through the PdfFontSettings
class. Users can modify font size, style, and choose to embed or subset fonts. Additionally, measurement settings can be adjusted using a floating factor for precise control over font rendering.
The following code example illustrates this.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Load the TrueType font from the local *.ttf file.
FileStream fontStream = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
// Initialize the PdfFontSettings
PdfFontSettings fontSettings = new PdfFontSettings(10, PdfFontStyle.Bold, true, true, true);
PdfFont pdfFont = new PdfTrueTypeFont(fontStream, fontSettings);
//Draw the text. graphics.DrawString("Hello World!!!", pdfFont, PdfBrushes.Black, new PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
// Initialize the PdfFontSettings
PdfFontSettings fontSettings = new PdfFontSettings(10, PdfFontStyle.Bold, true, true, true);
PdfFont pdfFont = new PdfTrueTypeFont(new Font("Arial"), fontSettings);
//Draw the text. graphics.DrawString("Hello World!!!", pdfFont, PdfBrushes.Black, new PointF(0, 0));
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Use the fontStream installed in the machine.
Dim fontStream As FileStream = New FileStream("Arial.ttf", FileMode.Open, FileAccess.Read)
'Initialize the PdfFontSettings.
Dim fontSettings As PdfFontSettings = New PdfFontSettings(10, PdfFontStyle.Bold, true, true, true)
Dim pdfFont As PdfFont = New PdfTrueTypeFont(fontStream, fontSettings)
'Draw the text.
graphics.DrawString("Hello World!!!", pdfFont, PdfBrushes.Black, New PointF(0, 0))
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(true)
You can download a complete working sample from GitHub.
LineLimit, ClipPath, NoClip properties in PdfStringFormat
LineLimit: When LineLimit is enabled, the provided string will be laid out within the specified bounds. If the LineLimit property is disabled, the layout will continue to fill any remaining space. The default value of the LineLimit property is true.
NoClip: If we enable the NoClip option, it will show the text without cutting any words. If we disable the NoClip option, any text outside the fitting area will be hidden.
The following code example illustrates this.
// Create a new PdfStringFormat and set its properties
PdfStringFormat format = new PdfStringFormat();
//Set no clip
format.NoClip = true;
//Set line limit
format.LineLimit = false;
// Create a new PdfFont using the Helvetica font family
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Create a new PdfDocument
PdfDocument document = new PdfDocument();
// Set the page margins to zero
document.PageSettings.Margins.All = 0;
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Get the graphics object of the page
PdfGraphics graphics = page.Graphics;
// Draw a red rectangle at the specified position and size
graphics.DrawRectangle(PdfPens.Red, new RectangleF(100, 100, 100, 20));
// Draw the string inside the rectangle with the specified font, brush, and format
graphics.DrawString("PDF text line 1 \r\nPDF text line 3", font, PdfBrushes.Black, new RectangleF(100, 100, 100, 20), format);
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//Close the document.
document.Close(true);
// Create a new PdfStringFormat and set its properties
PdfStringFormat format = new PdfStringFormat();
//Set no clip
format.NoClip = true;
//Set line limit
format.LineLimit = false;
// Create a new PdfFont using the Helvetica font family
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Create a new PdfDocument
PdfDocument document = new PdfDocument();
// Set the page margins to zero
document.PageSettings.Margins.All = 0;
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Get the graphics object of the page
PdfGraphics graphics = page.Graphics;
// Draw a red rectangle at the specified position and size
graphics.DrawRectangle(PdfPens.Red, new RectangleF(100, 100, 100, 20));
// Draw the string inside the rectangle with the specified font, brush, and format
graphics.DrawString("PDF text line 1 \r\nPDF text line 3", font, PdfBrushes.Black, new RectangleF(100, 100, 100, 20), format);
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
' Create a new PdfStringFormat and set its properties
Dim format As New PdfStringFormat()
' Set no clip
format.NoClip = True
' Set line limit
format.LineLimit = False
' Create a new PdfFont using the Helvetica font family
Dim font As New PdfStandardFont(PdfFontFamily.Helvetica, 12)
' Create a new PdfDocument
Dim document As New PdfDocument()
' Set the page margins to zero
document.PageSettings.Margins.All = 0
' Add a new page to the document
Dim page As PdfPage = document.Pages.Add()
' Get the graphics object of the page
Dim graphics As PdfGraphics = page.Graphics
' Draw a red rectangle at the specified position and size
graphics.DrawRectangle(PdfPens.Red, New RectangleF(100, 100, 100, 20))
' Draw the string inside the rectangle with the specified font, brush, and format
graphics.DrawString("PDF text line 1" & vbCrLf & "PDF text line 3", font, PdfBrushes.Black, New RectangleF(100, 100, 100, 20), format)
' Save the document
document.Save("Output.pdf")
' Close the document
document.Close(True)
LineLimit | NoClip | Result |
---|---|---|
true | true | |
false | true | |
true | false | |
false | false |
You can download a complete working sample from GitHub.