一行可以包含多少个字符?(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();
}
提前致谢。
Ok so I want to print the text of a text box but I have one problem when I have a line that is too big it goes out of the page how can I know how many chars a line can contains within the bounds, keep in mind that the size and font change.
I already have this code that I got from the web somewhere so you know what i want.
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();
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该阅读FontMetrics
一旦您知道如何获取字体规格,您可以将其与绘图区域结合使用来决定可以放置多少个字符。
编辑:
您可以按如下方式获取绘画区域的大小:
一旦获得页面的宽度,就可以从字体中获取字体度量,然后将页面宽度除以字体的宽度。以此为基础,这就是您可以在页面上水平放置的字符数。确保您使用相同的单位(宽度是默认像素)。如果需要,您可以对垂直方向执行相同的操作。
You should read about FontMetrics
Once you know how to get the font metric, you can use that in combination with your drawing area to decide how many characters you can place.
EDIT:
You can get the size of the painting area as follows:
Once you have the width of the page, you get the font metric from your font, then divide the page width by the font's width. Take the floor of that, and that is how many characters you can put horizontally on the page. Make sure that you are using the same units (width is default pixels). You can do the same thing for vertical if needed.
我使用这段代码可能会对您有所帮助。
I use this code that may help you.