使用 Adob​​e Acrobat 静默打印 PDF

发布于 2024-10-15 03:24:14 字数 662 浏览 5 评论 0原文

我在尝试使用 adobe acrobat 在 C# 中以静默方式打印 pdf 时遇到 2 个问题。我正在使用 Process.Start() 打印 pdf。

第一个问题是,如果不指定可执行文件的完整路径,我将无法启动 Adob​​e Acrobat。我认为当您安装它时它不会将其添加到您的路径中。 是否有一种简单的方法可以在不指定完整路径名的情况下在计算机上启动最新版本的 acrobat? 我担心客户端会进行更新并破坏我启动此版本的代码。我还担心他们在具有不同版本 Windows 的计算机上安装此程序(64 位环境与 32 位环境中的安装路径不同)。

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

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

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

打印效果很好,但 acrobat 窗口仍然处于打开状态。 除了以编程方式终止进程之外,还有其他解决方案吗?

I'm having 2 issues when trying to print a pdf silently in C# using adobe acrobat. I'm printing the pdfs using Process.Start().

The first issue is that I cannot launch Adobe Acrobat without specifying the full path to the executable. I assume it doesn't add it to your path when you install it. Is there an easy way to launch the newest version of acrobat on a machine without specifying full path names? I'm worried that the client is going to do an update and break my code that launches this. I'm also concerned with them installing this on machines with different versions of windows (install paths are different in 64 bit environment vs. 32 bit).

My second problem is the fact that whenever I launch acrobat and print it still leaves the acrobat window open. I thought that the command line parameters I was using would suppress all of this but apparently not.

I'm trying to launch adobe acrobat from the command line with the following syntax:

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

It prints out fine but it still leaves the acrobat window up. Is there any other solution besides going out and killing the process programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

浮生面具三千个 2024-10-22 03:24:14

我最终放弃了 Adob​​e Acrobat,并使用 FoxIt Reader (免费 pdf 阅读器) ) 进行 pdf 打印。这是我用来通过 FoxIt 在 C# 中进行打印的代码:

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();

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

Foxit Reader.exe -t "pdf filename" "printer name"

更新:

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

有些打印机也支持本机 pdf 打印,因此可以将原始 pdf 数据发送到打印机并进行打印。请参阅https://support.microsoft.com/en-us/kb/322091 用于将原始数据发送到打印机。

更新 2

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

http://www.pdfprinting.net/

I ended up bailing on Adobe Acrobat here and going with FoxIt Reader (Free pdf reader) to do my pdf printing. This is the code I'm using to print via FoxIt in C#:

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();

The above code prints to the default printer but there are command line parameters you can use to specify file and printer. You can use the following syntax:

Foxit Reader.exe -t "pdf filename" "printer name"

Update:

Apparently earlier versions of acrobat do not have the problem outlined above either. If you use a much older version (4.x or something similar) it does not exhibit this problem.

Some printers do support native pdf printing as well so it's possible to send the raw pdf data to the printer and it might print it. See https://support.microsoft.com/en-us/kb/322091 for sending raw data to the printer.

Update 2

In later versions of our software we ended up using a paid product:

http://www.pdfprinting.net/

滴情不沾 2024-10-22 03:24:14

Nick 的答案对我来说看起来不错,所以我将其翻译成 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();
        }
    }
}

}

Nick's answer looked good to me, so I translated it to c#. It works!

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();
        }
    }
}

}

万劫不复 2024-10-22 03:24:14

我尝试过 Adob​​e Reader 和 Foxit,但没有成功。两者的当前版本都非常喜欢弹出窗口并让进程保持运行。最终使用了 Sumatra 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();
    }

I've tried both Adobe Reader and Foxit without luck. The current versions of both are very fond of popping up windows and leaving processes running. Ended up using Sumatra PDF which is very unobtrusive. Here's the code I use. Not a trace of any windows and process exits nicely when it's done printing.

    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();
    }
枉心 2024-10-22 03:24:14

以下内容在 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();
}

The following is tested in both Acrobat Reader 8.1.3 and Acrobat Pro 11.0.06, and the following functionality is confirmed:

  1. Locates the default Acrobat executable on the system
  2. Sends the file to the local printer
  3. Closes Acrobat, regardless of version
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();
}
不…忘初心 2024-10-22 03:24:14

问题 1

您也许可以通过注册表解决问题。在 HKEY_CLASSES_ROOT\.pdf\PersistentHandler\(Default) 中,您应该找到一个 CLSID,该 CLSID 指向在两个位置之一找到的值。同一密钥的 CLSID 文件夹,或者(对于 64 位系统)在 Wow6432Node\CLSID 中向下一步,然后在该 CLSID 的密钥中。

在该键中,您可以查找 LocalServer32 并找到指向当前 exe 路径的默认字符串值。

我并不是 100% 同意这一点,但似乎是有道理的(尽管您必须在多个环境中进行验证,以确认实际上找到了您正在寻找的进程)。

(以下是有关涉及的注册表项 关于 PersistentHandlers

问题 2

可能使用 CreateNoWindow<进程启动信息的 /a>。

Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
p.StartInfo.Arguments = "/t \"Label.pdf\" \"HP4000\" \"HP LaserJet 4100 Series PCL6\" \"out.pdf\"";
p.CreateNoWindow = true;
p.Start();
p.WaitForExit();

(不过只是猜测,但我确信一点测试将证明它有效/无效)

Problem 1

You may be able to work your way around the registry. In HKEY_CLASSES_ROOT\.pdf\PersistentHandler\(Default) you should find a CLSID that points to a value found in one of two places. Either the CLSID folder of the same key, or (for 64 bit systems) one step down in Wow6432Node\CLSID then in that CLSID's key.

Within that key you can look for LocalServer32 and find the default string value pointing to the current exe path.

I'm not 100% on any of this, but seems plausible (though you're going to have to verify on multiple environments to confirm that in-fact locates the process you're looking for).

(Here are the docs on registry keys involved regarding PersistentHandlers)

Problem 2

Probably using the CreateNoWindow of the Process StartInfo.

Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
p.StartInfo.Arguments = "/t \"Label.pdf\" \"HP4000\" \"HP LaserJet 4100 Series PCL6\" \"out.pdf\"";
p.CreateNoWindow = true;
p.Start();
p.WaitForExit();

(only a guess however, but I'm sure a little testing will prove it to work/not work)

ぺ禁宫浮华殁 2024-10-22 03:24:14

如果您使用 Acrobat reader 4.0,您可以执行以下操作:
“C:\Program Files\Adobe\Acrobat 4.0\Reader\Acrord32.exe”/t /s“U:\PDF_MS\SM003067K08.pdf”Planning_H2
但如果 PDF 文件是在较新版本的 Acrobat 中创建的,则会打开一个不可见的窗口

If you use Acrobat reader 4.0 you can do things like this:
"C:\Program Files\Adobe\Acrobat 4.0\Reader\Acrord32.exe" /t /s "U:\PDF_MS\SM003067K08.pdf" Planning_H2
BUT if the PDF file has been created in a newer version of Acrobat an invisible window opens

数理化全能战士 2024-10-22 03:24:14

得到了另一个解决方案..它是 stackOverflow 中其他片段的组合。当我调用 CloseMainWindow,然后调用 Kill .. adobe 关闭时

    Dim info As New ProcessStartInfo()
    info.Verb = "print"
    info.FileName = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("App Paths").OpenSubKey("AcroRd32.exe").GetValue(String.Empty).ToString()
    info.Arguments = String.Format("/p /h {0}", "c:\log\test.pdf")
    info.CreateNoWindow = True
    info.WindowStyle = ProcessWindowStyle.Hidden
    info.UseShellExecute = False

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

    Dim counter As Integer = 0
    Do Until p.HasExited
        System.Threading.Thread.Sleep(1000)
        counter += 1
        If counter = 5 Then
            Exit Do
        End If
    Loop
    If p.HasExited = False Then
        p.CloseMainWindow()
        p.Kill()
    End If

got another solution .. its combination of other snippets from stackOverflow. When I call CloseMainWindow, and then call Kill .. adobe closes down

    Dim info As New ProcessStartInfo()
    info.Verb = "print"
    info.FileName = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("App Paths").OpenSubKey("AcroRd32.exe").GetValue(String.Empty).ToString()
    info.Arguments = String.Format("/p /h {0}", "c:\log\test.pdf")
    info.CreateNoWindow = True
    info.WindowStyle = ProcessWindowStyle.Hidden
    info.UseShellExecute = False

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

    Dim counter As Integer = 0
    Do Until p.HasExited
        System.Threading.Thread.Sleep(1000)
        counter += 1
        If counter = 5 Then
            Exit Do
        End If
    Loop
    If p.HasExited = False Then
        p.CloseMainWindow()
        p.Kill()
    End If
疾风者 2024-10-22 03:24:14

对于问题 2

使用 /h 参数将在最小化窗口中打开 Acrobat 或 Adob​​e Reader。

例子:
C:\Program Files (x86)\Adobe\Reader 10.0\Reader>AcroRd32.exe **/h** /t "Label.pdf" "HP4000" "HP LaserJet 4100 Series PCL6" "out.pdf"

相关文档:https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/Acrobat_SDK_developer_faq.pdf#page=24

For Problem 2

Using /h param will open the Acrobat or Adobe Reader in minimized window.

Example:
C:\Program Files (x86)\Adobe\Reader 10.0\Reader>AcroRd32.exe **/h** /t "Label.pdf" "HP4000" "HP LaserJet 4100 Series PCL6" "out.pdf"

Related Documentation: https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/Acrobat_SDK_developer_faq.pdf#page=24

浪推晚风 2024-10-22 03:24:14

您可以使用免费 PDFium 库以静默方式打印任何 PDF,无需在系统上安装 Foxit 或 Adob​​e Acrobat。很多帖子中提到的任何其他非免费图书馆都是纯粹的骗局。只需使用 NuGET 将 PDFium 免费库添加到您的项目中,这里是您在静态方法中所需的所有代码:

public static void PrintFileWithPDFium(string file, string printer_name, short copy_number = 1)
{
    // Create the printer settings for our printer
    PrinterSettings printerSettings = new PrinterSettings
    {
        PrinterName = printer_name,
        Copies = copy_number,
    };
    // Now print the PDF document
    using (PdfiumViewer.PdfDocument document = PdfiumViewer.PdfDocument.Load(file))
    {
        using (PrintDocument printDocument = document.CreatePrintDocument())
        {
            printDocument.PrinterSettings = printerSettings;
            //printDocument.DefaultPageSettings = pageSettings;
            printDocument.PrintController = new StandardPrintController();
            printDocument.OriginAtMargins = false;
            printDocument.Print();
        }
    }
}

任何问题只需评论这篇文章,我很高兴为您提供帮助

You can silent print any PDF with the free library PDFium, with no need of having Foxit or Adobe Acrobat installed on your system, on a silent way. Any other not free library mentioned in a lot of posts is pure scam. Just add the PDFium free library to your project by using NuGET and here is all the code you need in an static method:

public static void PrintFileWithPDFium(string file, string printer_name, short copy_number = 1)
{
    // Create the printer settings for our printer
    PrinterSettings printerSettings = new PrinterSettings
    {
        PrinterName = printer_name,
        Copies = copy_number,
    };
    // Now print the PDF document
    using (PdfiumViewer.PdfDocument document = PdfiumViewer.PdfDocument.Load(file))
    {
        using (PrintDocument printDocument = document.CreatePrintDocument())
        {
            printDocument.PrinterSettings = printerSettings;
            //printDocument.DefaultPageSettings = pageSettings;
            printDocument.PrintController = new StandardPrintController();
            printDocument.OriginAtMargins = false;
            printDocument.Print();
        }
    }
}

Any issues just comment this post, I'm glad to help you with this

哆兒滾 2024-10-22 03:24:14

您已经尝试过与 Acrobat Reader 不同的东西,所以我的建议是忘记 GUI 应用程序并使用第 3 方命令行工具,例如 RawFilePrinter.exe

private static void ExecuteRawFilePrinter() {
    Process process = new Process();
    process.StartInfo.FileName = "c:\\Program Files (x86)\\RawFilePrinter\\RawFilePrinter.exe";
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.Arguments = string.Format("-p \"c:\\Users\\Me\\Desktop\\mypdffile.pdf\" \"Canon Printer\"");

    process.Start();
    process.WaitForExit();
    if (process.ExitCode == 0) {
            //ok
    } else {
            //error
    }
}

下载最新版本:https://bigdotsoftware.pl/rawfileprinter

You have already tried something different than Acrobat Reader, so my advice is forget about GUI apps and use 3rd party command line tool like RawFilePrinter.exe

private static void ExecuteRawFilePrinter() {
    Process process = new Process();
    process.StartInfo.FileName = "c:\\Program Files (x86)\\RawFilePrinter\\RawFilePrinter.exe";
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.Arguments = string.Format("-p \"c:\\Users\\Me\\Desktop\\mypdffile.pdf\" \"Canon Printer\"");

    process.Start();
    process.WaitForExit();
    if (process.ExitCode == 0) {
            //ok
    } else {
            //error
    }
}

Latest version to download: https://bigdotsoftware.pl/rawfileprinter

天邊彩虹 2024-10-22 03:24:14
 cmd.exe /C start acrord32 /l /p /h  D:\test.pdf

https://texwiki.texjp.org/?Adobe%20Acrobat%20Reader

 cmd.exe /C start acrord32 /l /p /h  D:\test.pdf

https://texwiki.texjp.org/?Adobe%20Acrobat%20Reader

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文