0

I am developing C# application. In this C# program, i want to add print button so users may print any document they want by using this button. I managed to print a txt file by reading it. But if i choose a pdf file instead, program prints some error codes in result.

private void printButton_Click(object sender, EventArgs e)
{
    try
    {
        streamToPrint = new StreamReader("C:\\test.txt");
        try
        {
            printFont = new Font("Arial", 10);
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;

    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

    while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
    {
        yPos = topMargin + (count *
           printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(line, printFont, Brushes.Black,
           leftMargin, yPos, new StringFormat());
        count++;
    }

    if (line != null)
        ev.HasMorePages = true;
    else
        ev.HasMorePages = false;
}

Besides these, is there any way to open ctrl-p menu on the document(printing options)? Then user may choose any printer he/she desires.

I would appreciate your helps.

My regards...

Alasse
  • 361
  • 2
  • 9
  • 17

2 Answers2

1

You can not read a pdf file line by line. PDF is a binary format, not a text format. To clearify this try to open the PDF file in notepad (or your prefered text editor). You can use one of the many available libraries to acomplish this anyway or write your own PDF interpreter. I would recommend the available library iTextSharp (http://itextpdf.com/)

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • Thanks for reply. Yeah i realized that you are right. Is there any way to print the file without reading it line by line in C#? – Alasse Aug 21 '13 at 09:11
  • 2
    An easy way would be to use AcrobatReader for this job. You could silently print the file via this shell command: `AcroRd32.exe /N /T PdfFile PrinterName [ PrinterDriver [ PrinterPort ] ]` Or show a print dialoge with this shell command `AcroRd32.exe /P PdfFile`. But a much better way would be to use one of the many available .Net PDF libraries and use their printing capabilities. – Romano Zumbé Aug 21 '13 at 09:15
  • Thank you, it helped. But i can do that when i am printing PDF files. But what about other files such like jpeg, word etc? Converting them into pdf is another solution but isn't there any easier way to do that? – Alasse Aug 21 '13 at 09:19
  • 1
    There is no simple way to print every document format. I assume that you open most of these formats with different applications. All these applications implement different ways to read the files and print them. You would have to do that, too. Printing common image formats is not a big problem, since you can use .Nets build in functionality. Word documents can be printed using Microsofts Interop components. Print Images http://stackoverflow.com/questions/5750659/print-images-c-net Print Word documents: http://stackoverflow.com/questions/878302/printing-using-word-interop-with-print-dialog – Romano Zumbé Aug 21 '13 at 09:23
  • Then i am going for hard way. Assign itextpdf library codes to pdf files, word printer to word files etc. Thanks for answer! – Alasse Aug 21 '13 at 09:26
1

There is a PrintDialog class which should present those options. But I'm not sure this will get you far as Romano has brought up the point that you can't read the PDF without a library like iTextSharp or PDFSharp.

Papa
  • 1,628
  • 1
  • 11
  • 16