我现在要把一个word文档读取出来,并显示该文档的打印预览!
这是我的代码(但是读取方式不对):public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Font printFont;
private StreamReader objStringReader; private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null; // Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file.
while (count < linesPerPage &&
((line = objStringReader.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
} // If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
printFont = new Font("Arial", 10);
objStringReader = new StreamReader(@"J:\temp1.doc"); //将需要打印的文档内容(赋给)字串流--该字串流将在被打印对象使用
PrintPreviewDialog objPrintPreviewDialog = new PrintPreviewDialog();
objPrintPreviewDialog.Document = this.printDocument1; //将previewdialog与printdocument相链接,pdoText就是printdocument
objPrintPreviewDialog.ShowDialog(); //显示预览窗口
objStringReader.Close();
}
catch (Exception exp)
{
if (objStringReader != null)
objStringReader.Close();
System.Console.WriteLine(exp.Message.ToString());
}
}
}怎么改啊
这是我的代码(但是读取方式不对):public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Font printFont;
private StreamReader objStringReader; private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null; // Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file.
while (count < linesPerPage &&
((line = objStringReader.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
} // If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
printFont = new Font("Arial", 10);
objStringReader = new StreamReader(@"J:\temp1.doc"); //将需要打印的文档内容(赋给)字串流--该字串流将在被打印对象使用
PrintPreviewDialog objPrintPreviewDialog = new PrintPreviewDialog();
objPrintPreviewDialog.Document = this.printDocument1; //将previewdialog与printdocument相链接,pdoText就是printdocument
objPrintPreviewDialog.ShowDialog(); //显示预览窗口
objStringReader.Close();
}
catch (Exception exp)
{
if (objStringReader != null)
objStringReader.Close();
System.Console.WriteLine(exp.Message.ToString());
}
}
}怎么改啊
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货