5

I want to print out a document using C#. I have two buttons. btnUpload uploads or selects a word file. btnPrinthave to send uploaded file to a printer. How can I do this? Now using:

private void btnUpload_Click(object sender, EventArgs e)
{
    string fileName;
    // Show the dialog and get result.
    OpenFileDialog ofd = new OpenFileDialog();
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK) // Test result.
    {
        fileName = ofd.FileName;

        var application = new Microsoft.Office.Interop.Word.Application();
        //var document = application.Documents.Open(@"D:\ICT.docx");
        var document = application.Documents.Open(@fileName);
    }
}


private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDialog printDlg = new PrintDialog();
    PrintDocument printDoc = new PrintDocument();
    printDoc.DocumentName = "fileName";
    printDlg.Document = printDoc;
    printDlg.AllowSelection = true;
    printDlg.AllowSomePages = true;
    //Call ShowDialog
    if (printDlg.ShowDialog() == DialogResult.OK)
        printDoc.Print();
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
D M L K Sandanuwan
  • 159
  • 2
  • 5
  • 13
  • 2
    possible duplicate of [How can I send a file document to the printer and have it print?](http://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print) – p.s.w.g Dec 19 '13 at 22:13
  • 1
    Please share your research so far. – Ant P Dec 19 '13 at 22:13
  • Also check [Printing Word Documents in C#](http://www.codeproject.com/Articles/10999/Printing-Word-Documents-in-C) – Theraot Dec 19 '13 at 22:22
  • To select the file check [OpenFileDialog class](http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx) – BrunoLM Dec 19 '13 at 22:25
  • @user3027412: you need to handle `PrintPage` Event , check my answer below. – Sudhakar Tillapudi Dec 20 '13 at 06:15

1 Answers1

9

you need to hadle PrintPage event

Try This:

   String content="";
   private void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName;
        // Show the dialog and get result.
        OpenFileDialog ofd = new OpenFileDialog();
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            fileName = ofd.FileName;

            var application = new Microsoft.Office.Interop.Word.Application();
            //var document = application.Documents.Open(@"D:\ICT.docx");
             //read all text into content
            content=System.IO.File.ReadAllText(fileName);
            //var document = application.Documents.Open(@fileName);
        }
    }
 private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDialog printDlg = new PrintDialog();
        PrintDocument printDoc = new PrintDocument();
        printDoc.DocumentName = "fileName";
        printDlg.Document = printDoc;
        printDlg.AllowSelection = true;
        printDlg.AllowSomePages = true;
        //Call ShowDialog
        if (printDlg.ShowDialog() == DialogResult.OK)
        {
             printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);            
             printDoc.Print(); 
        }
    }
 private void pd_PrintPage(object sender, PrintPageEventArgs ev)
 {
   ev.Graphics.DrawString(content,printFont , Brushes.Black,
                   ev.MarginBounds.Left, 0, new StringFormat());
 }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • If the file I want to print is a PDF and has graphics like tables, formatted text, an image, how can that be drawn on the print page? I have tried searching for this a lot on google, no solution. – D J Oct 01 '21 at 14:55