打印在C#中现有的PDF(或其他文件) [英] Print existing PDF (or other files) in C#

查看:142
本文介绍了打印在C#中现有的PDF(或其他文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我建设我需要打印的PDF文件存在(由另一个应用程序创建的)的应​​用程序。
我怎样才能做到这一点在C#中,并提供一种机制,使用户可以选择不同的打印机或其他属性。

From an application I'm building I need to print existing PDFs (created by another app). How can I do this in C# and provide a mechanism so the user can select a different printer or other properties.

我看了PrintDialog类,但不知道是哪个文件试图如有打印,B / C的输出始终是一个空白页。也许我只是失去了一些东西在那里。

I've looked at the PrintDialog but not sure what file it is attempting to print, if any, b/c the output is always a blank page. Maybe I'm just missing something there.

我需要用iTextSharp的(如建议其他地方)?这似乎很奇怪我,因为我可以发送文件到打印机我只是没有任何漂亮的对话框之前手设置打印机等等,我真的不想写从地上爬起来的打印对话框但它似乎像很多的例子,我发现通过搜索就是这样做的。

Do I need to use "iTextSharp" (as suggested else where)? That seems odd to me since I can "send the the file to the printer" I just don't have any nice dialog before hand to set the printer etc. and I don't really want to write a printing dialog from the ground up but it seems like a lot of examples I found by searching did just that.

任何意见,例子或样本code将是伟大的!

Any advice, examples or sample code would be great!

此外,如果是PDF文件可以通过在diff格式,如位图或PNG其他应用程序如果让事情变得更容易。

Also if PDF is the issue the files could be created by the other app in a diff format such as bitmap or png if that makes things easier.

推荐答案

显示与该有它的项目设置为 PrinterSettings.InstalledPrinters 。

Display a little dialog with a combobox that has its Items set to the string collection returned by PrinterSettings.InstalledPrinters.

如果你可以把它的用GSView 在机器上安装了一个要求,你然后默默打印PDF。这是一个有点慢,迂回,但至少你不必弹出的Acrobat。

If you can make it a requirement that GSView be installed on the machine, you can then silently print the PDF. It's a little slow and roundabout but at least you don't have to pop up Acrobat.

下面是一些code我用打印出某些PDF我找回从UPS Web服务:

Here's some code I use to print out some PDFs that I get back from a UPS Web service:

    private void PrintFormPdfData(byte[] formPdfData)
    {
        string tempFile;

        tempFile = Path.GetTempFileName();

        using (FileStream fs = new FileStream(tempFile, FileMode.Create))
        {
            fs.Write(formPdfData, 0, formPdfData.Length);
            fs.Flush();
        }

        try
        {
            string gsArguments;
            string gsLocation;
            ProcessStartInfo gsProcessInfo;
            Process gsProcess;

            gsArguments = string.Format("-grey -noquery -printer \"HP LaserJet 5M\" \"{0}\"", tempFile);
            gsLocation = @"C:\Program Files\Ghostgum\gsview\gsprint.exe";

            gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
            gsProcessInfo.FileName = gsLocation;
            gsProcessInfo.Arguments = gsArguments;

            gsProcess = Process.Start(gsProcessInfo);
            gsProcess.WaitForExit();
        }
        finally
        {
            File.Delete(tempFile);
        }
    }

正如你所看到的,它需要的PDF数据作为一个字节数组,它写入到一个临时文件,并启动gsprint.exe以静默方式打印的文件到指定的打印机(HP LASERJET 5M)。你可以替换无论用户选择在对话框中的打印机名称。

As you can see, it takes the PDF data as a byte array, writes it to a temp file, and launches gsprint.exe to print the file silently to the named printer ("HP Laserjet 5M"). You could replace the printer name with whatever the user chose in your dialog box.

打印PNG或GIF会容易得多 - 只需扩展PrintDocument类,并使用Windows窗体提供正常的打印对话框

Printing a PNG or GIF would be much easier -- just extend the PrintDocument class and use the normal print dialog provided by Windows Forms.

祝你好运!

这篇关于打印在C#中现有的PDF(或其他文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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