Articles in this section
Category / Section

How to resize text based on the size in a PDF using C# and VB.NET

4 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can resize the text based on the size in PDF using C# and VB.NET.

Steps to resize text based on the size in PDF programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  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 code snippet to resize text based on the size in PDF.

C#

//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, 30);
//Declaring text to be drawn
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.";
//Bounds inside which the text should be drawn
RectangleF border = new RectangleF(0, 0, page.GetClientSize().Width, 150);
//Drawing the bounds 
graphics.DrawRectangle(PdfPens.Black, border);
//Initializing PdfStringLayouter variable
PdfStringLayouter layouter = new PdfStringLayouter();
// Initializing PdfStringLayoutResult in order to determine whether the string is clipped or not
PdfStringLayoutResult result = layouter.Layout(text, font, new PdfStringFormat(PdfTextAlignment.Center), new SizeF(border.Width, border.Height));
//This condition will pass if there is clipped text present with the given bounds value
if (result.Remainder != null && result.Remainder.Length != 0)
{
    //Method which resize the font size in order to fit the text inside the specified bounds
    PdfFont resizedFont = GetResizedFont(text, border, font);
    //Draw the text
    graphics.DrawString(text, resizedFont, PdfBrushes.Black, border);
}
else
{
    //Draw the text
    graphics.DrawString(text, font, PdfBrushes.Black, border);
}
//Save the document
document.Save("Output.pdf");
//Close the document
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer 
Process.Start("Output.pdf");

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 30)
'Declaring text to be drawn
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."
'Bounds inside which the text should be drawn
Dim border As RectangleF = New RectangleF(0, 0, page.GetClientSize().Width, 150)
'Drawing the bounds 
graphics.DrawRectangle(PdfPens.Black, border)
'Initializing PdfStringLayouter variable
Dim layouter As PdfStringLayouter = New PdfStringLayouter()
'Initializing PdfStringLayoutResult in order to determine whether the string is clipped or not
Dim result As PdfStringLayoutResult = layouter.Layout(text, font, New PdfStringFormat(PdfTextAlignment.Center), New SizeF(border.Width, border.Height))
'This condition will pass if there is clipped text present with the given bounds value
If result.Remainder IsNot Nothing AndAlso result.Remainder.Length <> 0 Then
    'Method which resize the font size in order to fit the text inside the specified bounds
    Dim resizedFont As PdfFont = GetResizedFont(text, border, font)
    'Draw the text
    graphics.DrawString(text, resizedFont, PdfBrushes.Black, border)
Else
    'Draw the text
    graphics.DrawString(text, font, PdfBrushes.Black, border)
End If
'Save the document 
document.Save("Output.pdf")
'Close the document 
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer 
Process.Start("Output.pdf")

 

  1. Add the following code in GetResizedFont method to get the resized font.

C#

    PdfFont pdfFont = font;
    var fontSize = pdfFont.Size;
    while (fontSize > 0f && pdfFont.MeasureString(text, bounds.Width).Height > bounds.Height)
    {
        fontSize -= 0.1f;
        if (font is PdfCjkStandardFont)
            pdfFont = new PdfCjkStandardFont((PdfCjkStandardFont)font, fontSize, font.Style);
        else if (font is PdfStandardFont)
            pdfFont = new PdfStandardFont((PdfStandardFont)font, fontSize, font.Style);
        else if (font is PdfTrueTypeFont)
            pdfFont = new PdfTrueTypeFont((PdfTrueTypeFont)font, fontSize);
 
    }
    //Assign the resized font
    font = pdfFont;
    return pdfFont;

 

VB.NET

    Dim pdfFont As PdfFont = font
    Dim fontSize = pdfFont.Size
    While fontSize > 0F AndAlso pdfFont.MeasureString(text, bounds.Width).Height > bounds.Height
        fontSize -= 0.1F
        If TypeOf font Is PdfCjkStandardFont Then
            pdfFont = New PdfCjkStandardFont(CType(font, PdfCjkStandardFont), fontSize, font.Style)
        ElseIf TypeOf font Is PdfStandardFont Then
            pdfFont = New PdfStandardFont(CType(font, PdfStandardFont), fontSize, font.Style)
        ElseIf TypeOf font Is PdfTrueTypeFont Then
            pdfFont = New PdfTrueTypeFont(CType(font, PdfTrueTypeFont), fontSize)
        End If
    End While
    'Assign the resized font
    font = pdfFont
    Return pdfFont

 

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

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

Output document screenshot

Take a moment to peruse the documentation, where you can find other options like drawing text in a new and existing PDF document using different fonts, Right-To-Left text, HTML styled text, Complex scripts, rich text format contents, replacing fonts in an existing PDF document with code examples.

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

Note:

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

 

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