[点晴永久免费OA]C# 指定打印机批量打印文件:静默/非静默
|
admin
2022年7月2日 12:22
本文热度 1860
|
批量打印文件可以静默打印也可以非静默打印
- 静默打印时打印机为默认打印机,或者在知道打印机名称的情况下指定参数打印
- 非静默打印,弹出printdialog, 让用户选择打印机,之后指定打印机参数进行打印
- 需要注意的是,printdialog应当使用system.form的控件,WPF的控件库的printdialog无PrintDocument属性
获取打印机
public string GetPrinter()
{
var printer = string.Empty;
var printDialog = new formPrintDialog()
{
Document = new PrintDocument()
};
var dialogRet = printDialog.ShowDialog();
if (dialogRet == System.Windows.Forms.DialogResult.OK || dialogRet == System.Windows.Forms.DialogResult.Yes)
{
printer = printDialog.Document .PrinterSettings.PrinterName;
}
return printer;
}
CMD打印
# windows打印指令,可以使用print help查看详情
var args = "print /D:\"" + printerName + "\" " + file;
var process = new Process()
{StartInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
createNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellexecute = false
}};
process.Start();
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine(args + "&exit");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
该文章在 2022/7/2 16:47:08 编辑过