使用Adobe Acrobat静默打印PDF

Col*_*e W 17 c# acrobat

当我尝试使用adobe acrobat在C#中静默打印pdf时,我遇到了两个问题.我正在使用Process.Start()打印pdf.

第一个问题是,如果没有指定可执行文件的完整路径,我就无法启动Adobe Acrobat.我假设它在安装时不会将其添加到您的路径中. 有没有一种简单的方法可以在机器上启动最新版本的acrobat而无需指定完整路径名? 我担心客户端会进行更新并破坏启动它的代码.我也关心他们在具有不同版本的Windows的机器上安装它(安装路径在64位环境与32位不同).

我的第二个问题是,无论何时我启动acrobat并打印它仍然会打开acrobat窗口.我认为我使用的命令行参数会抑制所有这些,但显然不是.

我正在尝试使用以下语法从命令行启动adobe acrobat:

C:\ Program Files(x86)\ Adob​​e\Reader 10.0\Reader> AcroRd32.exe/t"Label.pdf""HP4000""HP LaserJet 4100 Series PCL6""out.pdf"

它打印出来很好但它仍然离开了acrobat窗口. 除了外出并以编程方式杀死进程之外还有其他解决方案吗?

Col*_*e W 26

我最终在Adobe Acrobat上乞讨,并使用FoxIt Reader(免费pdf阅读器)进行我的pdf打印.这是我用来在C#中通过FoxIt打印的代码:

Process pdfProcess = new Process();
pdfProcess.StartInfo.FileName = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";
pdfProcess.StartInfo.Arguments = string.Format(@"-p {0}", fileNameToSave);
pdfProcess.Start();
Run Code Online (Sandbox Code Playgroud)

上面的代码打印到默认打印机,但有一些命令行参数可用于指定文件和打印机.您可以使用以下语法:

Foxit Reader.exe -t"pdf filename""打印机名称"

更新:

显然早期版本的acrobat也没有上面列出的问题.如果您使用更旧的版本(4.x或类似的东西),它不会出现此问题.

有些打印机也支持原生pdf打印,因此可以将原始pdf数据发送到打印机并打印出来.有关将原始数据发送到打印机的信息,请参阅https://support.microsoft.com/en-us/kb/322091.

更新2

在我们软件的更高版本中,我们最终使用付费产品:

http://www.pdfprinting.net/

  • 我很确定最新版本的FoxIt不再支持.我认为他们此时已将其集成到付费版本中. (2认同)

小智 9

尼克的回答看起来很好,所以我把它翻译成c#.有用!

using System.Diagnostics;

namespace Whatever
{
static class pdfPrint
{
    public static void pdfTest(string pdfFileName)
    {
       string processFilename = Microsoft.Win32.Registry.LocalMachine
            .OpenSubKey("Software")
            .OpenSubKey("Microsoft")
            .OpenSubKey("Windows")
            .OpenSubKey("CurrentVersion")
            .OpenSubKey("App Paths")
            .OpenSubKey("AcroRd32.exe")
            .GetValue(String.Empty).ToString();

        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = processFilename;
        info.Arguments = String.Format("/p /h {0}", pdfFileName);
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden; 
        //(It won't be hidden anyway... thanks Adobe!)
        info.UseShellExecute = false;

        Process p = Process.Start(info);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        int counter = 0;
        while (!p.HasExited)
        {
            System.Threading.Thread.Sleep(1000);
            counter += 1;
            if (counter == 5) break;
        }
        if (!p.HasExited)
        {
            p.CloseMainWindow();
            p.Kill();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}


Chr*_*uer 8

以下内容在Acrobat Reader 8.1.3和Acrobat Pro 11.0.06中进行了测试,并确认了以下功能:

  1. 在系统上找到默认的Acrobat可执行文件
  2. 将文件发送到本地打印机
  3. 关闭Acrobat,无论版本如何

string applicationPath;

var printApplicationRegistryPaths = new[]
{
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe",
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRD32.exe"
};

foreach (var printApplicationRegistryPath in printApplicationRegistryPaths)
{
    using (var regKeyAppRoot = Registry.LocalMachine.OpenSubKey(printApplicationRegistryPath))
    {
        if (regKeyAppRoot == null)
        {
            continue;
        }

        applicationPath = (string)regKeyAppRoot.GetValue(null); 

        if (!string.IsNullOrEmpty(applicationPath))
        {
            return applicationPath;
        }
    }
}

// Print to Acrobat
const string flagNoSplashScreen = "/s";
const string flagOpenMinimized = "/h";

var flagPrintFileToPrinter = string.Format("/t \"{0}\" \"{1}\"", printFileName, printerName); 

var args = string.Format("{0} {1} {2}", flagNoSplashScreen, flagOpenMinimized, flagPrintFileToPrinter);

var startInfo = new ProcessStartInfo
{
    FileName = printApplicationPath, 
    Arguments = args, 
    CreateNoWindow = true, 
    ErrorDialog = false, 
    UseShellExecute = false, 
    WindowStyle = ProcessWindowStyle.Hidden
};

var process = Process.Start(startInfo);

// Close Acrobat regardless of version
if (process != null)
{
    process.WaitForInputIdle();
    process.CloseMainWindow();
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我使用福昕阅读器很有用.版本7存在无提示打印问题,因此我使用了WaitForExit(5000)和process.Kill(). (2认同)

Fre*_*all 6

我没有运气就试过Adobe Reader和Foxit.两者的当前版本都非常喜欢弹出窗口并让进程保持运行.结束使用苏门答腊PDF非常不引人注目.这是我使用的代码.当打印完成后,没有任何窗口和进程的痕迹很好地退出.

    public static void SumatraPrint(string pdfFile, string printer)
    {
        var exePath = Registry.LocalMachine.OpenSubKey(
            @"SOFTWARE\Microsoft\Windows\CurrentVersion" +
            @"\App Paths\SumatraPDF.exe").GetValue("").ToString();

        var args = $"-print-to \"{printer}\" {pdfFile}";

        var process = Process.Start(exePath, args);
        process.WaitForExit();
    }
Run Code Online (Sandbox Code Playgroud)