PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Measure Text Size with line return https://forum.pdfsharp.net/viewtopic.php?f=2&t=4424 |
Page 1 of 1 |
Author: | FloBeji [ Mon Mar 06, 2023 3:55 pm ] |
Post subject: | Measure Text Size with line return |
Hello I have created a small code for drawing a text. (see the code bellow) I want to draw the following text : "PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly" The code use the xGraphics.MeasureString method to build the XRect needed for the draw. We can see in the following image that it works well in a single line context. But when I do the same with a size restriction : only 170 point for the width. I don't have enought space to draw the full text. Because of the 3 lines return, the text take more space. Then it need an additional line to draw the remaining text : "documents on the fly". How to measure the height needed when drawing a text with a width constraint ? My C# Code : Code: static void Main(string[] args)
{ var pdf = new PdfDocument(); var page = pdf.AddPage(); page.Height = 842; page.Width = 590; var xStringFormat = new XStringFormat(); xStringFormat.LineAlignment = XLineAlignment.Near; xStringFormat.Alignment = XStringAlignment.Near; var xGraphics = XGraphics.FromPdfPage(page); var xTextFormatter = new XTextFormatter(xGraphics); xTextFormatter.Alignment = XParagraphAlignment.Left; var xColor = XColors.Black; var xFont = new XFont("", 12, XFontStyle.Bold); var text = "PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly"; DrawTextWithThisWidth(xGraphics, xFont, xColor, xStringFormat, xTextFormatter, text, 20, page.Width.Value); DrawTextWithThisWidth(xGraphics, xFont, xColor, xStringFormat, xTextFormatter, text, 60, 170); pdf.Save("C:/testpdf.pdf"); } static void DrawTextWithThisWidth(XGraphics xGraphics, XFont xFont, XColor xColor, XStringFormat xStringFormat, XTextFormatter xTextFormatter, string text, int y, double widthImposed) { var size = xGraphics.MeasureString(text, xFont); var lineNumber = Math.Round(size.Width / widthImposed, 2); var xRect = new XRect(20, y, widthImposed, size.Height * lineNumber); xTextFormatter.DrawString(text, xFont, new XSolidBrush(xColor), xRect, xStringFormat); xGraphics.DrawRectangle(new XPen(xColor), xRect); var xRectLabel = new XRect(20, y + xRect.Height+10, 100, 20); xTextFormatter.DrawString($"xREct:{xRect}", new XFont("",8), new XSolidBrush(xColor), xRectLabel, xStringFormat); } |
Author: | TH-Soft [ Mon Mar 06, 2023 3:59 pm ] |
Post subject: | Re: Measure Text Size with line return |
Demonstration of XTextFormatter class: http://pdfsharp.net/wiki/TextLayout-sample.ashx Advanced version of XTextFormatter class: viewtopic.php?f=8&t=3192 |
Author: | FloBeji [ Tue Mar 07, 2023 9:15 am ] |
Post subject: | Re: Measure Text Size with line return |
TH-Soft wrote: Demonstration of XTextFormatter class: http://pdfsharp.net/wiki/TextLayout-sample.ashx Advanced version of XTextFormatter class: https://forum.pdfsharp.net/viewtopic.php?f=8&t=3192 This is perfect. Thanks you so much. Code: var xTextFormatterEx = new XTextFormatterEx(xGraphics);
XRect rect = new XRect(20, y, widthImposed, double.MaxValue); xTextFormatterEx.PrepareDrawString(text, xFont, rect, out int lastCharIndex, out double neededHeight); var xRect = new XRect(20, y, widthImposed, neededHeight); |
Author: | mtomy2008 [ Thu Feb 01, 2024 8:12 am ] |
Post subject: | Re: Measure Text Size with line return |
FloBeji wrote: TH-Soft wrote: Demonstration of XTextFormatter class: http://pdfsharp.net/wiki/TextLayout-sample.ashx Advanced version of XTextFormatter class: viewtopic.php?f=8&t=3192 This is perfect. Thanks you so much. Code: var xTextFormatterEx = new XTextFormatterEx(xGraphics); XRect rect = new XRect(20, y, widthImposed, double.MaxValue); xTextFormatterEx.PrepareDrawString(text, xFont, rect, out int lastCharIndex, out double neededHeight); var xRect = new XRect(20, y, widthImposed, neededHeight); Thanks brother @FloBeji ! I really appreciate your help.. !! download lagu |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |