我的目的是将数据库中文章内容读到流中,然后输出打印,可打迎的结果是空的.
System.IO.MemoryStream stream = new MemoryStream();
string content = reader.GetString(2);
byte[] byteArray = Encoding.Default.GetBytes(content);
stream.Write(byteArray,0,byteArray.Length);StartPrint(stream);
StartPrint()方法的定义如下public void StartPrint(Stream streamToPrint)
{
this.streamToPrint=streamToPrint;
// Allow the user to choose the page range he or she would
// like to print.
printDialog1.AllowSomePages = true; // Show the help button.
printDialog1.ShowHelp = true; // Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
printDialog1.Document = printDocument1;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例 DialogResult result = printDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框 // If the result is OK then print the document.
if (result==DialogResult.OK)
{
printDocument1.Print();//开始打印
} } 这跟内存流的编码什么的有关吗?
System.IO.MemoryStream stream = new MemoryStream();
string content = reader.GetString(2);
byte[] byteArray = Encoding.Default.GetBytes(content);
stream.Write(byteArray,0,byteArray.Length);StartPrint(stream);
StartPrint()方法的定义如下public void StartPrint(Stream streamToPrint)
{
this.streamToPrint=streamToPrint;
// Allow the user to choose the page range he or she would
// like to print.
printDialog1.AllowSomePages = true; // Show the help button.
printDialog1.ShowHelp = true; // Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
printDialog1.Document = printDocument1;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例 DialogResult result = printDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框 // If the result is OK then print the document.
if (result==DialogResult.OK)
{
printDocument1.Print();//开始打印
} } 这跟内存流的编码什么的有关吗?
{
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("宋体", 12, System.Drawing.FontStyle.Regular); // Draw the content.
System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
text=streamReader.ReadToEnd();
e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
}
{
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("宋体", 12, System.Drawing.FontStyle.Regular); // Draw the content.
System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
text=streamReader.ReadToEnd();
e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
} public void StartPrint(Stream streamToPrint)
{
this.streamToPrint=streamToPrint;
// Allow the user to choose the page range he or she would
// like to print.
printDialog1.AllowSomePages = true; // Show the help button.
printDialog1.ShowHelp = true; // Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
printDialog1.Document = printDocument1;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例 DialogResult result = printDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框 // If the result is OK then print the document.
if (result==DialogResult.OK)
{
printDocument1.Print();//开始打印
} } //这里是调用打印的地方
if(Convert.ToInt32(tabPage.Tag) != 0)
{
DBCon db = new DBCon();
OleDbDataReader reader = db.GetLawsContentByID(Convert.ToInt32(tabPage.Tag));
while(reader.Read())
{
System.IO.MemoryStream stream = new MemoryStream();
string content = reader.GetString(2);
byte[] byteArray = Encoding.Default.GetBytes(content);
stream.Write(byteArray,0,byteArray.Length); StartPrint(stream);
}
reader.Close();
}
string szContext="Visual Studio 2005";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Byte[] buffer=Encoding.Default.GetBytes(szContext);
ms.Write(buffer,0,buffer.Length);
ms.Position = 0;//你缺这句System.IO.StreamReader streamReader = new System.IO.StreamReader(ms);
string text = streamReader.ReadToEnd();
this.Text = text;
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);其次,字画的位置有问题,如果是在页内,用(0,0)试试;如果是header或者rooter,需要另外操作。
1.由于我在数据库中保存的内容是网页格式的内容,比如说<tr>这里是内容</tr>,打印的结果是原样打印出来,如何不将<tr>之类的打印出来呢,如<b></b>就打印成粗体等等.2.里面的汉字全部成了乱码,如何解决呢
An extended RichTextBox to save and load "HTML lite" files
http://www.codeproject.com/cs/miscctrl/htmlrichtextbox.asp然后再用抓屏的方式,进行打印。to 2.里面的汉字全部成了乱码,如何解决呢change
System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
with
System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint, Encoding.Default);
// 按下即可打印
private void button1_Click(object sender, EventArgs e)
{
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
if (this.stream == null)
{
this.stream = new System.IO.MemoryStream();
}
byte[] s = System.Text.Encoding.Default.GetBytes("hollo word".ToCharArray());
this.stream.Write(s, 0, s.Length);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
}
//打印
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (this.stream == null)
{
MessageBox.Show("Have no content!");
return;
}
byte[] s = this.stream.GetBuffer();
Graphics g = e.Graphics;
Font f = new Font("宋体", 30);
g.DrawString(System.Text.Encoding.Default.GetString(s), f, new SolidBrush(Color.Red), new PointF(10f, 10f));
}