30

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).

4

2 回答 2

38

您是否尝试过 XTextFormatter 类?

见这里: http ://www.pdfsharp.net/wiki/TextLayout-sample.ashx

代码片段:

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);
于 2011-03-16T00:50:13.347 回答
1

这是我所做的不涉及使用 Rect 类:

我有一个定义的右侧限制,并确定当前字符串是否会大于设置的界限。如果是,我就写了。否则,我继续添加它。

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

我知道我迟到了这个问题,但我希望它可以帮助别人。

于 2017-06-29T15:30:42.577 回答