5 Replies - 6232 Views - Last Post: 05 February 2016 - 06:26 AM Rate Topic: -----

#1 Lila1220   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 16

Print/Printing PDF files from a console application without any tools

Posted 29 January 2016 - 10:39 AM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Print/Printing PDF files from a console application without any tools

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Print/Printing PDF files from a console application without any tools

Posted 29 January 2016 - 10:46 AM

Might be possible using P/Invoke:

https://social.msdn....m=csharpgeneral

But you're probably better off using ITextSharp to load the document and print it that way. I'm not sure if the above method requires your printer to support direct PDF printing or not.

Another option is, if Acrobat Reader is installed, to "shell out" and start that process with command line options that support printing, but that's not "without any tools".
Was This Post Helpful? 0
  • +
  • -

#3 Lila1220   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 16

Re: Print/Printing PDF files from a console application without any tools

Posted 04 February 2016 - 02:41 PM

Thank you Curtis for your reply! :-)

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
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: Print/Printing PDF files from a console application without any tools

Posted 04 February 2016 - 04:03 PM

I don't understand why you are killing Reader after 7 seconds. What is the significance of 7 seconds? Why can't you have multiple Reader running/printing?
Was This Post Helpful? 0
  • +
  • -

#5 Lila1220   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 16

Re: Print/Printing PDF files from a console application without any tools

Posted 04 February 2016 - 11:02 PM

I would like to avoid for the user to manually have to close the Adobe Reader.
I have tried to put the process.kill() after the foreach loop, to print all documents without having to open the reader again and again, but then it only works
for the first document and after that, Adobe Reader is giving me the message 'document not found'...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: Print/Printing PDF files from a console application without any tools

Posted 05 February 2016 - 06:26 AM

Interesting. Something you may want to put as comments in your code when you check it into your source control so that people maintaining it months or years from now would know why that logic is there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1