PDFsharp换行符

时间:2011-03-15 22:11:25

标签: c# wpf pdf newline pdfsharp

我正在努力获得新的一行,但是如果我使用\n,那么通过向\r\n这样的字符串添加内容(无法正常工作)来获取新行没有任何帮助

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

不起作用。

2 个答案:

答案 0 :(得分:29)

您是否尝试过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);

答案 1 :(得分:0)

这就是我所做的,不涉及使用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

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