打印本地文件

baisun 2009-09-07 09:24:02
如果自动打印本地文件(比如word)?
例如,选择一个本地文件后,点打印,就自动从打印机打印出来(打印机已设置好)?
大概代码是?
...全文
150 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
baisun 2009-09-11
  • 打赏
  • 举报
回复
调用 PrintDocument的Print()后,能否获取打印完毕或打印成功或已送到打印机队列的消息?
boywangliang 2009-09-09
  • 打赏
  • 举报
回复
up
reachen 2009-09-09
  • 打赏
  • 举报
回复
mark下
子夜1978 2009-09-09
  • 打赏
  • 举报
回复
向楼主学习~~
LutzMark 2009-09-09
  • 打赏
  • 举报
回复
System.Drawing.Printing.PrintDocument
风之影子 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baisun 的回复:]
比如打印html页面,利用javascript可以自动打印
[/Quote]

你的html是打开的.

而你现在文件只有个路经.文件类型是什么,内容是什么.


简单点的无非就是用PrintDocument来画
可以考虑把要打印的文本拷贝到打印机


baisun 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 baisun 的回复:]
调用 PrintDocument的Print()后,能否获取打印完毕或打印成功或已送到打印机队列的消息?
[/Quote]
这个
w_f_y4422 2009-09-08
  • 打赏
  • 举报
回复
lz的想法很好,但实现起来有点矛盾,除非是XML,TXT等可以存储存纯数据的文件,可供不同程序共享,像WORD那种自带格式的,不用接口的话,我觉得是不可能的,因为数据并不能共享。
w_f_y4422 2009-09-08
  • 打赏
  • 举报
回复
我觉得要调用本地文件的应用程序的接口吧,比如WORD文档就要用互操作性,如果单纯地用PrintDocument,原来文件的格式不是丢失了吗?
baisun 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lsj_zrp 的回复:]
简单点的无非就是用PrintDocument来画
可以考虑把要打印的文本拷贝到打印机
[/Quote]
这个?
lsj_zrp 2009-09-08
  • 打赏
  • 举报
回复
如果是纯文本或者是纯图片的都好说
如果是两者混合的,估计就比较麻烦
简单点的无非就是用PrintDocument来画
可以考虑把要打印的文本拷贝到打印机
liffe 2009-09-08
  • 打赏
  • 举报
回复
bangding
mjp1234airen4385 2009-09-08
  • 打赏
  • 举报
回复
不同的文件需要不同的打开格式支持,
你随便那一个文件来,不可能正确打印。
kaizi_sun 2009-09-08
  • 打赏
  • 举报
回复
ding
baisun 2009-09-08
  • 打赏
  • 举报
回复
调用 PrintDocument的Print()后,能否获取打印完毕或打印成功或已送到打印机队列的消息?
peng_yaoming 2009-09-07
  • 打赏
  • 举报
回复
4 6 楼都可以参考
cnzdgs 2009-09-07
  • 打赏
  • 举报
回复
用PrintDocument类。
starj1 2009-09-07
  • 打赏
  • 举报
回复
查一下系统本身有的右键菜单上的"打印"按钮是怎么调用程序和设置参数的
gisyellow 2009-09-07
  • 打赏
  • 举报
回复
主要使用System.Drawing.Printing.PrintDocument、System.Windows.Forms.PrintDialog等

参考:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Drawing.Printing.PrintDocument pd;
private PrintPreviewControl ppc;
private PrintPreviewDialog ppd;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;

String text="";
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pd = new System.Drawing.Printing.PrintDocument();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 154);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "开始打印";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 154);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "打印预览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(208, 154);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "打印机设置";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(270, 116);
this.textBox1.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(314, 199);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "打印窗体";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
//创建实例
this.pd=new System.Drawing.Printing.PrintDocument();
this.ppc=new PrintPreviewControl();
this.ppd=new PrintPreviewDialog();
this.printDialog1=new PrintDialog();

//触发事件
this.pd.BeginPrint+=new System.Drawing.Printing.PrintEventHandler(pd_BeginPrint);
this.pd.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);


}

private void pd_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//设置横向打印
this.pd.DefaultPageSettings.Landscape=true;

//设置彩色打印
this.pd.DefaultPageSettings.Color=true;

//设置打印纸张类型和大小
this.pd.DefaultPageSettings.PaperSize=new System.Drawing.Printing.PaperSize("A4",800,1100);


}

private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//获取文本框的内容绘制图形传到打印机打印
text=this.textBox1.Text;
e.Graphics.DrawString(text,
new Font("宋体",30, FontStyle.Regular), Brushes.Black, 0, 0);

}

private void button1_Click(object sender, System.EventArgs e)
{
//开始打印
this.pd.Print();

}

private void button2_Click(object sender, System.EventArgs e)
{
//设置打印预览信息
ppc.Document=pd;
ppc.Columns=2;
ppc.Rows=2;
ppc.Zoom=0.5;
ppc.StartPage=1;

//显示预览
ppd.Document=pd;
try
{
ppd.ShowDialog();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


}

private void button3_Click(object sender, System.EventArgs e)
{
//打印机设置
this.printDialog1.Document=pd;
this.printDialog1.AllowSomePages=true;
this.printDialog1.PrintToFile=false;
//确定打印机信息后开始打印
if(this.printDialog1.ShowDialog()==DialogResult.OK)
{
try
{
this.pd.Print();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
baisun 2009-09-07
  • 打赏
  • 举报
回复
比如打印html页面,利用javascript可以自动打印
加载更多回复(2)

111,086

社区成员

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

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

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