批量打印文件可以靜默打印也可以非靜默打印
- 靜默打印時(shí)打印機(jī)為默認(rèn)打印機(jī),或者在知道打印機(jī)名稱的情況下指定參數(shù)打印
- 非靜默打印,彈出printdialog, 讓用戶選擇打印機(jī),之后指定打印機(jī)參數(shù)進(jìn)行打印
- 需要注意的是,printdialog應(yīng)當(dāng)使用system.form的控件,WPF的控件庫的printdialog無PrintDocument屬性
獲取打印機(jī)
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 編輯過