Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Nov 28, 2017 9:48 am

Hey, I've run into a problem with writing long strings to the pdf page using .Canvas.DrawString().

A quick summary of what im doing.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);

page.Canvas.DrawString(long sting, new PdfTrueTypeFont(new Font("Calibri Light", 12f, FontStyle.Regular)), new PdfSolidBrush(Color.Black), 0, y, format: new PdfStringFormat(PdfTextAlignment.Left));

I've tried adding margin to the page, but that didn't do any good.
The string still continues off the page.

LukeRH
 
Posts: 4
Joined: Wed Nov 08, 2017 11:19 pm

Tue Nov 28, 2017 10:13 am

Hello,

Thanks for your inquiry.
Please refer to the code below.
Code: Select all
PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri Light", 12f, FontStyle.Regular));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            format.LineSpacing = 20f;
            PdfStringLayouter stringLayout = new PdfStringLayouter();
            PdfStringLayoutResult result = stringLayout.Layout(longString, font, format, new SizeF(page.GetClientSize().Width, 0));
            PointF position = new PointF(0, 0);
            foreach (LineInfo line in result.Lines)
            {

                page.Canvas.DrawString(line.Text, font, PdfBrushes.Black, position, format);
                position.Y = position.Y + result.LineHeight;
            }
            doc.SaveToFile("lognString.pdf");


If there's still any issue, please write back.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Nov 28, 2017 11:07 am

Thank you for your fast reply,
Your solution worked, thanks for the great support ;)

LukeRH
 
Posts: 4
Joined: Wed Nov 08, 2017 11:19 pm

Wed Nov 29, 2017 1:18 am

Hello,

Thanks for your feedback.
Just feel free to contact us if you need any assistance.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF

cron