自动文字环绕文本到打印此页? [英] Automatically Word-Wrapping Text To A Print Page?

查看:157
本文介绍了自动文字环绕文本到打印此页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,打印字符串,但如果字符串说:等等等等等等......,没有换行符,文本所占用一行。我希望能够塑造字符串,所以它自动换行到纸张的尺寸。

I have some code that prints a string, but if the string is say: "Blah blah blah"... and there are no line breaks, the text occupies a single line. I would like to be able to shape the string so it word wraps to the dimensions of the paper.

private void PrintIt(){
    PrintDocument document = new PrintDocument();
    document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
    document.Print();
}

static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
    e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
}



我想我可以想出一个字符的长度,自动换行手动的,但如果在办法做到这一点内置,我宁愿做。谢谢!

I suppose I could figure out the length of a character, and wrap the text manually, but if there is a built in way to do this, I'd rather do that. Thanks!

推荐答案

是的,有是的的DrawString 有字自动换行文本的能力。您可以使用 MeasureString 的方法来检查wheather指定的字符串完全可以绘制在页面上或不和多少空间是必需的。

Yes there is the DrawString has ability to word wrap the Text automatically. You can use MeasureString method to check wheather the Specified string can completely Drawn on the Page or Not and how much space will be required.

还有一个 TextRenderer 类专门用于这一目的。

There is also a TextRenderer Class specially for this purpose.

下面是一个例子:

         Graphics gf = e.Graphics;
         SizeF sf = gf.MeasureString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), 60);
         gf.DrawString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), Brushes.Black,
                         new RectangleF(new PointF(4.0F,4.0F),sf), 
                         StringFormat.GenericTypographic);



在这里,我已经指定最多60像素的宽度,然后测量线会给我将要求尺寸绘制此字符串。现在,如果你已经有了一个尺寸,那么你可以用返回的大小比较,看它是否会被绘制正确或截断

Here i have specified maximum of 60 Pixels as width then measure string will give me Size that will be required to Draw this string. Now if you already have a Size then you can compare with returned Size to see if it will be Drawn properly or Truncated

这篇关于自动文字环绕文本到打印此页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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