How to resize text based on the size in a PDF using C# and VB.NET
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 a PDF using C# and VB.NET.
Steps to resize text based on the size in a PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- 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
- 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")
- 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.
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 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® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to resize text based on the size in a PDF using C# and VB.NET.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!