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

查看:22
本文介绍了在 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.

任何建议、示例或示例代码都会很棒!

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.

推荐答案

显示一个带有组合框的小对话框,该组合框的 Items 设置为 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.

这是我用来打印从 UPS 网络服务返回的一些 PDF 的一些代码:

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 FilesGhostgumgsviewgsprint.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天全站免登陆