Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone i have an application which i wrote in c#, i want to send the pdf file to the printer.

i have this code below

C#
try
                       {
                           streamToPrint = new StreamReader("file1" + rand.ToString() + ".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);


and the event is
C#
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;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            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 more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }


With this code i am reading the content of the file then send it to printer, but i need to pdf files send directly to the printer. Since, i can not read the content of it.

Thanks
Posted

1 solution

Hello,

If you are developing a desktop application then you can following shell command to do the pdf printing. Your application user will require adobe acrobat reader though.

AcroRd32.exe /N /T PdfFile PrinterName [ PrinterDriver [ PrinterPort ] ]


The above code will silently print the pdf file to specified printer. If you want to show a print dialog box then use following command.

AcroRd32.exe /P PdfFile


You may find Run other programs from your .NET code library by Xiangyang Liu very useful.

Regards,
Prasad P. Khandekar
 
Share this answer
 
Comments
hilmisu 22-Nov-11 9:48am    
Thanks, i dont want print dialog, but how can i execute shell command ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900