从C#启动Acrobat Reader 10.0:如何最小化?

5

我正在Win 7系统上启动Reader 10.0,从C#程序向打印机发送PDF文件。这是我现在正在做的:

startInfo.FileName = adobeReaderPath;
string args = String.Format("/t \"{0}\" \"{1}\"", this.pdfFileName, this.printerName);
startInfo.Arguments = args;
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process process = Process.Start(startInfo);

我注意到以这种方式启动Reader(或者从命令提示符中)实际上会启动2个AcroRd32.exe可执行文件。它们都没有最小化。我也尝试了使用ProcessWindowStyle.Hidden,结果相同。
有没有一种方法强制Reader启动时最小化?
谢谢!

你能否发布一个更完整的示例(例如如何调用此函数以及从哪里调用)?因为我最近在Win 7上做了同样的事情,只运行了1个实例。 - Alex
5个回答

2
尝试在命令行中包括/h,以此来运行Adobe Reader实例并最小化到任务栏。然而,据我所知,并没有“好的”选项可以将其完全隐藏。除非使用一些不可预测的方法,比如通过Win32 API进行某些黑客操作。启动某个应用程序以最小化的更通用的方法是通过API,详见Steve的帖子。
这应该就可以了。
string args = String.Format("/h /t \"{0}\" \"{1}\"", this.pdfFileName, this.printerName);

2

启动进程后,您可以获取其MainWindowHandle并使用P/Invoke将其最小化:

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

//..
ShowWindow(process.MainWindowHandle, 11);  //11 is ForceMinimized

1
为什么要使用Win32 API,而不是使用Adobe内置的函数来最小化启动它呢? :) - Alex
@Alex,如果他想要这个更通用化,Win32 API是正确的选择。当然,这个参数对于Adobe来说是有效的,但是如果他需要它能够与FoxIt或其他应用程序一起使用呢? - Steve Danner
@Steve,我试了一下,但这似乎对阅读器窗口没有影响,即在调用ShowWindow后,窗口仍未最小化。 - I Z
你可能想在pInvoke链接中尝试其他nCmdShow参数。你可以尝试2(ShowMinimized)或0(Hide)。 - Steve Danner
很遗憾,结果一样。:( 这可能与Acrobat在启动进程时会以某种方式启动两个AcroRd32进程有关。 - I Z
这很奇怪。听起来像是Web浏览器处理多个选项卡的方式。也许尝试迭代所有子进程,并对它们调用相同的win32方法。 - Steve Danner

0

看这个:http://www.robvanderwoude.com/commandlineswitches.php#Acrobat

Open a PDF file:

AcroRd32.exe PdfFile

Open a PDF file in a new instance of Adobe Reader:

AcroRd32.exe /N PdfFile

Open a PDF file at page 7:

AcroRd32.exe /A "page=7=OpenActions" PdfFile

Open a PDF file with navigation pane active, zoom out to 50%, and search for and highlight the word "batch":

AcroRd32.exe /A "zoom=50&navpanes=1=OpenActions&search=batch" PdfFile

Print a PDF file with dialog:

AcroRd32.exe /P PdfFile

Print a PDF file silently:

AcroRd32.exe /N /T PdfFile PrinterName [ PrinterDriver [ PrinterPort ] ]

The last command will open a new Adobe Reader window, print the PDF file and then terminate its window unless that window happens to be the only Adobe Reader window left: at least one Adobe Reader window will be left open.

编辑: http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf#page=5

翻译:

0
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";

proc.StartInfo.FileName = @"Path of Adobe exe";
proc.StartInfo.Arguments = String.Format(@"/p /h {0}", fileToPrint);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();

0

使用Adobe Acrobat Reader,您实际上无法直接将PDF文档发送到打印机,使用您提到的代码也不行。

您需要一个具有打印PDF功能的.NET PDF API。您可以将其引用添加到项目中,然后开始使用该API。您可以在互联网上搜索此类API,这些API也是免费且易于使用的。


我正在成功地进行。唯一的问题是令人讨厌的阅读器窗口,我正在尝试将其最小化。 - I Z
好的, 由于AcroRd32.exe在打印完成后仍然在运行,因此您可以使用Process类通过名称获取正在运行的进程AcroRd32.exe,使用Process.GetProcessesByName("AcroRd32.exe")并通过调用Process类实例的Kill方法来终止它们。请记住,Process.GetProcessesByName方法会给您返回一个Process实例数组,因此您需要循环遍历并通过调用Kill方法来终止该进程。 - Uday0119
我已经在做这个了。我只是试图避免在启动过程时弹出窗口。 - I Z
好的,但我建议您使用PDF .net API,因为它们比使用Adobe Reader更方便。有些API甚至不需要您在计算机上安装Adobe Reader,而且它们也很快... - Uday0119
UDAY,你有没有想到过某个特定的API,它的源代码也是可用的? - I Z
嗯,我在使用 .net 的 PDF 库方面没有太多实际经验,但一些流行的库包括 iText、PDFLib 等... 你可以在这里获取列表(http://csharp-source.net/open-source/pdf-libraries)。 - Uday0119

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接