C# – Print PDF file and Doc file using C#

cdocpdfprinting

In my application I'm trying to create a function to print existing PDFs or Doc. How can I do this in C# and provide a mechanism so the user can select a different printer or other properties.

I've looked at the PrintDialog but not sure what file it is attempting to print, if any, b/c the output is always a blank page. Maybe I'm just missing something there.

Any advice, examples or sample code would be great!

The below is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
         public Form1()
         {
              InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
              string printPath = 
                   System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
              System.IO.StreamReader fileToPrint;
              fileToPrint= new System.IO.StreamReader(printPath + @"\myFile.txt");
              System.Drawing.Font printFont;
              printPDF(e);
              printDocument1.Print();
              fileToPrint.Close();
         }

         private void button2_Click(object sender, EventArgs e)
         {
              //printDoc(e);
         }

         public void printPDF(object sender ,  
                              System.Drawing.Printing.PrintPageEventArgs e))
         {       
              printFont = new System.Drawing.Font("Arial", 10);
              float yPos = 0f;
              int count = 0;
              float leftMargin = e.MarginBounds.Left;
              float topMargin = e.MarginBounds.Top;
              string line = null;
              float linesPerPage = e.MarginBounds.Height /     
                                   printFont.GetHeight(e.Graphics);
              while (count < linesPerPage)
              {
                    line = fileToPrint.ReadLine();
                    if (line == null)
                    {
                         break;
                    }
              yPos = topMargin + count * printFont.GetHeight(e.Graphics);
              e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos,
                                  new StringFormat());
              count++;
              }

              if (line != null)
              {
                   e.HasMorePages = true;
              }

              fileToPrint.Close();        
         }

         public void printDoc()
         {
         }
     }
 }

Best Answer

This has worked in the past:

using System.Diagnostics.Process;

...

Process process = new Process();

process.StartInfo.FileName = pathToPdfOrDocFile; 
process.UseShellExecute = true;
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + printerName + "\""; 
process.Start();

process.WaitForInputIdle();
process.Kill();

To print to the default printer, replace printto with print, and leave off the Arguments line.