Articles in this section
Category / Section

How to draw normal and bold text on the same line in a PDF using C# and VB.NET

6 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw the normal and bold text on the same line in a PDF file by using PdfHTMLTextElement instance.

Steps to draw the normal and bold text on the sample line in a PDF file programmatically:

  1. Create a new C# console application project. drawingtext
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org. drawingtext.nuget
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

 VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Use the following C# or VB.NET code snippet to draw the normal and bold text on the same line in a PDF document.

C#

// Create a PDF document
PdfDocument doc = new PdfDocument();
 
// Add a page
PdfPage page = doc.Pages.Add();
 
// Create font
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 11.5f);
 
// HTML styled text
string htmlText = @"<b>PDF</b> stands for <b>P</b>ortable <b>D</b>ocument <b>F</b>ormat.The key word is portable, intended to combine the qualities of authenticity, reliability and ease of use together into a single packaged concept.";
 
// Create new instance for the PdfHTMLTextElement
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.Black);
 
// Set text alignment
richTextElement.TextAlign = TextAlign.Justify;
 
// Create a new instance for the PdfMetaFileLayoutFormat
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
 
// Set the layout type
format.Layout = PdfLayoutType.OnePage;
 
// Get the page client size
SizeF pageSize = page.GetClientSize();
 
// Draw the HTML text
richTextElement.Draw(page, new RectangleF(0, 15, pageSize.Width, pageSize.Height), format);
 
// Save the document
doc.Save("output.pdf");
 
// Close the document
doc.Close(true);

 

VB.NET

'Create a PDF document
Dim doc As New PdfDocument()
 
'Add a page
Dim page As PdfPage = doc.Pages.Add()
 
'Create font
Dim font As New PdfStandardFont(PdfFontFamily.Helvetica, 11.5F)
 
'HTML styled text
Dim htmlText As String = "<b>PDF</b> stands for <b>P</b>ortable <b>D</b>ocument <b>F</b>ormat.The key word is portable, intended to combine the qualities of authenticity, reliability and ease of use together into a single packaged concept."
 
'Create new instance for the PdfHTMLTextElement
Dim richTextElement As New PdfHTMLTextElement(htmlText, font, PdfBrushes.Black)
 
'Set text alignment
richTextElement.TextAlign = TextAlign.Justify
 
'Create a new instance for the PdfMetaFileLayoutFormat
Dim format As New PdfMetafileLayoutFormat()
 
'Set the layout type
format.Layout = PdfLayoutType.OnePage
 
'Get the page client size
Dim pageSize As SizeF = page.GetClientSize()
 
'Draw the html string
richTextElement.Draw(page, New RectangleF(0, 15, pageSize.Width, pageSize.Height), format)
 
'Save the document
doc.Save("output.pdf")
 
'Close the document
doc.Close(True)

 

By executing the program, you will get the PDF document as follows. drawingtextout

A complete working sample can be downloaded from DrawingText.zip.

Take a moment to peruse the documentation, where you can find other options like drawing Unicode text, RTL text, Complex script language, ordered list, and unorder list text, in a PDF document.

Refer here to explore the rich set of Syncfusion Essential&reg; PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion&reg; assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion&reg; license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to draw normal and bold text on the same line in a PDF using C# and VB.NET.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied