【问题标题】:C# - Print a PDF Error (System.ComponentModel.Win32Exception:...)C# - 打印 PDF 错误 (System.ComponentModel.Win32Exception:...)
【发布时间】:2021-01-18 14:41:50
【问题描述】:

我正在尝试编写一个打印 PDF 的应用程序。 现在我收到一条错误消息,经过长时间的搜索,我无法进一步了解。

目标是在不打开 PDF 阅读器的情况下打印 PDF 文件。

我的代码:

using System.Diagnostics;



private void button1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = @"C:\testFile.pdf";
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;

            Process p = new Process();
            p.StartInfo = info;
            p.Start();

            p.WaitForInputIdle();
            System.Threading.Thread.Sleep(3000);
            if (false == p.CloseMainWindow())
                p.Kill();
        }

错误情况发生在 p.Start(); 行。

错误信息:

System.ComponentModel.Win32Exception:“系统找不到 指定文件”。

新体验:20.01.2021

如果我注释掉info.Verb = "print";。然后打开 PDF。 这是否表明它找到了 PDF 但无法访问打印机?

【问题讨论】:

  • 你在 c: 有一个名为 testFile.pdf 的文件吗?
  • 尝试使用比系统根目录限制更少的位置。
  • @Mangs 是的,路径是正确的
  • @Alex K. 我会尝试.. 但这与它有什么关系?我不想保存任何东西,我只想打印它。如果我没有正确理解,我很抱歉。我还是初学者。
  • 不太确定这将如何工作,但错误消息告诉您它找不到文件或无法访问它。因此,不同的路径可以帮助您。

标签: c# pdf printing


【解决方案1】:

使用此代码对我有用:

    public static void PrintToASpecificPirnter()
    {
        using (PrintDialog printDialog = new PrintDialog())
        {
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                var StartInfo = new ProcessStartInfo();
                StartInfo.CreateNoWindow = true;
                StartInfo.UseShellExecute = true;
                StartInfo.Verb = "printTo";
                StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
                StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                StartInfo.FileName = fileName;

                Process.Start(StartInfo);
            }
        }
    }

【讨论】:

  • fileName 是文件名和路径。
猜你喜欢
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2010-09-30
  • 2012-06-02
相关资源
最近更新 更多