一行可以包含多少个字符?(C#打印文本)
问题描述:
好吧,我想打印一个文本框的文本,但我有一个问题,当我有一个太大的行,它超出了页面我知道一行可以包含多少个字符,请记住大小和字体更改。一行可以包含多少个字符?(C#打印文本)
我已经有了这个代码,我从网上得到的所以你知道我想要什么。
private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
Font printFont = txtMain.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
// Iterate over the string using the StringReader, printing each line.
while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
{
// calculate the next line position based on the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
// draw the next line in the rich edit control
e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
myBrush.Dispose();
}
在此先感谢。
答
你应该阅读有关FontMetrics
一旦你知道如何获得字体的度量,您可以使用结合你的绘图区域来决定你可以有多少个字符的地方。
编辑: 你可以得到的绘画区域的大小如下:
//This gives you a rectangle object with a length and width.
Rectangle bounds = e.MarginBounds;
一旦你的页面的宽度,你从你的字体得到的字体度量,然后除以页面宽度字体的宽度。以此为基础,这就是您可以在页面上水平放置多少个字符。确保您使用的是相同的单位(宽度是默认像素)。如果需要,你可以做同样的事情。
答
我使用这段代码可以帮助你。
private string txt(string yourtext)
{
string[] text = new string[200];
text = yourtext.ToLower().Split(' ');
string b = "";
int i = 1;
foreach (string s in text)
{
if (b.Length < i * 100)
b += s + " ";
else
{
b += "\n";
i++;
}
}
return b;
}
坦克我还没有尝试过,但它似乎是我需要的 – Wergenwolf 2012-02-16 20:04:16
回复如果你需要更多的方向。 – crush 2012-02-16 20:04:39