PDFsharp Line Break

2019-01-23 02:17发布

I am trying to get new line but if I use \n it does not help any way to have new line by adding something to string like \r\n (does not work)

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

does not work.

2条回答
倾城 Initia
2楼-- · 2019-01-23 02:41

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.

查看更多
戒情不戒烟
3楼-- · 2019-01-23 02:50

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);
查看更多
登录 后发表回答