C# Print Word Document without opening Word or Printerselection - c#

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.

Related

Print PDF hidden from user

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.

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

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.

How can I get filename when user close Word?

I have one button and one textbox on form. When I click on the button, Microsoft Word is started. When the user closes Word, I want to get the file name that the user saved his work under to show up in the text box.
I am using the following code for button_click:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#"C:\Program Files\Microsoft Office\Office12\winword.exe");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit();
if (listFiles.HasExited)
{
System.IO.FileInfo ff = new System.IO.FileInfo(myOutput.ToString());
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
I don't believe this is possible via the command line. Also, realize that, if the user opens another document, your code will not finish, since it'll wait until all of the docs are closed (the process won't end until that point).
That being said, you should be able to use the Word/Office Interop libraries, and listen to the DocumentBeforeSave and/or DocumentBeforeClose events. This should let you pull the information from the Document itself, right as it's being saved/closed.
I don't think you can do this with the current code you have. ProcessStartInfo is very generic and has no knowledge of word.
I think you will want to use Visual Studio Tools for Office (VSTO) to start word. This way you have a reference to the word application and document and can get the document name.
this article should get you started http://www.c-sharpcorner.com/UploadFile/mgold/WordFromDotNet11082005235506PM/WordFromDotNet.aspx the sample code has a object called aDoc. Then after the save you can check aDoc.FullName to get the path.
I'm not sure whether it's possible or not because name of the saved file is an internal information that's not public for other applications.
Instead in your particular case since you are using MS Word you can use Word interop to start the Word application in your application and then handle its events
Create a blank Word document in the desired location, and then get Word to load that?

Categories