我正在尝试在Windows应用程序中使用C#打印.pdf和.tif文件。
打印成功完成,但我的问题是Adobe阅读器正在打开pdf文件的背景,并打开tif文件的Windows打印对话框。
实际上我将使用服务运行我的方法,因此这些过程应该以静默方式进行。我该怎么做才能避免这种情况?
这是我的代码
public void PrintDocument(string filepath)
{
//PrintDialog pd = new PrintDialog();
printProcess.StartInfo.FileName = filepath;
//Also tried usecellexcecution=false;
//Redirect=true; something like this
printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printProcess.Start();
printProcess.CloseMainWindow();
}
我曾尝试使用MSDN中建议的RawprinterHelper
,但它会打印垃圾字符。
答案 0 :(得分:1)
获取pdf进程的窗口句柄然后隐藏它或使用进程类的windowstyle来最小化它。
是的,因为您使用的是Process.Start,它会启动。
答案 1 :(得分:1)
如果您的打印机在网络上并且您知道它的IP地址,则可以使用TcpClient将文件直接发送到打印机。我已经将它用于我的打印机,但只是尝试过它用于PDF,所以我不知道它对其他打印机/文件类型的效果如何。
您必须更改打印机设置,使其为using a tcp port(在设备和打印机中选择您的打印机(单击),然后单击打印服务器属性,在打开的向导中,您可以添加新的TCP港口)。您还必须设置printer to raw rather than lpc设置
然后我使用了类似于以下方法的东西;
public void SilentPrint(string filePath, string printerIPAddress)
{
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
var client = new TcpClient(printerIPAddress, 9100);//9100 is the default print port for raw data
using (var stream = client.GetStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
}
答案 2 :(得分:0)
如果要以静默方式打印PDF,则需要使用正确的工具来完成工作。在这种情况下,它不是Adobe Reader,因为它将始终启动应用程序窗口,它不会以静默方式加载。
找一个可以静默地让你打印PDF的PDF库。
答案 3 :(得分:0)
尝试“PrintTo”作为你的动词。 这应该禁用'打印预览'阶段。