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. Image for the cookie policy date

Adjusting Font Width Only

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!

1 Reply

CM Chinnu Muniyappan Syncfusion Team August 9, 2017 12:30 PM UTC

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 


Loader.
Up arrow icon