显示一个带有组合框的小对话框,该组合框的 Items 设置为返回的字符串集合PrinterSettings.InstalledPrinters
。
如果您可以要求在机器上安装GSView,则可以静默打印 PDF。它有点慢和迂回,但至少你不必弹出 Acrobat。
这是我用来打印从 UPS Web 服务返回的一些 PDF 的一些代码:
private void PrintFormPdfData(byte[] formPdfData)
{
string tempFile;
tempFile = Path.GetTempFileName();
using (FileStream fs = new FileStream(tempFile, FileMode.Create))
{
fs.Write(formPdfData, 0, formPdfData.Length);
fs.Flush();
}
try
{
string gsArguments;
string gsLocation;
ProcessStartInfo gsProcessInfo;
Process gsProcess;
gsArguments = string.Format("-grey -noquery -printer \"HP LaserJet 5M\" \"{0}\"", tempFile);
gsLocation = @"C:\Program Files\Ghostgum\gsview\gsprint.exe";
gsProcessInfo = new ProcessStartInfo();
gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
gsProcessInfo.FileName = gsLocation;
gsProcessInfo.Arguments = gsArguments;
gsProcess = Process.Start(gsProcessInfo);
gsProcess.WaitForExit();
}
finally
{
File.Delete(tempFile);
}
}
如您所见,它将 PDF 数据作为字节数组,将其写入临时文件,然后启动 gsprint.exe 以静默方式将文件打印到指定的打印机(“HP Laserjet 5M”)。您可以用用户在对话框中选择的任何内容替换打印机名称。
打印 PNG 或 GIF 会容易得多——只需扩展 PrintDocument 类并使用 Windows 窗体提供的正常打印对话框。
祝你好运!