在按钮上单击从服务器打印 PDF



我在这个问题上旋转我的轮子。我有一个使用 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。

请和谢谢

您需要生成 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.netfiles2014dt140305.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()

最新更新