高分求救!!printdocument实例

tangchengxing 2005-09-08 05:44:20
高分求救!!printdocument实例,调试了两天了,printdocument控件就是不能进行打印,使用了微软网站上的代码也不能进行打印,高分请各位高手帮忙!
...全文
451 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovefootball 2005-09-09
  • 打赏
  • 举报
回复
给你的代码就是一个完整的.cs文件
如果你打开文件操作可以进行
那么打印是没问题的
我这边都测试好的
zhgroup 2005-09-09
  • 打赏
  • 举报
回复
跟踪一下看看吧,
tangchengxing 2005-09-09
  • 打赏
  • 举报
回复
为什么在我的机器上打印出来的都是空的文件呀,并且打印预览功能也不能实现呀,打开时是一个空的预览页呀
tangchengxing 2005-09-09
  • 打赏
  • 举报
回复
为什么打印代码在我机器上不行呀
tangchengxing 2005-09-09
  • 打赏
  • 举报
回复
有没有人能帮我呀
anchky 2005-09-09
  • 打赏
  • 举报
回复

tangchengxing 2005-09-09
  • 打赏
  • 举报
回复
tangjanghai@163.com这个是我的邮箱,能不能发一个到实例到我的邮箱里呀,我把代码贴上去还是不能运行呀
lovefootball 2005-09-08
  • 打赏
  • 举报
回复
把它拷贝到一个.cs文件
就可以运行看效果
包括打开文件,保存文件,打印文件,打印预览等等
TechEye 2005-09-08
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, System.EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
PrintPreviewDialog prvDlg = new PrintPreviewDialog();
prvDlg.Document = doc;
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
(prvDlg as Form).WindowState = FormWindowState.Maximized;
prvDlg.ShowDialog();


}

void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

e.Graphics.DrawString("This is test String", this.Font, new SolidBrush(Color.Blue), 10, 10);
}
lovefootball 2005-09-08
  • 打赏
  • 举报
回复
private void button1_Click (object sender,System.EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream (openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin );
this.richTextBox1.Text = "";
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
this.richTextBox1.Text += strLine + "\n";
strLine = m_streamReader.ReadLine();
}
m_streamReader.Close();
}
}
catch (Exception em)
{
Console.WriteLine(em.Message.ToString()) ;
}

}

private void button3_Click (object sender,System.EventArgs e)
{
try
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream (@saveFileDialog1.FileName,FileMode.OpenOrCreate , FileAccess.Write ) ;
StreamWriter m_streamWriter = new StreamWriter(fs) ;
m_streamWriter.Flush ();
m_streamWriter.BaseStream.Seek (0,SeekOrigin.Begin);
m_streamWriter.Write (richTextBox1.Text);
m_streamWriter.Flush ();
m_streamWriter.Close ();
}
}
catch (Exception em)
{
Console.WriteLine (em.Message.ToString()) ;
}
}

private void button4_Click (object sender,System.EventArgs e)
{
printDialog1.Document = ThePrintDocument;
printDialog1.ShowDialog ();
}

private void button2_Click (object sender,System.EventArgs e)
{
try
{
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = ThePrintDocument;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog();
}
catch(Exception exp)
{
Console.WriteLine(exp.Message.ToString());
}
}

private void button5_Click ( object sender , System.EventArgs e )
{
printDialog1.Document = ThePrintDocument ;
if ( printDialog1.ShowDialog () == DialogResult.OK )
{
ThePrintDocument.Print();
}
}
protected void ThePrintDocument_PrintPage ( object sender , PrintPageEventArgs ev )
{
string strText = richTextBox1.Text ;
myReader = new StringReader(strText);
float linesPerPage = 0 ;
float yPosition = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left ;
float topMargin = ev.MarginBounds.Top ;
string line = null ;
Font printFont = richTextBox1.Font ;
SolidBrush myBrush = new SolidBrush ( Color.Black ) ;
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight ( ev.Graphics ) ;
while ( count < linesPerPage && ( ( line = myReader.ReadLine ( ) ) != null ) )
{
yPosition = topMargin + ( count * printFont.GetHeight ( ev.Graphics ) ) ;
ev.Graphics.DrawString(line,printFont,myBrush,leftMargin,yPosition,new StringFormat ( ) ) ;
count++ ;
}
if ( line != null )
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
myBrush.Dispose();
}
}
}
wuyi8808 2005-09-08
  • 打赏
  • 举报
回复
namespace Skyiv
{
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

class PrintBill : PrintDocument
{
public DataTable dtUser; // 要打印的用户信息数据表
public DataTable dtBill; // 要打印的对帐单数据表
protected PageSetupDialog setdlg; // 页面设置对话框

int iPageNo; // 当前页号
int iStart; // dtBill的起始行
int iEnd; // dtBill的结束行
int iCurrent; // dtBill的当前行

// 构造函数
public PrintBill()
{
DocumentName = Pub.softName;
PrinterSettings.PrinterName = Pub.PrinterName;
DefaultPageSettings.Margins = new Margins(10,10,10,10);

setdlg = new PageSetupDialog();
setdlg.Document = this;
}

// 打印开始时
protected override void OnBeginPrint(PrintEventArgs pea)
{
base.OnBeginPrint(pea);

iPageNo = 0;
iCurrent = iStart;
}

// 打印每一页时
protected override void OnPrintPage(PrintPageEventArgs ppea)
{
base.OnPrintPage(ppea);

Graphics g = ppea.Graphics;
g.PageUnit = GraphicsUnit.Inch;
g.PageScale = 0.01F;
Rectangle mb = ppea.MarginBounds;

float cx0 = mb.Width;
float cy0 = mb.Height;
float cx = 960;
float cy = cy0 * cx / cx0;
g.TranslateTransform(mb.Left, mb.Top);
g.PageScale /= (cx / cx0);
PaintPage(g, cx, cy);
++iPageNo;
ppea.HasMorePages = (iCurrent < iEnd);
}

float [] x = { 0,40,130,210,290,370,450,530,610,690,770,850,930 };
float [] y = { 0,45,65,90 };
float dy = 25; // 每行高度

// 具体画每一页
void PaintPage(Graphics g, float cx, float cy)
{
Font f0 = new Font("宋体", 24, GraphicsUnit.Pixel);
Font f = new Font("宋体", 13, GraphicsUnit.Pixel);
SolidBrush b = new SolidBrush(Pub.clrPrint);
Pen p = new Pen(Pub.clrPrint);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

// ...
}

// 属性: 开始行
public int RowStart
{
get { return iStart; }
set { iStart = value; }
}

// 属性: 结束行
public int RowEnd
{
get { return iEnd; }
set { iEnd = value; }
}

// 只读属性: 页面设置对话框
public PageSetupDialog PageSetupDlg
{
get { return setdlg; }
}
}
}
lovefootball 2005-09-08
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Printing;
namespace testApplication1
{
public class preview : Form
{
private RichTextBox richTextBox1 ;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private OpenFileDialog openFileDialog1;
private SaveFileDialog saveFileDialog1;
private PrintDialog printDialog1;
private PrintDocument ThePrintDocument;
private PrintPreviewDialog printPreviewDialog1;
private StringReader myReader;
private System.ComponentModel.Container components = null;

public preview ()
{
InitializeComponent();
}
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
richTextBox1 = new RichTextBox ();
button1 = new Button();
button2 = new Button();
button3 = new Button();
button4 = new Button();
button5 = new Button();
saveFileDialog1 = new SaveFileDialog();
openFileDialog1 = new OpenFileDialog();
printPreviewDialog1 = new PrintPreviewDialog();
printDialog1 = new PrintDialog () ;
ThePrintDocument = new PrintDocument();
ThePrintDocument.PrintPage += new PrintPageEventHandler (ThePrintDocument_PrintPage);
SuspendLayout();
richTextBox1.Anchor = AnchorStyles.None;
richTextBox1.Name = "richTextBox1";
richTextBox1.Size = new Size(448,280);
richTextBox1.TabIndex = 0;
richTextBox1.Text = "";
button1.Anchor = AnchorStyles.None;
button1.Location = new Point(41,289);
button1.Name = "button1";
button1.Size = new Size (48,30);
button1.TabIndex = 1;
button1.Text = "open";
button1.Click += new System.EventHandler(button1_Click);
button2.Anchor = AnchorStyles.None;
button2.Location = new Point(274,288);
button2.Name = "button2";
button2.Size = new Size(48,30);
button2.TabIndex = 4;
button2.Text = "preview";
button2.Click += new System.EventHandler(button2_Click);
button3.Anchor = AnchorStyles.None;
button3.Location = new Point(108,288);
button3.Name = "button3" ;
button3.Size = new Size(48,30);
button3.TabIndex = 2 ;
button3.Text = "save" ;
button3.Click += new System.EventHandler(button3_Click);
button4.Anchor = AnchorStyles.None;
button4.Location = new Point(174,288);
button4.Name = "button4";
button4.Size = new Size(80,30);
button4.TabIndex = 3;
button4.Text = "setupprinter";
button4.Click += new System.EventHandler(button4_Click);
button5.Anchor = AnchorStyles.None;
button5.Location = new Point(345,288);
button5.Name = "button5";
button5.Size = new Size(48,30);
button5.TabIndex = 5;
button5.Text = "print";
button5.Click += new System.EventHandler(button5_Click);
saveFileDialog1.DefaultExt = "*.txt";
saveFileDialog1.FileName = "file.txt";
saveFileDialog1.InitialDirectory = "c:\\";
saveFileDialog1.Title = "save other";
openFileDialog1.DefaultExt = "*.txt";
openFileDialog1.FileName = "file.txt";
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Title = "open txt file";
AutoScaleBaseSize = new Size(6,14);
ClientSize = new Size(448,325);
this.Controls.Add(button1);
this.Controls.Add(button2);
this.Controls.Add(button3);
this.Controls.Add(button4);
this.Controls.Add(button5);
this.Controls.Add(richTextBox1);

this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "C#process file";
this.ResumeLayout(false);
}

tangchengxing 2005-09-08
  • 打赏
  • 举报
回复
最近在赶一个任务急用,谢谢各位了!
tangchengxing 2005-09-08
  • 打赏
  • 举报
回复
请各位高手能够提供一个例子,谢谢了,也可以提供代码!
新鲜鱼排 2005-09-08
  • 打赏
  • 举报
回复
mark

110,525

社区成员

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

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

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