print existing pdf file - c#

I have made a Windows Form application and I want to print an existing PDF document with my default printer.
I have the file, stored in c:\users\marten\document.pdf.
I have searched a long time for some examples, but the only examples I found was printing a text file or printing a string into a document.
Can someone give me a good example or tutorial?

This uses the installed pdf reader to print the file against the default printer on the machine.
string path = "" <- your path here.
if (path.EndsWith(".pdf"))
{
if (File.Exists(path))
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
}

Related

Run a C# exe with parameters that is to start another application and get output from console

I have an exe which has some parameters- path of another application and some files to be opened from that application. There would be an output as part of that application which would be displayed in the console of my exe.
But i am unable to get the output from the console.
I have the code:
ProcessStartInfo psi = new ProcessStartInfo("\"" + dllpath + "\\newapplication.exe" + "\"");
Process p = new Process();
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.Start();
The process starts successfully, and then i have to open a file in the process which happens through another class. So after the file opened, some extraction happens and the result is displayed on the console.
When i give p.WaitForExit(); nothing happens other than starting the application! How do i acheive to retreive the output on StandardOutput as per my code? Need Help!
This is the correct way to do it:
string outputProcess = "";
string errorProcess = "";
using (Process process = new Process())
{
process.StartInfo.FileName = path;
process.StartInfo.Arguments = arguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
outputProcess = process.StandardOutput.ReadToEnd();
errorProcess = process.StandardError.ReadToEnd();
process.WaitForExit();
}
Remember to use the using statement when you have an IDisposable object

Is there any way to print PDF from window service C# or (Server side printing)

I need to print pdf from web application to server connected printer. After a long search currently i am using windows form application to print pdf from the folder on server, window form application stopped when system restarts or log off, so i am trying to create window service,enter code here but the same code is not working on windows service. Is there any way to fulfil my requirement.
string[] filePaths = Directory.GetFiles(filePath);
foreach (string file in filePaths)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = file;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(10000);
if (false == p.CloseMainWindow())
p.Kill();
//System.Threading.Thread.Sleep(5000);
File.Delete(file);
}

Printing a Text file programmatically

I'm tring to make a program that saves a textbox text to a text file and prints the text file.
I found this code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#"TempDocument.txt");
psi.Verb = "PRINT";
Process.Start(psi);
Here
But it doesn't open a dialog it's just printing.
I want to have a dialog in order to choose another printer or open in OneNote.
To show a printDialog, you can try :
However, I don't know which kind of project your talking about, so maybe this will not fit.
printDialog = new PrintDialog();
//when you click on OK
if (printDialog.ShowDialog() == DialogResult.OK)
{
//path is your documents to print location
ProcessStartInfo info = new ProcessStartInfo(path);
info.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
System.Diagnostics.Process.Start(info);
}

Open a specific page in a PDF file c#

I open a pdf file when my form is loaded with the following code:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
This works fine but now I want to open a specific page. For example page number 5 of the document test.pdf? Does any one have an idea? Tried some stuff but dind't work!
Thanks!
Try
process.StartInfo.Arguments = "/A \"page=n\" \"F:\\STAGE\\test.pdf"";
changing n to the page number you want
Checkout this : http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf
It explain what arguments Adobe Reader can receive.
And it has a Page argument.
Your code must be :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=N\"";
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
Where N is your page number.
call it like what was suggested here: Adobe Reader Command Line Reference
So it would be:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "EXE_PATH\\AcroRd32.exe";
startInfo.Arguments = "/A \"page=PAGE_NUM\" \"FILE_PATH\"";
Process.Start(startInfo);
you can try this code.
Process myProcess = new Process();
myProcess.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
myProcess.StartInfo.Arguments = "/A \"page={pagenum}\" \"c:\\Classic\\Manual\\DocumentationManual.pdf\"";
myProcess.Start();
please change the path of AcroRd32.exe as per your directory.
Thanks
Try this.
Note: you must have acrobat reader installed in your pc before you can use axAcroPDF .
int n = 5; //page number
string filePath = "F:\STAGE\test.pdf";
axAcroPDF1.LoadFile(filePath);
axAcroPDF1.setCurrentPage(n);

How to print a PDF to something other than the default printer

I used this code to print a pdf file from acrobat reader.
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
However, it prints through default printer.
How can I select the printer to send it to? default.
I have tried with a property for ex: info.Arguments, but that doesn't work.
Use the /t command line argument to force adobe to use a specific printer:
AcroRd32.exe /t path "printername" "drivername" "portname"
See the PDF developer FAQ for more info:
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/intro_to_sdk/DeveloperFAQ.pdf
How about using the "printto" verb? pasing "\\\server\printer" for info.Arguments

Categories

Resources