Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a document using C# code?

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();
}
like image 202
D M L K Sandanuwan Avatar asked Dec 19 '13 22:12

D M L K Sandanuwan


People also ask

Does C have a print function?

Print Function in C, C++, and Python Print function is used to display content on the screen. Approach: Some characters are stored in integer value inside printf function. Printing the value as well as the count of the characters.

Why printf is used in C instead of print?

Originally Answered: Why is it called printf rather print? The commonly accepted answer is that the 'f' at the end of the function name refers to “formatted”, indicating that the function requires a format string in addition to the values to be printed.

How do you read and write from a file in C?

For reading and writing to a text file, we use the functions fprintf() and fscanf(). They are just the file versions of printf() and scanf() . The only difference is that fprintf() and fscanf() expects a pointer to the structure FILE.


1 Answers

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());
 }
like image 80
Sudhakar Tillapudi Avatar answered Oct 03 '22 19:10

Sudhakar Tillapudi