我正在打开PrintDialog
用户可以选择打印机设置并进行打印。
目前我正在使用以下代码
var files = Directory.GetFiles(sourceFolder);
foreach (var file in files)
{
var pdoc = new PrintDocument();
var pdi = new PrintDialog
{
Document = pdoc
};
if (pdi.ShowDialog() == DialogResult.OK)
{
pdoc.DocumentName = file;
pdoc.Print();
}
}
有没有办法使用PrintDialog
一次将所有文件发送到打印机。因此,用户可以选择文件夹并为文件夹内的所有文档设置一个打印设置,然后进行打印?
答案 0 :(得分:1)
试试这个示例代码:
var files = Directory.GetFiles(sourceFolder);
if (files.Length != 0)
{
using (var pdoc = new PrintDocument())
using (var pdi = new PrintDialog { Document = pdoc, UseEXDialog = true })
{
if (pdi.ShowDialog() == DialogResult.OK)
{
pdoc.PrinterSettings = pdi.PrinterSettings;
// ************************************
// Pay attention to the following line:
pdoc.PrintPage += pd_PrintPage;
// ************************************
foreach (var file in files)
{
pdoc.DocumentName = file;
pdoc.Print();
}
}
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
string file = ((PrintDocument)sender).DocumentName; // Current file name
// Do printing of the Document
...
}