换行文字中的问题。 [英] Problem in newline text.

查看:59
本文介绍了换行文字中的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码时,我希望有一个多行文字:



 使用(XGraphics gfx = XGraphics.FromPdfPage(page))
{
string text = aaa \\\ nbbb \ ncccc;
gfx.DrawString(text,font,XBrushes.Black, 20 20 );
}





事实上,当我使用GDI +绘制winform时,此文本呈现多行。有没有办法用PDF Sharp实现这一点?

解决方案

XGraphics.DrawString(...)方法不处理换行符(换行符),如.NET Graphics.DrawString(...)方法。

相反,PDFSharp将为\r和\ n输出类似空格的东西。



这对于解决这个问题有点棘手。你需要一个 XTextFormatter ,一个 XRect 用于可用于layouter的区域,然后在格式化程序而不是XGraphics对象上调用DrawString(...):



 var formatter =  new  XTextFormatter(pageGraphics); 
var layoutRectangle = new XRect( 10 10 ,page.Width,page.Height);
formatter.DrawString( Hello\\\\
World
,arial,XBrushes。黑色,layoutRectangle);





使用PDFSharp生成PDF [ ^


When I use this code I expect a multiline text:

using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
   string text = "aaa\r\nbbb\ncccc";
   gfx.DrawString(text, font, XBrushes.Black, 20, 20);
}



In fact, when I draw in a winform using GDI+ this text renders in multiple lines. Is there a way to achieve this with PDF Sharp?

解决方案

The XGraphics.DrawString(...) method does not handle line breaks (newlines) as the .NET Graphics.DrawString(…) method does.
Instead, PDFSharp will output something like blanks for \r and \n.

This is a little bit trickier to work around. You need an XTextFormatter, a XRect for the region which is available for the layouter and then call DrawString(…) on the formatter instead of the XGraphics object:

var formatter = new XTextFormatter(pageGraphics);
var layoutRectangle = new XRect(10, 10, page.Width, page.Height);
formatter.DrawString("Hello\r\nWorld", arial, XBrushes.Black, layoutRectangle);



Generating PDFs with PDFSharp[^]


这篇关于换行文字中的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆