We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Adjusting Font Width Only

Thread ID:

Created:

Updated:

Platform:

Replies:

132014 Aug 8,2017 11:05 AM Aug 9,2017 08:30 AM Windows Forms 1
loading
Tags: PDF
James Riddles
Asked On August 8, 2017 01:29 PM

I am working on a project to place text on a line in a PDF.  The line will have a max width of a variable amount.  I have successfully written a function to repeatedly reduce the size of the font by 0.5 points at a time, until the text fits.  Is there any way that I can affect the horizontal scaling only?  Or, perhaps the character spacing, as well?

Thanks!

Chinnu Muniyappan [Syncfusion]
Replied On August 9, 2017 08:30 AM

Hi James, 
 
Thank you for contacting Syncfusion support. 
 
The font size reducing workaround is the right way to fit the text with in smaller bounds. And we can scale the text horizontally/vertically by using PdfGraphics.ScaleTransform. If horizontal scaling is applied to a string then the whole string will shrink compared to its regular size. we have created a simple sample for your reference, please refer the below code snippet and sample for more details. 
            //Create a new PDF document 
            PdfDocument doc = new PdfDocument(); 
 
            //Add a new page 
            PdfPage page = doc.Pages.Add(); 
 
            //input text 
            string text = "Hello World!."; 
 
            //Create new PDF font 
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular); 
 
            //Measure the text width. 
            float textWidth = font.MeasureString(text).Width; 
 
            //Represent the line width to fix the text. 
            float lineWidth = 30; 
 
            //Find scaling factor 
            float scale = lineWidth / textWidth; 
 
            //Save the graphics 
            page.Graphics.Save(); 
 
            //Set scaling factor 
            page.Graphics.ScaleTransform(scale,1);            
 
            //Draw the string 
            page.Graphics.DrawString(text, font, PdfBrushes.Black, PointF.Empty); 
 
            //Restore the graphics 
            page.Graphics.Restore(); 
 
            //Save the PDF document 
            doc.Save("output.pdf"); 
 
            //Close the document 
            doc.Close(true); 
 
 
 
Please let us know if you have any concern. 
 
Regards, 
Chinnu 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;