Print PDF files from a console application without additional tools

Lila1220

New member
Joined
Jan 29, 2016
Messages
3
Programming Experience
Beginner
Hello,
I would need some advice please. My situation is the following:
I have a zip-folder with PDF files in it. I go over all the PDFs in the zip folder and print each one.
Is this possible with the native Visual Studio and what it has to offer? Or do I essentially need a tool such as iTextPDF or PDFSharp?
I have been researching for so many hours and I cannot find a solution.
My main problem is, that I cannot find a way to connect my PDF-document I would like to print with the PrintDocument object. I cannot give a path to the document to the PrintDocument. It does not have an attribute either for passing on the path of where your document is located.
So how do I tell it which document to print?
So when I show my print preview, it is all blank. Is this because I cannot natively print PDF from a console application? Or is it possible somehow?
I would appreciate some help very much.
Thank you in advance! :)
Lila

Some excerpt of my C# code, which I am using within a console application:

PrintDialog pDialog = new PrintDialog();

PrintDocument printDoc = new PrintDocument();

printDoc.DocumentName = ("Print Document");


if (pDialog.ShowDialog() == DialogResult.OK)
{
printDoc.PrinterSettings = pDialog.PrinterSettings;
pDialog.Document = printDoc;
var settingsValid = printDoc.PrinterSettings.IsValid;
}
if (settingsValid)
{
PrintPreviewDialog p = new PrintPreviewDialog();
p.Document = printDoc;
try
{
var r = p.ShowDialog();
printDoc.Print();
}
 
There is no built-in functionality in .Net framework for pdf files. If there is a pdf document viewer application installed in system you can redirect printing to that by using Process/ProcessStartInfo and verb "print" to have that application print the document.
 
Thank you for your reply John!

For anybody interested in the code, I got my console application to work now, using Adobe Reader. A difficulty I had to overcome was the requirement to print to a specific printer, chosen by the user from the dialog box.

The following did the trick. It is printing silently too, closing Adobe Reader after 7 seconds:

PrintDialog pDialog = new PrintDialog();
if (pDialog.ShowDialog() == DialogResult.OK)
{
DirectoryInfo unzippedFolder = new DirectoryInfo(zibname);

foreach (FileInfo file in unzippedFolder.GetFiles())//print each PDF-file within the unzipped folder
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
Process process = new Process();
startInfo.Arguments = String.Format("/h /t "{0}" "{1}"", file.FullName, pDialog.PrinterSettings.PrinterName);//file.FullName: full path of PDF file startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = false;
process = Process.Start(startInfo);
if (!process.WaitForExit(7000))
{
// kill Adobe Reader
process.Kill();
}
}

Best regards
Lila
 
A difficulty I had to overcome was the requirement to print to a specific printer
That can also be done with Verb "printto" and quoted printer name in Arguments.
 
Thanks John,

I tried using "printTo", but could not get it to work without specifying
startInfo.FileName =
@"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";

I was hoping there was some way to avoid having to specify the path to the Adobe Reader .exe, as this means that whenever the user would upgrade their Adobe Reader version, my program would stop working as the path would be incorrect.

Is there any way around this?
 
Filename is just the path to the pdf document.
 

Latest posts

Back
Top Bottom