【发布时间】:2014-01-08 16:06:03
【问题描述】:
我想使用 C# 打印出一个文档。我有两个按钮。 btnUpload 上传或选择一个 word 文件。 btnPrint 必须将上传的文件发送到打印机。我怎样才能做到这一点?现在使用:
private void btnUpload_Click(object sender, EventArgs e)
{
string fileName;
// Show the dialog and get result.
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
fileName = ofd.FileName;
var application = new Microsoft.Office.Interop.Word.Application();
//var document = application.Documents.Open(@"D:\ICT.docx");
var document = application.Documents.Open(@fileName);
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "fileName";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
【问题讨论】:
-
请分享您迄今为止的研究。
-
选择文件检查OpenFileDialog class
-
@user3027412:你需要处理
PrintPage事件,在下面查看我的答案。