Articles in this section
Category / Section

How to draw PDF text box based on text size in C#, 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 PDF text box based on text size using C# and VB.NET.

Steps to draw PDF text box based on text size programmatically:

  1. Create a new C# Windows Forms application project. Create new windows forms application
  2. 2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

  3. NuGet package reference screenshot
  4. Include the following namespaces in the Form1.Designer.cs file.

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to draw PDF text box based on text size.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a new page to the PDF document
PdfPage page = document.Pages.Add();
//Create a textbox field and add the properties
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
textBoxField.ToolTip = "First Name";
textBoxField.Text = "First Name";
//Get the font from textbox field
PdfFont pdfFont = textBoxField.Font;
//Measure the size of text in textbox field
SizeF size = pdfFont.MeasureString(textBoxField.Text);
//Set measured size as text box size
textBoxField.Bounds = new RectangleF(0, 0, size.Width + 4, size.Height);
//Add the form field to the document
document.Form.Fields.Add(textBoxField);
//Save and close the document
document.Save("TextBoxOnTextSize.pdf");
document.Close(true);

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
‘Create a textbox field and add the properties
Dim textBoxField As PdfTextBoxField = New PdfTextBoxField(page, "FirstName")
textBoxField.ToolTip = "First Name"
textBoxField.Text = "First Name"
'Get the font from textbox field
Dim pdfFont As PdfFont = textBoxField.Font
'Measure the size of text in textbox field
Dim size As SizeF = pdfFont.MeasureString(textBoxField.Text)
'Set measured size as text box size
textBoxField.Bounds = New RectangleF(0, 0, size.Width + 4, size.Height)
'Add the form field to the document
document.Form.Fields.Add(textBoxField)
'Save and close the document
document.Save("TextBoxOnTextSize.pdf")
document.Close(True)

 

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

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

Screenshot of output pdf file

Take a moment to peruse the documentation for working with forms, where you will find other options like creating, removing, modifying, and filling form fields in PDF document.

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.


Conclusion

I hope you enjoyed learning about how to draw PDF text box based on text size in C#, VB.NET.

You can refer to our .NET PDF library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF library example 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 forumsDirect-Trac, or feedbackportal. 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 (0)
Please  to leave a comment
Access denied
Access denied