PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Text aligned in a rectangle
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2428
Page 1 of 1

Author:  saks07 [ Wed May 08, 2013 7:37 am ]
Post subject:  Text aligned in a rectangle

Hi,

I'm new in this forum and I have started using PDF# a few days ago. I have a problem, in the pdf document I want to draw a string from a rich text box in a rectangle, but when the document is created the string goes out of the page (picture below). I have followed your examples on allining text and using text formatter, on pdf wiki, but with no success.

Image

Here is my code:

PdfDocument doc = new PdfDocument();
doc.Info.Title = "Test";

PdfPage page = doc.AddPage();

XGraphics gfx = XGraphics.FromPdfPage(page);

XFont font = new XFont("Arial", 14, XFontStyle.Regular);

XRect rect = new XRect(60, 75, 350, 150);
gfx.DrawRectangle(XBrushes.Black, rect);

XStringFormat format = new XStringFormat();

gfx.DrawString(rchtxtbxText.Text, font, XBrushes.Red, rect, format);

const string file = "Test.pdf";
doc.Save(@"C:\...\Desktop\" + file);

I would like to know how to draw a string in a rectangle. If someone could help me or give me some examples I would appreciate it. Thanks in advance and sorry for my bad english

Author:  Thomas Hoevel [ Wed May 08, 2013 9:17 am ]
Post subject:  Re: Text aligned in a rectangle

Hi!

You have to use XTextFormatter to get more than a single line automatically.
With gfx.DrawString you will always get a single line only.

Link to sample code:
http://www.pdfsharp.net/wiki/TextLayout-sample.ashx

Author:  saks07 [ Wed May 08, 2013 9:48 am ]
Post subject:  Re: Text aligned in a rectangle

I get the same result, the text is not alligned in the rectangle, is in one line and it's out of page bounds like in the picture i posted. In the Xtextformatter example you have a const string text variable which has a defined string length in every row or has ("\n") to jump in a new line, otherwise I get the string from a rich text box which has word wrap enabled

Author:  Thomas Hoevel [ Wed May 08, 2013 11:47 am ]
Post subject:  Re: Text aligned in a rectangle

XTextFormatter breaks lines only at spaces. Use useful test text and it'll work.
Or modify XTextFormatter to your needs.

Author:  saks07 [ Wed May 08, 2013 2:52 pm ]
Post subject:  Re: Text aligned in a rectangle

It works. Thanks a lot for your support

Regards,
saks07

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/