PDFsharp word wrap
Asked Answered
S

2

21

How does one wrap text inside the rectangle using PDFsharp?

In my attempts the text still extends off the PDF's page. Here is what was tried:

rect = new XRect(20, 300, 400, 100);
tf.Alignment = XParagraphAlignment.Left;
gfx.DrawString("\t • Please call the borrower prior to closing and confirm your arrival time and the closing location. \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 320, 100, 100);
gfx.DrawString("\t • If the borrower needs to change the closing appointment time or date for any reason please have them call McDonnell Law Firm at 866-931-8793 \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 340, 100, 100);
gfx.DrawString("\t • Completed documents must be received same day. Fax back to 888-612-4912 or email [email protected] \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 360, 100, 100);
gfx.DrawString("\t • Documents are to be returned via Fedex or UPS with shipping label provided. Documents must be dropped same day. \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);

This is what it is doing > enter image description here

Stralsund answered 28/3, 2014 at 23:0 Comment(3)
What's the problem? Please be more specific.Gitlow
I added a picture. The text is going off of the page. I have changed the width of the rectangles but it makes no difference.Stralsund
I believe he meant "can't seem to understand..."Wilson
K
18

From your code snippet I assume that tf is an object of the XTextFormatter class while gfx is an XGraphics object.

XGraphics.DrawString does not support line breaks.

XTextFormatter.DrawString supports line breaks.

The error in your code: you are calling gfx.DrawString where you meant to call tf.DrawString.

Kneepad answered 23/6, 2015 at 20:52 Comment(2)
Pity, In VS2017 intelli-sense does not find a class called XTextFormatter :( And class CText,, which is found, has no DrawString() method :(Bureaucrat
Aah, had to add its location, in VB "imports PdfSharp.Drawing.Layout", to the code page to find it. OK, so far, now :)Bureaucrat
B
15

This is my example which does the following:

  1. defines a rectangle and draw a thin box to show the outline

  2. places the text within the box, small logic handled to add margin (adding 5 px to X coordination and deduct the same 5px as width area of text.

  3. XTextFormatter will be using to place the text within the defined rectangle.

Example

 PdfDocument pdf = new PdfDocument();
 PdfPage pdfPage = pdf.AddPage();
 XGraphics graph = XGraphics.FromPdfPage(pdfPage);

 var tf = new XTextFormatter(graph);
 var rect = new XRect(25, 50, 200, 34);

 XPen xpen = new XPen(XColors.Navy, 0.4);

 graph.DrawRectangle(xpen, rect);
 XStringFormat format = new XStringFormat();
 format.LineAlignment = XLineAlignment.Near;
 format.Alignment = XStringAlignment.Near;

 XBrush brush = XBrushes.Purple;
 tf.DrawString("This is some text written in a textbox over three lines bla bla bla bla bla ffffffffffffffffffffdsdsdsd", 
               new XFont("Helvetica", 8), 
               brush, 
               new XRect(rect.X + 5, rect.Y, rect.Width - 5, 34), format);

How to save and run the example:

string pdfFilename = "firstpage.pdf";
pdf.Save(pdfFilename);
Process.Start(pdfFilename);
Benevento answered 2/8, 2018 at 7:56 Comment(1)
Doesn't write over 3 lines, but it does word wrap +1Underpart

© 2022 - 2024 — McMap. All rights reserved.