使用C#打印pdf文件 [英] Printing pdf files with c#

查看:491
本文介绍了使用C#打印pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个用C#编写的应用程序,我想将pdf文件发送到打印机.

我在下面有此代码

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

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

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);
        }



使用此代码,我正在读取文件的内容,然后将其发送到打印机,但是我需要将pdf文件直接发送到打印机.因为,我看不懂它的内容.

谢谢



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

推荐答案

你好,

如果要开发桌面应用程序,则可以按照shell命令进行pdf打印.您的应用程序用户将需要Adobe Acrobat Reader.

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 ] ]



上面的代码将以静默方式将pdf文件打印到指定的打印机.如果要显示打印对话框,请使用以下命令.



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



您可能会发现 通过.b代码从.NET代码库中运行其他程序 >刘向阳非常有用.

问候,
Prasad P. Khandekar



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

Regards,
Prasad P. Khandekar


这篇关于使用C#打印pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆