开发者

Printing a PDF using vb.net

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!


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.


You should check out ABCpdf.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜