Is there anyway to specify a PrintTo printer when spawning a process?

痴心易碎 提交于 2019-11-30 12:42:20

The following works for me (tested with *.doc and *.docx files)

the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)

just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
        info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
        info.CreateNoWindow = true;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        info.UseShellExecute = true;
        info.Verb = "PrintTo";
        System.Diagnostics.Process.Start(info);
    }
}

Theoretically, according to an article on MSDN you should be able to change it to be along the lines of (untested):

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
        pStartInfo.Arguments = "\"" + PrinterName + "\"";
    }

    pStartInfo.CreateNoWindow = true;
    pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pStartInfo.UseShellExecute = true;
    pStartInfo.WorkingDirectory = sDocPath;

    pStartInfo.Verb = "PrintTo";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!