ssrs到内存中的字节和字节到pdf

时间:2015-09-09 16:49:52

标签: asp.net vb.net pdf ssrs-2008 itextsharp

我正在尝试将ssrs报告复制到内存中的pdf,但是当我打开pdf'文件格式不正确时,我收到错误消息' 。我正在使用asp.net和iTextSharp。

我想将ssrs复制到内存中的新pdf(rptpdf)文件,然后打开现有的pdf并将图像复制到该页面的第二页(rptpdf)pdf。

出于测试目的,我试图看看我是否可以将ssrs复制到pdf。

       Dim doc As New iTextSharp.text.Document()
       Dim content As Byte()
      Try
        Using myMemoryStream As New MemoryStream()

            Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, myMemoryStream)
            doc.Open()


            Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
            Dim page As iTextSharp.text.pdf.PdfImportedPage
            '--- bytes = rptViewer.ServerReport.Render("PDF", "", "", "", "", Nothing, Nothing)
            Dim b() As Byte = ReportRender.GetReport()

            Dim reader2 As New iTextSharp.text.pdf.PdfReader(b)

            Dim pages As Integer = reader2.NumberOfPages

            For i As Integer = 1 To pages
                doc.SetPageSize(iTextSharp.text.PageSize.LETTER)
                doc.NewPage()
                page = writer.GetImportedPage(reader2, i)
                cb.AddTemplate(page, 0, 0)
            Next

            doc.Close()
            reader2.Close()
            writer.Close()


            content = myMemoryStream.ToArray()
            myMemoryStream.Close()
        End Using



        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=" & "hello" & ".pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Write(content)

已更新

我已设法将reportviewer报告字节复制到内存流,但是在添加pdf文件时(第" writer.AddPage(页)")我得到错误' object not reference'

           Using myMemoryStream As New MemoryStream()


        Dim rptpdfwriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, myMemoryStream)

        If rptpdfwriter Is Nothing Then
            Return
        End If

        doc.Open()

        Dim cb As iTextSharp.text.pdf.PdfContentByte = rptpdfwriter.DirectContent
        Dim page2 As iTextSharp.text.pdf.PdfImportedPage
        '--- bytes = rptViewer.ServerReport.Render("PDF", "", "", "", "", Nothing, Nothing)
        Dim b() As Byte = ReportRender.GetReport()

        Dim reader2 As New iTextSharp.text.pdf.PdfReader(b)

        Dim pages2 As Integer = reader2.NumberOfPages

        For i As Integer = 1 To pages2
            doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate)
            doc.NewPage()
            page2 = rptpdfwriter.GetImportedPage(reader2, i)
            cb.AddTemplate(page2, 0, 0)
        Next


        reader2.Close()



        For Each item As Expenses In expenses
            Select Case item.Ext.ToLower
                Case ".pdf"
                    Dim writer As New iTextSharp.text.pdf.PdfCopy(doc, myMemoryStream)
                    Dim reader As New iTextSharp.text.pdf.PdfReader(Server.MapPath("/docs/expenses/" + item.ExpenseID.ToString + item.Ext))
                    reader.ConsolidateNamedDestinations()

                    For i As Integer = 1 To reader.NumberOfPages
                        Dim page As iTextSharp.text.pdf.PdfImportedPage = writer.GetImportedPage(reader, i)
                        writer.AddPage(page)
                    Next

                    Dim form As iTextSharp.text.pdf.PRAcroForm = reader.AcroForm
                    If form IsNot Nothing Then
                        writer.CopyAcroForm(reader)
                    End If

                    reader.Close()

                Case ".jpg"
                    Dim pdfWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, myMemoryStream)
                    Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("/docs/expenses/" + item.ExpenseID.ToString + item.Ext))

                    If Not doc.IsOpen Then doc.Open()
                    doc.Add(jpg)
                Case Else
                    Exit Sub

            End Select


        Next



        doc.Close()

        Content = myMemoryStream.ToArray()

        myMemoryStream.Flush()
        myMemoryStream.Dispose()

        Response.ContentType = "application/pdf"
        Response.AppendHeader("Content-Disposition", "attachment; filename=LeftCorneraaa.pdf")
        Response.BinaryWrite(Content)

    End Using

0 个答案:

没有答案