13

我有的

我目前正在编写一个程序,该程序采用指定的文件并对其执行一些操作。目前它会打开它,和/或将其附加到电子邮件中并将其邮寄到指定的地址。

该文件可以是以下格式:Excel、Excel 报告、Word 或 PDF。

我目前正在做的是使用文件路径生成一个进程,然后启动该进程;但是,我也在尝试修复我添加的错误功能,该功能将动词“PrintTo”添加到启动信息中,具体取决于指定的设置。

我需要的

我要完成的任务是我希望打开文档,然后将其打印到程序本身命名的指定打印机上。之后,文件应该会自动关闭。

如果没有办法通用地做到这一点,我们也许可以想出一种方法来为每个单独的文件类型做到这一点。

你需要什么

这是我正在使用的代码:

ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
       // TODO: Add default printer.
    }

    pStartInfo.Verb = "PrintTo";
}

// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
    Process p = Process.Start(pStartInfo);
}

我过得怎么样...

仍然难倒......可能会像微软那样称呼它,“这是设计使然”。

4

3 回答 3

25

以下对我有用(使用 *.doc 和 *.docx 文件测试)

通过使用“System.Windows.Forms.PrintDialog”和“System.Diagnostics.ProcessStartInfo”出现 windows printto 对话框,我只使用选定的打印机 :)

只需将FILENAME替换为Office 文件的全名(路径+名称)即可。我认为这也适用于其他文件......

// 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);
    }
}
于 2011-03-25T13:14:47.020 回答
4

从理论上讲,根据MSDN 上的一篇文章,您应该能够将其更改为(未经测试):

// 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";
}
于 2011-03-03T15:32:19.883 回答
1

从罗兰·肖那里得到:</p>

               ProcessStartInfo startInfo = new ProcessStartInfo(Url)
                {
                    Verb = "PrintTo",
                    FileName = FilePath,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true,
                    Arguments = "\"" + PrinterName+ "\"",
                };
                Process.Start(startInfo);

文件路径看起来像 'D:\EECSystem\AttachedFilesUS\53976793.pdf'

PrinterName 是您的打印机名称

复制代码,它会工作。

于 2021-05-08T04:54:58.007 回答