CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2016
    Posts
    2

    Printing PDf files from a console application without any additional tools

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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Printing PDf files from a console application without any additional tools

    Quote Originally Posted by Lila1220 View Post
    Or do I essentially need a tool such as iTextPDF or PDFSharp?
    Yes, to do it, you are going to need something that takes the pdf and forms a document that you can print.

  3. #3
    Join Date
    Jan 2016
    Posts
    2

    Re: Printing PDf files from a console application without any additional tools

    Thank you 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

  4. #4
    Join Date
    Apr 2014
    Posts
    23

    Re: Printing PDf files from a console application without any additional tools

    Print PDF in C# without using additional tools:

    Code:
    //namespaces
    using System.Drawing.Printing;
    using System.Diagnostics;
    using System.Collections.Specialized;
    
    // print method
     private void pdfPrint(string filePath)
        {
            PrintDocument pd = new PrintDocument();
            Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = true;
            startInfo.FileName = filePath;
            startInfo.Verb = "print";
            startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";
            p.StartInfo = startInfo;
            p.Start();
            p.WaitForExit();
    
          
        }
        protected void btn_print_Click(object sender, EventArgs e)
        {
    
            string filePath="C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\1.pdf";
            pdfPrint(filePath);
        }
    Alternatively, if you want more options while printing, such as choosing default printer or any other network connected printer, printing all the PDF pages or only print the selected pages, you'd better make use of a 3rd party tool: How to print PDF in C#

  5. #5
    Join Date
    Jul 2019
    Posts
    2

    Re: Printing PDf files from a console application without any additional tools

    Try ZetPDF. ZetPDF is a .net SDK for adding PDF render and print support in .net applications. It is designed to solve most developer’s needs with regards to PDF rendering.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured