Printing a PDF automatically [closed]
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionI have a webservice that has a crystal report that generates an invoice from my database and then exports the crystal to a PDF
, into a stream and then returns as a string on the web method.
On the client side I have a Winforms C#
app that calls this webservice. I would like to be able to automatically print the PDF
on the client side without opening Acrobat Reader.
I would preferably not want to save the PDF
either because currently I have to save the开发者_如何转开发 PDF
then use a process to open Acrobat Reader and then print the PDF
.
There is a free utility called SumatraPDF that lets you print PDF documents silently. I have succesfully used it in a winform app.
private void print(string printerName, string fileName)
{
try
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "SumatraPDF.exe";
proc.StartInfo.Arguments = "-print-to " + '"' + printerName+ '"' + " " + '"' + fileName+ '"';
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = true;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
EventLog.WriteEntry("InboundServicioImpresion", ex.Message + " " + ex.StackTrace);
}
}
For the "automatic print", you could use a Timer or a infinite loop that executes the method above.
I had a similar challenge. The solution I've made was buying a component called PDFTron. It has an API to send pdf documents to a printer from an unattended service. I posted some information in my blog about that. Take a look!
How to print a PDF file programmatically???
Thanks,
Roberto Lopes
You could use the command line:
acrord32 /t "out.pdf" "Acrobat Distiller" "AdobePS Acrobat Distiller" "in.pdf"
The syntax is:
acroRd32.exe /t path printername drivername portname
- Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader.
精彩评论