Print PDF hidden from user - c#

I have a PDF printed from C# with this code:
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "printto";
info.FileName = segnToPrint;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = "\""+ stmp+ "\"";
Process p = new Process();
p.StartInfo = info;
p.Start();
p.EnableRaisingEvents = true;
p.WaitForInputIdle();
System.Threading.Thread.Sleep(1000);
// Close Acrobat regardless of version
if (p != null)
{
p.WaitForInputIdle();
p.CloseMainWindow();
}
stmp is the address of the printer. The print works fine and it's perfect but i see the windows of Acrobat Reader any time that i call this function and the page of Acrobat Reader remains open after the last file printed.
How can i hidden all the process to the user?

If you are looking to hide some window then you may try move its window left and top position outside the screen using SetWindowPos function (see C# code here).
But please be aware of user interactions as user could be confused by the program running in the taskbar but not available on desktop.

Unfortunately (as you have found) acrobat reader will always open a window. If you wish to silently print without ever seeing acrobat then the only approach would be to use something other than acrobat reader. Two possible options are to use another PDF reader like FoxIt or to try to send your PDF directly to the printer in raw form, getting round the need for 3rd party applications all-together.

Related

C# Print Word Document without opening Word or Printerselection

I tried using the code below, using PrintDocument, etc.. but can't get it to work.
Every time I run the Code below it opens a window asking me to select a printer. Using PrintDocument always led to empty pages, but the docs got printed.
How can I print Word documents without opening any windows?
foreach (string doc in dirFiles)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = doc;
info.Arguments = SelectedDrucker; //Printername
info.CreateNoWindow = true;
Process.Start(info);
}
Only the Word application can correctly print a Word document as it interprets the content/layout as it's sent to the printer. You first need to open the document in the Word application, then use its PrintOut method.
(This is also what Windows does when a user right-clicks a Word file and selects "Print".)
So there's no way to print a document without opening a window. You can, however, minimize the Word document window once it's been opened.

Always print to default printer even the printervname is specified

I created one windows application in which I need to print PDF files silently.
string printername="jn-01";
if (printername != "NULL")
{
using (PrintDialog pd = new PrintDialog())
{
pd.PrinterSettings.PrinterName = printername;
MessageBox.Show(printername);
pd.PrinterSettings.Copies = 1;
if (pd.PrinterSettings.IsValid)
{
ProcessStartInfo info = new ProcessStartInfo(e.FullPath);
info.Verb = "PrintTo";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
}
}
I use above code for printing. But the system always print to default ptinter.
How can I solve this issue?
You are setting the printer name on a print dialog, but you aren't using the print dialog to print. Notice how you never use pd when printing. You are instead executing the PDF file (effectively using ShellExecute) with PrintTo verb. Now PrintTo verb supports providing the printer name as a command line argument. You can set it on info.Arguments. However for this to work, the default application that handles the PDF files needs to support it. See this page for more info on these verbs.
If the default PDF application has no support for this, then your alternative could be to call SetDefaultPrinter before printing and then restore it to its previous value after printing (Use GetDefaultPrinter to find which one is default first). Note that this changes the default printer for the user, so should not be the default choice when doing this.

How to print various file types programmatically

I am writing an app which performs some tests and produces a number of different reports. These may be any combination of Labels, PDF for end customer, PDF for repair department, XML file etc.
Depending on the report type, I need to send the file to either the file system or to one of a number of different printers (A4, label etc). Ideally there should be no popup windows - just straight to paper.
How can I send a file (PDF, XML) to a printer? I had thought that for XML/Text I could just File.Copy to LPTn but that doesn't seem to work. For PDF I'm guessing that I could call Acrobat with some parameters which cause the PDF to be printed.
The printers I use are mapped to LPTn. Is there a better way to do this and to store the definitions in the app? i.e. Labels go to MyLabelPrinter and A4 PDFs got to MyA4Printer.
Has anyone done this?
ProcessStartInfo info = new ProcessStartInfo("[path to your file]");
info.Verb = "PrintTo";
info.Arguments = "\"[printer name]\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Take a look at this webpage. You should find the info you are looking at for PDF.
For example, it will look like that:
ProcessStartInfo infoOnProcess = new ProcessStartInfo("C:/example.pdf");
info.Verb = "PrintTo";
//Put a if there, if you want to change printer depending to file extension
info.Arguments = "\"HP-example-Printer\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(infoOnProcess);

Is there a way to have a batch file run within a windows form application in C#?

So anyways, I've been working on a batch IDE, and I was wondering if there was a good way to effectively embed the file into the form.
It would function sort of like a debug mode, where at any time, the user can click a button, and the batch file would load into the actual form.
Like the black cmd window would be embedded into the form...
Is there any way to do that?
ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.FileName = "C:\\echo.cmd";
var p = Process.Start(psi);
Console.WriteLine(p.StandardOutput.ReadToEnd());
And in C:\echo.cmd I have just basic echo hello!. When this code is executed - you'll see hello! received from batch's output stream.
Note that if executed command will wait for some input - ReadToEnd() can't return. In this case you should use Process.OutputDataReceived event.
Look at the process object and the StandardInput, StandardOutput and StandardError streams.
That is essentially all the command window is showing with some special handling of control characters.

PDF to XPS Converting via Microsoft XPS Document Writer

Printing pdf document with Microsoft XPS Document Writer:
string filename = "C:\\1.pdf";
Process process = new Process();
process.StartInfo.Verb = "PrintTo";
process.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 9.0\Reader\acrord32.exe";
process.StartInfo.Arguments =
"/t \"C:\\1.pdf\" \"Microsoft XPS Document Writer\" \"xps\" XPSPort:";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardOutput.ReadToEnd();
process.WaitForExit();
The only problem is Save Dialog, which requests file name (*.xps) where to save result. Everbody advices DOCINFO to solve this problem, but I didn't find any example of using.
I need programatically print PDF File via Microsoft XPS Document Writer with default output file name. How should I use DOCINFO in this situation?
Can you help me?
You can't reliably print by spawning Acrobat Reader unless you give it a desktop session and there will be a user there, because it sometimes pops up dialogs that need user attention.
Also it violates Adobe's licence if used unattended.
You can, however print using Ghostscript.
There is a C# interface to Ghostscript called Ghostscript.Net that I've used successfully on some very large projects. Both Ghostscript and Ghostcript.Net are free & open source.

Categories