使用 vb.net 打印 PDF

发布于 2024-11-16 15:09:30 字数 228 浏览 2 评论 0原文

假设我有 PDF 文件的所有详细信息(文件名、打印机名称、页面等)。 有什么方法/代码可以用来打印 PDF 文档吗? (发送到打印队列)

但是有三个条件: 1.我已经使用了Process.startinfo方法并且需要其他的东西。 2.我无法使用任何外部COM或任何其他对象,例如AdobePDF 3. 我无法打开文件然后让用户手动打印。

抱歉,如果我提出了太多条件,但这就是我需要的。

谢谢!

Assuming that I have all the details of a PDF file (file name, printer name, pages etc).
Is there any way/code using which I can print a PDF document? (Send it to the printing queue)

But there are three conditions:
1. I have already used the Process.startinfo method and need something else.
2. I cant use any external COM or any other object like AdobePDF
3. I cant open the file and then let the user print manually.

Sorry if I am putting too many conditions, but that's how I need it.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感性不性感 2024-11-23 15:09:30

为了打印 PDF,必须首先将其呈现为图像。如果您只想在应用程序的屏幕上显示 PDF,情况也是如此。 PDF 的想法是打印纸张的电子版本。因此,查看/打印需要相同的基本步骤。

在不使用任何外部 COM/对象的情况下编写将 PDF 打印到打印机的代码在技术上是否可行?是的,绝对是。容易吗?绝对不是。您必须编写自己的 PDF 渲染引擎,这需要花费数千小时的工作。

因此,为了完成此任务,您需要使用第 3 方库来处理将 PDF 渲染为图像,然后将其发送到打印机。

看起来 PDFBox 最近添加了一个 PrintPDF 选项,不幸的是它是基于 Java 的,但有实际上这个领域并没有很多免费的选择。我公司生产的快速 PDF 库具有 PrintDocument 功能,可为您打印 PDF ,但它不是免费的,而且它是一个外部 COM。

简而言之:如果不调整您的要求并使用第 3 方库,您不太可能能够完成您想要做的事情。

In order to a print a PDF it must be rendered as an image first. The same goes if you just want to display the PDF on screen in your application. The idea is that PDF is the electronic version of a printed piece of paper. So the same basic steps are required to view/print.

Is it technically possible to write code that prints a PDF to the printer without using any external COM/object? Yes, absolutely. Is it easy? Absolutely not. You would have to write your own PDF rendering engine and that is thousands of hours of work.

So in order to accomplish this task you will need to use a 3rd party library to handle the rendering of the PDF to an image which can then be sent to the printer.

It looks like PDFBox has recently added a PrintPDF option which is unfortunately Java based, but there aren't actually a whole lot of free options in this area. My companies production Quick PDF Library has a PrintDocument feature which will print the PDF for you, but it's not free and it's an external COM.

In short: it's unlikely that you will be able to do what you're trying to do without adjusting your requirements and using a 3rd party library.

伏妖词 2024-11-23 15:09:30

您应该查看 ABCpdf

You should check out ABCpdf.

人生戏 2024-11-23 15:09:30

尝试通过命令行和以下代码使用lvbprint:

http://www.lvbprint.de/html/ gsbatchprint1.html

例如:
C:\temp\gsbatchprint64\gsbatchprintc.exe -P \server\printer-N A3 -O 端口 -FC:\temp\gsbatchprint64\Test*.pdf -I Tray3

    ' print a pdf with lvbrint
    Private Function UseLvbPrint(ByVal oPrinter As tb_Printer, fileName As String, portrait As Boolean, sTray As String) As String

        Dim lvbArguments As String
        Dim lvbProcessInfo As ProcessStartInfo
        Dim lvbProcess As Process

        Try

            Dim sPrinterName As String

                If portrait Then
                    lvbArguments = String.Format(" -P ""{0}"" -O Port -N A4 -F ""{1}"" -I ""{2}"" ", sPrinterName, fileName, sTray)
                Else
                    lvbArguments = String.Format(" -P ""{0}"" -O Land -N A4 -F ""{1}"" -I ""{2}"" ", sPrinterName, fileName, sTray)
                End If

            lvbProcessInfo = New ProcessStartInfo()
            lvbProcessInfo.WindowStyle = ProcessWindowStyle.Hidden

            lvbProcessInfo.FileName = LvbLocation
            lvbProcessInfo.Arguments = lvbArguments

            lvbProcessInfo.UseShellExecute = False

            lvbProcessInfo.RedirectStandardOutput = True
            lvbProcessInfo.RedirectStandardError = True


            lvbProcessInfo.CreateNoWindow = False

            lvbProcess = Process.Start(lvbProcessInfo)

            '
            ' Read in all the text from the process with the StreamReader.
            '
            Using reader As StreamReader = lvbProcess.StandardOutput
                Dim result As String = reader.ReadToEnd()
                WriteLog(result)
            End Using

            Using readerErr As StreamReader = lvbProcess.StandardError
                Dim resultErr As String = readerErr.ReadToEnd()
                If resultErr.Trim() > "" Then
                    WriteLog(resultErr)

                    lvbProcess.Close()
                    Return resultErr
                End If
            End Using

            If lvbProcess.HasExited = False Then
                lvbProcess.WaitForExit(3000)
            End If

            lvbProcess.Close()

            Return ""

        Catch ex As Exception
            Return ex.Message 
        End Try
    End Function

Try using lvbprint via command line and the following code:

http://www.lvbprint.de/html/gsbatchprint1.html

for example:
C:\temp\gsbatchprint64\gsbatchprintc.exe -P \server\printer-N A3 -O Port -F C:\temp\gsbatchprint64\Test*.pdf -I Tray3

    ' print a pdf with lvbrint
    Private Function UseLvbPrint(ByVal oPrinter As tb_Printer, fileName As String, portrait As Boolean, sTray As String) As String

        Dim lvbArguments As String
        Dim lvbProcessInfo As ProcessStartInfo
        Dim lvbProcess As Process

        Try

            Dim sPrinterName As String

                If portrait Then
                    lvbArguments = String.Format(" -P ""{0}"" -O Port -N A4 -F ""{1}"" -I ""{2}"" ", sPrinterName, fileName, sTray)
                Else
                    lvbArguments = String.Format(" -P ""{0}"" -O Land -N A4 -F ""{1}"" -I ""{2}"" ", sPrinterName, fileName, sTray)
                End If

            lvbProcessInfo = New ProcessStartInfo()
            lvbProcessInfo.WindowStyle = ProcessWindowStyle.Hidden

            lvbProcessInfo.FileName = LvbLocation
            lvbProcessInfo.Arguments = lvbArguments

            lvbProcessInfo.UseShellExecute = False

            lvbProcessInfo.RedirectStandardOutput = True
            lvbProcessInfo.RedirectStandardError = True


            lvbProcessInfo.CreateNoWindow = False

            lvbProcess = Process.Start(lvbProcessInfo)

            '
            ' Read in all the text from the process with the StreamReader.
            '
            Using reader As StreamReader = lvbProcess.StandardOutput
                Dim result As String = reader.ReadToEnd()
                WriteLog(result)
            End Using

            Using readerErr As StreamReader = lvbProcess.StandardError
                Dim resultErr As String = readerErr.ReadToEnd()
                If resultErr.Trim() > "" Then
                    WriteLog(resultErr)

                    lvbProcess.Close()
                    Return resultErr
                End If
            End Using

            If lvbProcess.HasExited = False Then
                lvbProcess.WaitForExit(3000)
            End If

            lvbProcess.Close()

            Return ""

        Catch ex As Exception
            Return ex.Message 
        End Try
    End Function
野生奥特曼 2024-11-23 15:09:30

听起来您需要将原始数据直接发送到打印机!(抱歉,在 C# 中)

我确实设法通过将 PDF 转换为字节数组,然后使用 TCP 将其直接发送到打印机,以静默方式打印到网络打印机。

如果您知道打印机的 IP 地址,则可以使用 TcpClient 将文件直接发送到打印机。我已经让它适用于我的打印机,但只尝试过 PDF,所以我不知道它对于其他打印机/文件类型的效果如何。

您必须更改打印机设置,使其使用 TCP 端口(在设备和打印机中选择您的打印机(单击),然后单击打印服务器属性,在打开的向导中您可以添加新的 TCP 端口)。您还必须将 [打印机设置为 raw 而不是 lpc][2] 设置,

然后我使用了类似于以下方法的方法;

    Public Sub SilentPrint(filePath As String, printerIPAddress As string)

        Dim bytes = System.IO.File.ReadAllBytes(filePath)

        Dim client = new TcpClient(printerIPAddress, 9100) '9100 is the default print port for raw data

        using  myStream  = client.GetStream()

            myStream.Write(bytes, 0, bytes.Length)
            myStream.Close()

        End Using

End Sub

它对我有用,但我不能确定它在所有情况下都有效。

It sounds like you need to send raw data straight to the printer! (sorry it's in C#)

I did manage to silently print to a network printer by converting the PDF to a byte array and then sending it directly to the printer using TCP.

If you know your printer's IP address it might be possible to send the file directly to the printer using TcpClient. I've got this to work for my printer, but have only tried it for PDFs so I don't know how well it will work for other printers/file types.

You will have to change your printer settings so that it is using a tcp port (In devices and printers select your printer (single click), then click print server properties, in the wizard that opens you can add a new TCP port). You will also have to set the [printer to raw rather than lpc][2] settings

I then used something similar to the following method;

    Public Sub SilentPrint(filePath As String, printerIPAddress As string)

        Dim bytes = System.IO.File.ReadAllBytes(filePath)

        Dim client = new TcpClient(printerIPAddress, 9100) '9100 is the default print port for raw data

        using  myStream  = client.GetStream()

            myStream.Write(bytes, 0, bytes.Length)
            myStream.Close()

        End Using

End Sub

It worked for me but I can't be certain it will work in all cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文