Working with Text

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 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)
//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 into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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;

//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));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);
            
//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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 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)
//Create the file open picker
var picker = new FileOpenPicker();

picker.FileTypeFilter.Add(".pdf");

//Browse and chose the file
StorageFile file = await picker.PickSingleFileAsync();

//Creates an empty PDF loaded document instance
PdfLoadedDocument doc = new PdfLoadedDocument();

//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await doc.OpenAsync(file);

//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 into memory stream.
MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

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

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "Output.pdf");
//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);

//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;

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

//Defining the ContentType 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);
//Load the file as stream
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf ");

//Load a PDF document.
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));

//Save the document into memory stream.
MemoryStream stream = new MemoryStream();

doc.Save(stream);

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

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

Drawing text using different fonts

Essential PDF allows you to add text to the PDF document using the following types of fonts.

  1. Standard fonts
  2. TrueType fonts
  3. 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.

Standard type 1 fonts

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 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)
//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 into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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;

//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));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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;

//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)
//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.

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Arial.ttf");

//Initialize the PDF TrueType font. 

PdfFont font = new PdfTrueTypeFont(fontStream, 14);

//Draw the text.

graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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;

//Load the TrueType font. 

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Arial.ttf");

//Initialize the PDF TrueType font.  

PdfFont font = new PdfTrueTypeFont(fontStream, 14);

//Draw the text.

graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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 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)
//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 into memory stream.
MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

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

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "Output.pdf");
//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);

//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;

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

//Defining the ContentType 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;

//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));

//Save the document into memory stream.
MemoryStream stream = new MemoryStream();

document.Save(stream);

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

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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 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)
//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 as stream

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document instances

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to the PDF/UWP section for respective code samples

Save(stream, "Output.pdf");
//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);

//If the position is not set to '0', then the PDF will be empty.

stream.Position = 0;

//Close the document

document.Close(true);

//Defining the ContentType 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 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 as stream

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document instances

document.Close(true);

//Save the stream into PDF file

//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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.

//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)
//PDF supports embedding fonts or displaying a Unicode text in the PDF document by default in UWP platform.
//PDF supports embedding fonts or displaying a Unicode text in the PDF document by default in Xamarin platform.
//PDF supports embedding fonts or displaying a Unicode text in the PDF document by default in ASP.NET Core platform.

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 font

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)
//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;

//Load the TrueType font

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.arial.ttf");

//Initialize the PDF TrueType font

PdfFont font = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Regular);

//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

Stream inputStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Arabic.txt");

StreamReader reader = new StreamReader(inputStream);

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);

//Save the document into memory stream

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the document

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to the PDF/UWP section for respective code samples

Save(stream, "Output.pdf");
//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 font

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);

//If the position is not set to '0', then the PDF will be empty

stream.Position = 0;

//Close the document

doc.Close(true);

//Defining the ContentType 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 doc = new PdfDocument();

//Add a page to the document

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the TrueType font

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.arial.ttf");

//Initialize the PDF TrueType font

PdfFont font = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Regular);

//Read the text from file

Stream inputStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Arabic.txt");

StreamReader reader = new StreamReader(inputStream);

string text = reader.ReadToEnd();

reader.Dispose();

//Set the format for string

PdfStringFormat format = new PdfStringFormat();

//Set the property for RTL text

format.TextDirection = PdfTextDirection.RightToLeft;

//Set the alignment

format.Alignment = PdfTextAlignment.Right;

format.ParagraphIndent = 35f;

//Draw string with right-to-left format

graphics.DrawString(text, font, PdfBrushes.Black, new Syncfusion.Drawing.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 Syncfusion.Drawing.RectangleF(0, 100, page.GetClientSize().Width, page.GetClientSize().Height), format);

//Save the document into memory stream

MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the document

doc.Close(true);

//Save the stream into PDF file

//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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.

  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)

    • Font
    • B
    • I
    • U
    • Sub
    • Sup
    • BR
  2. The PdfMetafileLayoutFormat class enables to break the HTML text into multiple pages.
  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.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)
//PDF supports Adding HTML Styled Text only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Adding HTML Styled Text only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Adding HTML Styled Text only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

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();

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));

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()

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))

document.Save("Output.pdf")

document.Close(True)
//Create a PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

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 the document into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create a PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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 PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

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 Syncfusion.Drawing.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 Syncfusion.Drawing.RectangleF(page.GetClientSize().Width / 2, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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();

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);

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()

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)

document.Save("Output.pdf")

document.Close(True)
//Create a PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

PdfGraphics graphics = page.Graphics;

//Read the long text from the text file.

Stream inputStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Input.txt");

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);

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create a PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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 PDF document instance

PdfDocument document = new PdfDocument();

//Add page to the document

PdfPage page = document.Pages.Add();

PdfGraphics graphics = page.Graphics;

//Read the long text from the text file.

Stream inputStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.txt");

StreamReader reader = new StreamReader(inputStream, Encoding.UTF8);

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 Syncfusion.Drawing.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 Syncfusion.Drawing.RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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.

//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 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 the document.

doc.Save("Output.pdf")

doc.Close(True)
//PDF supports Inserting Rich Text Format contents only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Inserting Rich Text Format contents only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Inserting Rich Text Format contents only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

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();

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()

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)
//Create a new instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

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 the document into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create a new instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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 instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

PdfGraphics graphics = page.Graphics;

Syncfusion.Drawing.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 Syncfusion.Drawing.RectangleF(0, 20, size.Width, size.Height));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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();

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()

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)
//Create a new instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

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 the document into memory stream.

MemoryStream stream = new MemoryStream();

await document.SaveAsync(stream);

//Close the document.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(stream, "Output.pdf");
//Create a new instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

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);

//If the position is not set to '0' then the PDF will be empty.

stream.Position = 0;

//Close the document.

document.Close(true);

//Defining the ContentType 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 instance of PdfDocument class.

PdfDocument document = new PdfDocument();

//Add a new page to the document.

PdfPage page = document.Pages.Add();

PdfGraphics graphics = page.Graphics;

Syncfusion.Drawing.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 Syncfusion.Drawing.RectangleF(0, 10, size.Width, size.Height));

//Save the document into memory stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the document.

document.Close(true);

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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.

//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)
//PDF supports Replace fonts in an existing document only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Replace fonts in an existing document only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports Replace fonts in an existing document only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

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.

  1. Syncfusion.Compression.Base.dll
  2. Syncfusion.Pdf.Base.dll
  3. 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)
//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

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("ComplexScriptSample.Assets.tahoma.ttf");

//Create a new PDF font instance

PdfFont font = new PdfTrueTypeFont(fontStream, 10);
           
//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 PDF document
         
MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the PDF document

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to the PDF/UWP section for respected code samples

 Save(stream, "Output.pdf");
//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);

//Create a new PDF font instance

PdfFont font = new PdfTrueTypeFont(fontStream, 10);
           
//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 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 doc = new PdfDocument();

//Add a page to the document

PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the font as stream

Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.tahoma.ttf");

//Create a new PDF font instance

PdfFont font = new PdfTrueTypeFont(fontStream, 10);
           
//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 PDF document
           
MemoryStream stream = new MemoryStream();

doc.Save(stream);

//Close the PDF document

doc.Close(true);

//Save the stream into PDF file

//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
	Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
	Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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)
Stream inputFileStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("input.pdf");

//Load a PDF document

PdfLoadedDocument doc = new PdfLoadedDocument(inputFileStream);

//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 PDF document

MemoryStream stream = new MemoryStream();

await doc.SaveAsync(stream);

//Close the PDF document

doc.Close(true);

//Save the stream as PDF document file in local machine. Refer to the PDF/UWP section for respective code samples

Save(stream, "Output.pdf");
FileStream inputFileStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);

//Load a PDF document

PdfLoadedDocument doc = new PdfLoadedDocument(inputFileStream);

//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 PDF document
          
MemoryStream stream = new MemoryStream();

await 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);
Stream inputFileStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");

//Load a PDF document

PdfLoadedDocument doc = new PdfLoadedDocument(inputFileStream);

//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 PDF document
          
MemoryStream stream = new MemoryStream();

await doc.Save(stream);

//Close the PDF document

doc.Close(true);

//Save the stream into PDF file

//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{ 
	Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
} 
else
{
	Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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 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)
//Create a new PDF document

PdfDocument document = new PdfDocument();

//Add a page

PdfPage page = document.Pages.Add();

//Create font

Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.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";

//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();

await document.SaveAsync(stream);

//Close the PDF document

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to the PDF/UWP section for respected code samples

Save(stream, "Output.pdf");
//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);

//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

PdfPage page = document.Pages.Add();

//Create font

Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.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";

//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);

MemoryStream stream = new MemoryStream();

document.Save(stream);

//Close the PDF document

document.Close(true);

//Save the stream into PDF file

//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples

if (Device.RuntimePlatform == Device.UWP)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}