Printing a PDF Silently with Adobe Acrobat - c#

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?

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/

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

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

The following is tested in both Acrobat Reader 8.1.3 and Acrobat Pro 11.0.06, and the following functionality is confirmed:
Locates the default Acrobat executable on the system
Sends the file to the local printer
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();
}

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

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)

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

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

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

Related

Calling WSL bash.exe from C#

Mostly just as a curiosity, I wrote a little app to start up Terminator shell on Windows, using Ubuntu/WSL and Xming window server.
Doing things manually from the shell, I can run Firefox, gedit, Terminator, etc on Windows, it's pretty cool.
So I checked the location of bash.exe using where bash and it returned...
C:\Windows\System32\bash.exe
However when I tried to run this code...
using (var xminProc = new Process())
{
xminProc.StartInfo.FileName = #"C:\Program Files (x86)\Xming\Xming.exe";
xminProc.StartInfo.Arguments = ":0 -clipboard -multiwindow";
xminProc.StartInfo.CreateNoWindow = true;
xminProc.Start();
}
using (var bashProc = new Process())
{
bashProc.StartInfo.FileName = #"C:\Windows\System32\bash.exe";
bashProc.StartInfo.Arguments = "-c \"export DISPLAY=:0; terminator; \"";
bashProc.StartInfo.CreateNoWindow = true;
bashProc.Start();
}
I get the error...
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
And checking my entire system for bash.exe reveals it really be in another place altogether...
I'm not sure if this location is one that I can rely on, I'm worried it's ephemeral and can change during a Windows Store update, although I may be wrong about that.
Why does the command prompt show bash.exe to be in System32 but it's really in another location altogether?
Can I get C# to also use the System32 location?
As #Biswapriyo stated first set the platafrom to x64 on your solution:
Then you may run on your ubuntu machine from c# as:
Console.WriteLine("Enter command to execute on your Ubuntu GNU/Linux");
var commandToExecute = Console.ReadLine();
// if command is null use 'ifconfig' for demo purposes
if (string.IsNullOrWhiteSpace(commandToExecute))
{
commandToExecute = "ifconfig";
}
// Execute wsl command:
using (var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
}
})
{
proc.Start();
proc.StandardInput.WriteLine("wsl " + commandToExecute);
System.Threading.Thread.Sleep(500); // give some time for command to execute
proc.StandardInput.Flush();
proc.StandardInput.Close();
proc.WaitForExit(5000); // wait up to 5 seconds for command to execute
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.ReadLine();
}

C# process start impersonation error

I am trying to run the process with different user. When I run normal "notepad.exe" it works fine. But when I change the file to any other executable with full path(C:\\Program Files\\Microsoft Office\\Office15\\Excel.exe) or (C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe) it doesn't work. Instead gives errors like attached in pic.
Any suggestions...??
static void Main(string[] args)
{
SecureString securePwd = new SecureString();
string password = "P#ssw0rd";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
Process p = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\welcome.pdf";
p.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\";
p.StartInfo.ErrorDialog = true;
p.StartInfo.EnvironmentVariables.Add("TempPath", "C:\\Temp");
p.StartInfo.Domain = "testdom";
p.StartInfo.UserName = "testuser";
p.StartInfo.Password = sec_pass;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StreamReader myStreamReader = p.StandardOutput;
// Read the standard error of net.exe and write it on to console.
Console.WriteLine(myStreamReader.ReadLine());
p.Close();
}
Notepad doesn't store any user-specific settings. I'm certain all of the Office products do, and it wouldn't surprise me if Acrobat does too.
So, first thing to fix is to make sure that your ProcessStartInfo sets LoadUserProfile to true. That may be sufficient.
However, sometimes applications also act quite differently when run for the first time versus any subsequent launches, so I'd also make sure that you have, at least once, launched each of these applications as the intended target user, whilst you're actually logged onto the machine as that user (versus just launching a single process as that user).
In your code example your trying to open a pdf document in notepad.
Just checking, what happens when you change the file name to the adobe exe (you may need to add the path to the exe) instead of notepad.exe

Multiple process commands fail to get back any response

Having a challenge being able to send commands to cmd.exe from via the C# Process class.
Basically I want to call R.exe and then send it several commands to stage the data before I run some R functions and then pull out the result.
But I can't get the result back from a simple 'dir' statemenet :(
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
string pathToR = #"C:\Program Files\R\R-3.3.1\bin";
p.StartInfo = info;
p.Start();
List<string> output = new List<string>();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("cd " + pathToR);
sw.WriteLine("dir");
while (p.StandardOutput.Peek() > -1)
{
var peekVal = p.StandardOutput.Peek();
output.Add(p.StandardOutput.ReadLine());
}
}
}
foreach (var line in output)
{
Console.WriteLine(line);
}
p.StandardInput.Close();
p.StandardOutput.Close();
p.WaitForExit();
p.Close();
Console.ReadLine();
Output:
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
c:\users\micah_000\documents\visual studio 2015\Projects\RStagingTestApp\RStagingTestApp\bin\Debug>cd C:\Program Files\R\R-3.3.1\bin
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
I've seen several variations on this result, but I've never seen any kind of response from my commands :(
A lot of places say the async reads work best. I've seen some persuasive, comprehensive explanations for this, but I haven't been able to get it to work for me.
This one seemed to work. It might have been setting AutoFlush to true. Or just reading once at the end.

Print multiple copies of pdf

I am currently using the below code for printing a pdf using Foxit Reader software. Now my problem is I want to print multiple copies of a file. Could anyone let me know how to specify the number of copies while printing a pdf in the below code.
[Edit]
I dont want to use a loop to print multiple copies of pdf. I want to specify it only as a command line argument.
Any help much appreciated :)
Process process = new System.Diagnostics.Process();
process.EnableRaisingEvents = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = foxitReaderInstalledPath;
string arguments = String.Format(#"-t ""{0}"" ""{1}""", this.Path, printerName);
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
According to the Foxit manual there is no option to do what you want except with a loop (which you don't want to use).
Either you use some PDF library for .NET -there are plenty free and commercial ones out there (see for example .NET library to print PDF files )- or you use for example Acrobat reader for printing (IIRC it has a commandline switch to achieve what you want)...
Just put that in a loop. You can always manipulate the termination of the process later.
It'd be nice to put it in the Arguments, but I don't think FoxIt supports it that I know of.
int numberOfCopies = 2;
Process process = new System.Diagnostics.Process();
for (int i = 1; i <= numberOfCopies; i++)
{
process.EnableRaisingEvents = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = foxitReaderInstalledPath;
string arguments = String.Format(#"-t ""{0}"" ""{1}""", this.Path, printerName);
process.StartInfo.Arguments = arguments;
process.Start();
}

Is there anyway to specify a PrintTo printer when spawning a process?

What I Have
I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified addresses.
The file can either be of the formats: Excel, Excel Report, Word, or PDF.
What I am currently doing is spawning a process with the path of the file and then starting the process; however I also am trying to fix a bug feature that I added which adds the verb 'PrintTo' to the startup information, depending on a specified setting.
What I Need
The task I am trying to accomplish is that I would like to have the document open and then print itself to a specified printer named within the program itself. Following that up, the file should then close itself automatically.
If there is no way to do this generically, we might be able to come up with a way to do it for each separate file type.
What you Need
Here is the code I'm using:
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);
}
How I'm doing...
Still stumped... might call it like Microsoft does,'That was by design'.
The following works for me (tested with *.doc and *.docx files)
the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)
just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...
// 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);
}
}
Theoretically, according to an article on MSDN you should be able to change it to be along the lines of (untested):
// 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";
}
get from Rowland Shaw:
ProcessStartInfo startInfo = new ProcessStartInfo(Url)
{
Verb = "PrintTo",
FileName = FilePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
Arguments = "\"" + PrinterName+ "\"",
};
Process.Start(startInfo);
FilePath look like 'D:\EECSystem\AttachedFilesUS\53976793.pdf'
PrinterName is your printer name
copy the code,it will work.

Categories