2011-03-15 126 views
23

我想获得新的生产线,但如果我用\ n它不添加东西串像\ r \ n(不工作)PDFsharp换行符

gfx.DrawString("Project No \n" + textBoxProjNumber.Text, fontUnder, XBrushes.Black, 230, 95); 
帮助任何方式有新线

不起作用。

+0

如果它是一种控制台模式,那么调用DrawString()两次? – BlackBear 2011-03-15 22:29:17

+0

对不起,没有? – 2011-03-15 22:50:52

+2

尝试调用DrawString()为每一行你想打印,例如DrawString(Project No“); DrawString(textBoxProj .....); – BlackBear 2011-03-15 22:54:13

回答

27

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

为什么我们需要矩形? – 2011-03-16 07:57:31

+0

这是有效的。谢谢。还有一件事是,在migradoc中如何检测段落是否长于页面并自动插入分页符 – 2011-03-16 08:00:08

+1

我刚才找到的一些东西(我知道pdfsharp已被更好的库取代),但在VB.net中,你必须使用+ vbNewLine而不是/ n – ShiftyThomas 2013-06-12 09:49:24

0

这是我做过什么,使用矩形类不包括:

我有一个定义的右侧限制和确定当前字符串是否会大于设定范围。如果是的话,我写了。否则,我继续增加它。

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 

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

+0

类似'XTextFormatter'类的方法,但不太灵活。 – 2017-06-29 17:24:52