关于打印获取自动换行后每行的字符串内容

pp_shy 2008-08-28 04:44:24
在做打印的时候,可以通过Graphics.MeasureString和Graphics.DrawString对长度过长的文本进行自动换行处理,但如何获取自动换行后每一行字符串的具体内容啊?
(注:不希望通过用Graphics.MeasureString来计算每个字符长度从而来判断每一行的字符内容的方法)
...全文
510 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiddkiller 2008-09-03
  • 打赏
  • 举报
回复
UP
LQknife 2008-09-02
  • 打赏
  • 举报
回复
帮你顶
hanyu0528 2008-09-02
  • 打赏
  • 举报
回复
还真不懂啊,有没高人来解决啊,偶也想知道,帮顶~!!!
pp_shy 2008-09-02
  • 打赏
  • 举报
回复
UP
hiddkiller 2008-08-29
  • 打赏
  • 举报
回复
打印文本文件的例子 看看有帮助没有!
对了 我也有个问题 没事帮我看看吧 呵呵 互相帮助!

http://topic.csdn.net/u/20080828/17/b6f9e485-c210-422a-adae-86451ad3d8a9.html


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace WindowsApplication2
{
public partial class PrintingExample : Form
{
public PrintingExample()
{
InitializeComponent();
}
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
public static float Scalt=(float)1;
private Font printFont;
private StreamReader streamToPrint;
bool flag = true;
ArrayList arraylist;
int count = 0;
string filePath;
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "Text file(*.txt)|*.txt|All files(*.*)|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
filePath = openFileDialog1.FileName;

}
catch (Exception error)
{
MessageBox.Show("错误信息是: " + error.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


try
{
if(textBox1.Text.Trim() !=null)
Scalt = float.Parse(textBox1.Text.Trim());
streamToPrint = new StreamReader
(filePath, Encoding.GetEncoding("gb2312"));
arraylist = new ArrayList();
count = 0;
flag = true;
printFont = new Font("Tahoma", 10 * Scalt);
try
{
//printFont = new Font("Tahoma", 10 * Scalt);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage);
PrintPreviewDialog dig = new PrintPreviewDialog();
dig.Document = pd;
dig.ShowDialog();
//pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
int linesPerPage = (int) (ev.MarginBounds.Height /printFont.GetHeight(ev.Graphics));
float yPos = 0;

float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;

string line = null;
string linePage=null;

if (flag)
{
int FontSize = (int)(ev.PageBounds.Width / ev.Graphics.MeasureString("啊", printFont).Width);
while (
((line = streamToPrint.ReadLine()) != null))
{
int rows =line.Length / FontSize;
int leavings = line.Length % FontSize;
if (rows > 0)
{
for (int i = 0; i <= rows;i++ )
{
if (leavings > 0 && i == rows)
{
linePage = line.Substring(i * FontSize);
}
else if (leavings ==0 && i==rows)
{
break;
}
else
{
linePage = line.Substring(i * FontSize, FontSize);
}
arraylist.Add(linePage);
}
}
else
{

arraylist.Add(line);
}
}
flag = false;
}
int page = 0;
while (
count < arraylist.Count)
{
linePage = (string)arraylist[count];
yPos = topMargin + (page*
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(linePage, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
page++;
if(((count%linesPerPage)==0)&&(count>=linesPerPage))
break;
}
if (count <arraylist.Count)
ev.HasMorePages = true;

else
ev.HasMorePages = false;
}

private void PrintingExample_Load(object sender, EventArgs e)
{
this.textBox1.Text = "1";
}

private void PrintingExample_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}


}
}
hiddkiller 2008-08-29
  • 打赏
  • 举报
回复
StreamReader类是对文件操作的!
自己想想, 用SubString 截取换行符 就OK了!
换行符号 好像是 \r
hiddkiller 2008-08-29
  • 打赏
  • 举报
回复
你换行是在字符串里加 换行符 还是在打印的时候另起一行呢?

如果是加了换行符
用 StreamReader类的实例.ReadLine() 可获得(读取一行);

通过Graphics.MeasureString和Graphics.DrawString对长度过长的文本进行自动换行的时候 加换行符就行了啊..

pp_shy 2008-08-29
  • 打赏
  • 举报
回复
有人知道吗?
pp_shy 2008-08-29
  • 打赏
  • 举报
回复
该问题我发了2贴共400分,解决后立马给分
pp_shy 2008-08-29
  • 打赏
  • 举报
回复
楼上的不要意思,没有办法加换行符号,我是打印自绘的界面,让用户自己加换行符号不现实啊
spgoal 2008-08-28
  • 打赏
  • 举报
回复
不懂,帮顶
格拉 2008-08-28
  • 打赏
  • 举报
回复
占个SF!帮顶!

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧