35

I am trying to get new line but if I use \n it does not work.

Any way to have new line by adding something to string like \r\n (which also does not work)

gfx.DrawString("Project No \n" + textBoxProjNumber.Text, fontUnder, XBrushes.Black, 230, 95);

(the example snippet shows what I've tried but does not work).

Dave
  • 8,163
  • 11
  • 67
  • 103
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

2 Answers2

43

Have you tried the XTextFormatter class?

See here: http://www.pdfsharp.net/wiki/TextLayout-sample.ashx

Code snippet:

PdfDocument document = new PdfDocument();

PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);
XTextFormatter tf = new XTextFormatter(gfx);

XRect rect = new XRect(40, 100, 250, 220);
gfx.DrawRectangle(XBrushes.SeaShell, rect);
tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft);
  • Why do we need rectangle in that? – Afnan Bashir Mar 16 '11 at 07:57
  • 1
    This works.Thanks.One more thing that in migradoc how to make it detect that if paragraph is longer than a page and automatically insert page break – Afnan Bashir Mar 16 '11 at 08:00
  • 2
    Something I've just found (I know pdfsharp has been replaced by better libraries) but in VB.net you have to use + vbNewLine not /n – ShiftyThomas Jun 12 '13 at 09:49
  • 4
    @ShiftyThomas I'm curious about what "better libraries" replaced pdfSharp? Please share! – ctb Jul 26 '14 at 00:03
  • 2
    @ctb I don't think pdfSharp is still being updated, so a couple of times I've come across bugs in it and not been able to do anything about it. I've used http://sourceforge.net/projects/itextsharp/ for a bit now but they have changed their to payment licensing. For lightweight I've used http://www.stefanochizzolini.it/en/projects/clown/index.html – ShiftyThomas Aug 01 '14 at 09:59
  • 1
    @ctb PDFsharp is still being developed (yes, an update is long overdue). – I liked the old Stack Overflow Aug 04 '14 at 08:21
  • @AfnanBashir I know your question is four years old, but here is the answer: when using MigraDoc, page breaks will be added automatically when a paragraph (or a table) will not fit on a single page. The XTextFormatter is not part of MigraDoc and the version of XTextFormatter that comes with PDFsharp does not handle page breaks (it's meant to get you started and you can adapt that class to your needs). We recommend using MigraDoc for documents with a lot of text. – I liked the old Stack Overflow Jun 30 '15 at 08:20
  • 1
    He's talking about line breaks, not page breaks. – Patrick Borkowicz Apr 26 '17 at 17:56
  • @Patrick The second comment is about page breaks. My comment is a reply to that comment. – I liked the old Stack Overflow Apr 27 '17 at 08:19
2

This is what I did that does not involve using the Rect class:

I had a defined right lateral limit and determined whether the current string would be larger than the set bounds. If it was, I wrote it. Otherwise, I continued adding to it.

foreach (string field in temp)
{
    if (field == string.Empty)
    {
        continue;
    }
    else
    {
        tempSB.Clear();
        tempSB.Append(sb.ToString());
        tempSB.Append(field).Append(", ");  //append the incoming value to SB for size testing

        if (gfx.MeasureString(tempSB.ToString(), defaultFont).Width > 500)  //if the incoming string is bigger than the right bounds, write it and clear SB
        {
            gfx.DrawString(sb.ToString(), defaultFont, blackBrush, 50, currentLine + defaultSpacing);
            currentLine += 15;
            sb.Clear();
            sb.Append(" " + field).Append(",");  //add the overflow to the beginning of the next line
         }
         else
         {
             sb.Append(field).Append(", ");  //if it is not too big, append it
         }
     }

 }
 if (sb.Length > 0 && sb[sb.Length - 1] == ',') sb.Length--;
 gfx.DrawString(sb.ToString(), defaultFont, blackBrush, 50, currentLine + defaultSpacing); //write out whatever has not already been written out

I know I'm late to this question but I hope it can help someone.

wbt11a
  • 798
  • 4
  • 12