PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:20 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Mar 06, 2023 3:55 pm 
Offline

Joined: Mon Mar 06, 2023 3:39 pm
Posts: 2
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 ?

Image

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);
        }


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 06, 2023 3:59 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Demonstration of XTextFormatter class:
http://pdfsharp.net/wiki/TextLayout-sample.ashx

Advanced version of XTextFormatter class:
viewtopic.php?f=8&t=3192

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 07, 2023 9:15 am 
Offline

Joined: Mon Mar 06, 2023 3:39 pm
Posts: 2
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);


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 01, 2024 8:12 am 
Offline

Joined: Thu Feb 01, 2024 8:09 am
Posts: 1
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group