【问题标题】:Print existing PDF (or other files) in C#在 C# 中打印现有的 PDF(或其他文件)
【发布时间】:2010-09-21 09:09:10
【问题描述】:

从我正在构建的应用程序中,我需要打印现有的 PDF(由另一个应用程序创建)。 如何在 C# 中执行此操作并提供一种机制,以便用户可以选择不同的打印机或其他属性。

我查看了 PrintDialog 但不确定它正在尝试打印什么文件,如果有的话,b/c 输出始终是空白页。也许我只是在那里遗漏了一些东西。

我是否需要使用“iTextSharp”(如其他地方建议的那样)?这对我来说似乎很奇怪,因为我可以“将文件发送到打印机”我只是事先没有任何好的对话框来设置打印机等,而且我真的不想从头开始编写打印对话框但似乎我通过搜索找到的很多示例都是这样做的。

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

此外,如果 PDF 是问题,其他应用程序可以使用差异格式(如位图或 png)创建文件,如果这样更容易的话。

【问题讨论】:

  • 这里有谁知道如何打印随机文件,而不仅仅是 pdf?托比
  • @Tobias:随机文件与随机应用程序相关联。甚至 .doc 也可以与写字板、Word 或 OpenOffice 相关联。每个应用程序都有自己的渲染。因此,唯一有用的方法是利用 Windows 的文件关联来处理所涉及的文件类型。

标签: c# printing


【解决方案1】:

显示一个带有组合框的小对话框,该组合框的 Items 设置为 PrinterSettings.InstalledPrinters 返回的字符串集合。

如果您可以要求在机器上安装GSView,则可以静默打印 PDF。有点慢和绕圈,但至少你不必弹出 Acrobat。

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

    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”)。您可以将打印机名称替换为用户在对话框中选择的任何名称。

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

祝你好运!

【讨论】:

  • 这对我帮助很大。我有一个 Intranet Web 应用程序,它需要将文档打印到无人值守的网络打印机。所有其他方法都失败了,但这个方法有效!
【解决方案2】:

虽然这是VB,但您可以轻松翻译它。顺便说一下Adobe没有弹出,它只打印pdf然后就消失了。

''' <summary>
''' Start Adobe Process to print document
''' </summary>
''' <param name="p"></param>
''' <remarks></remarks>
Private Function printDoc(ByVal p As PrintObj) As PrintObj
    Dim myProcess As New Process()
    Dim myProcessStartInfo As New ProcessStartInfo(adobePath)
    Dim errMsg As String = String.Empty
    Dim outFile As String = String.Empty
    myProcessStartInfo.UseShellExecute = False
    myProcessStartInfo.RedirectStandardOutput = True
    myProcessStartInfo.RedirectStandardError = True

    Try

        If canIprintFile(p.sourceFolder & p.sourceFileName) Then
            isAdobeRunning(p)'Make sure Adobe is not running; wait till it's done
            Try
                myProcessStartInfo.Arguments = " /t " & """" & p.sourceFolder & p.sourceFileName & """" & " " & """" & p.destination & """"
                myProcess.StartInfo = myProcessStartInfo
                myProcess.Start()
                myProcess.CloseMainWindow()
                isAdobeRunning(p)
                myProcess.Dispose()
            Catch ex As Exception
            End Try
            p.result = "OK"
        Else
            p.result = "The file that the Document Printer is tryng to print is missing."
            sendMailNotification("The file that the Document Printer is tryng to print" & vbCrLf & _
            "is missing. The file in question is: " & vbCrLf & _
            p.sourceFolder & p.sourceFileName, p)
        End If
    Catch ex As Exception
        p.result = ex.Message
        sendMailNotification(ex.Message, p)
    Finally
        myProcess.Dispose()
    End Try
    Return p
End Function

【讨论】:

【解决方案3】:

您将需要 Acrobat 或其他可以打印 PDF 的应用程序。从那里您 P/Invoke 到 ShellExecute 以打印文档。

【讨论】:

  • 你能通过该调用传递打印机选择和其他参数吗?
【解决方案4】:

您也可以使用 PDFsharp - 它是一个用于创建和处理 PDF 的开源库。 http://www.pdfsharp.net/

【讨论】:

【解决方案5】:

我正在为我的项目做同样的事情,它对我有用

看看能不能帮到你……

Process p = new Process();
p.EnableRaisingEvents = true; //Important line of code
p.StartInfo = new ProcessStartInfo()
{
    CreateNoWindow = true,
    Verb = "print",
    FileName = file,
    Arguments = "/d:"+printDialog1.PrinterSettings.PrinterName
};   
try
{
    p.Start();
} 
catch 
{ 
    /* your fallback code */ 
}

你也可以玩不同的windows选项

PRINT 命令以获得所需的输出...Reference link

【讨论】:

    【解决方案6】:

    在对这项任务进行大量研究和谷歌搜索后,Microsoft 发布了一个很棒的 KB,可以在不需要任何其他应用程序的情况下打印 pdf。无需调用 adobe 或 ghostprint。它可以打印而无需将文件保存到磁盘,这让生活变得非常轻松。

    http://support2.microsoft.com/?kbid=322091

    【讨论】:

    • 它适用于一些打印机。打印机需要能够自己处理 PDF 文件。如果不是,则 PDF 文件会像文本文件一样打印。
    • 上面的链接重定向到support.microsoft.com/?kbid=322091 什么都没有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2021-10-04
    • 1970-01-01
    • 2011-07-23
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多