按钮单击“从服务器打印PDF”

时间:2014-03-05 22:36:17

标签: asp.net vb.net printing onclick adobe

我正在转动这个轮子。我有一个使用vb.net的asp.net网站。我通过Intranet SharePoint页面链接到此ASP.net站点。我有一些代码可以从SSRS报告中生成pdf文件。现在在相同的代码行中,我想打印pdf文件。我发现这样做但是没有运气让它起作用:

    ''''PRINT REPORT
    ''get default printer
    Dim oPS As New System.Drawing.Printing.PrinterSettings
    Dim defaultprintername As String = ""
    Try
        'Set my print ---- defaultprintername = "Dell 3330dn Laser Printer XL"
        defaultprintername = oPS.PrinterName
    Catch ex As System.Exception
        defaultprintername = ""
        SendAllLabel.Text = "ERROR - Could not set printer"
    Finally
        oPS = Nothing
    End Try

    Dim pathToExecutable As String = "AcroRd32.exe"
    Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + strReportOutput + " " + defaultprintername + "")
    Dim Process As New Process()
    Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    Process.StartInfo.UseShellExecute = True
    Process.StartInfo.Verb = "print"
    Process.StartInfo.CreateNoWindow = True
    Process.StartInfo = starter
    ''WILL NOT START BUT NO EXCEPTION
    Try
        Process.Start()
        Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    Catch ex As System.Exception
        SendAllLabel.Text = "ERROR - Could not set print job."
    End Try

    System.Threading.Thread.Sleep(30000)

    Process.CloseMainWindow()
    Dim iLoop As Int16 = 0
    'check the process has exited or not
    If Process.HasExited = False Then
        Try
            Process.Kill()
        Catch ex As Exception
            SendAllLabel.Text = "ERROR - Could not stop process."
        End Try
        'if not then loop for 100 time to try and close the process'with 10 seconds delay
        While Not Process.HasExited
            System.Threading.Thread.Sleep(10000)
            Process.CloseMainWindow()
            iLoop = CShort(iLoop + 1)

            If iLoop >= 100 Then 


                Try
                    Process.Kill()
                Catch ex As Exception
                    SendAllLabel.Text = "ERROR - Could not stop process."
                End Try
                Exit While
            End If
        End While
    End If

    Process.Close()
    Process.Dispose()
    Process = Nothing
    starter = Nothing

需要做些什么才能让它发挥作用?我看到许多网站引用了这段代码,但没有任何东西超出它的范围。我需要授予权限吗?如果是这样,我该怎么做?服务器使用的是Adobe Reader 10.0,大多数用户使用的是Reader 11.0。

请和谢谢

1 个答案:

答案 0 :(得分:0)

您需要生成PDF然后将其流式传输到浏览器,这是我很久以前写的东西。

   Response.Clear()
    Response.ClearHeaders()
    Response.ClearContent()

    Response.ContentType = "application/pdf"

    ' IE & Acrobat seam to require "content-disposition" header being in the response.  If you don't add it, the doc still works most of the time, but not always.
    ' this makes a new window appear: 
    ' you can replace the filename with the actual filename
    Response.AddHeader("content-disposition", "attachment; filename=something.PDF")

    ' Create a new memory stream that will hold the pdf output
    Dim memStream As New System.IO.MemoryStream()

    ' Export the report to PDF:
    ' put the pdf file into the memStream
    ' the strpath variable should hold the full path and filename of the pdf file
    ' for example dim strPath = "\\hw.net\files\2014dt140305.105459.pdf"
    Dim bData As Byte()
    Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(strPath))
    bData = br.ReadBytes(br.BaseStream.Length)
    Dim memStream As MemoryStream = New MemoryStream(bData, 0, bData.Length)

    ' Write the PDF stream out
    Response.BinaryWrite(memStream.ToArray())

    ' Send all buffered content to the client
    Response.End()