打开pdf文档时出现错误 [英] Error is comming when opening pdf document

查看:157
本文介绍了打开pdf文档时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网页中的母版页,当我打开pdf文件时,在脚本编写中会产生错误

错误在下面给出

I am using master page in a web page and When i opening the pdf file, The error is generated in scripting

The error is given below

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '%PDF-1.4
%��2 0 obj '

.
我打开pdf的代码如下所示

.
My code to open pdf is given below

Public Sub ExportToPdf(ByVal ExDataTable As DataTable)
        'Here set page size as A4

        Dim pdfDoc As New Document(PageSize.A4, 10, 10, 10, 10)

        Try
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream)
            pdfDoc.Open()

            'Set Font Properties for PDF File
            Dim fnt As Font = FontFactory.GetFont("Times New Roman", 12)
            Dim dt As DataTable = ExDataTable
            'dt.Columns(0).ColumnName = "A"
            'dt.Columns(1).ColumnName = "B"
            'dt.Columns(2).ColumnName = "c"
            'dt.Columns(3).ColumnName = "d"


            If dt IsNot Nothing Then
                '----
                Dim tblHeader As New PdfPTable(4)
                tblHeader.WidthPercentage = 85
                tblHeader.SetWidths(New Integer(3) {1, 1.8, 1, 1})
                Dim cellDocHdr As New PdfPCell(New Phrase("Order Summary"))
                cellDocHdr.Colspan = 4
                cellDocHdr.HorizontalAlignment = 1
                cellDocHdr.Border = 0
                tblHeader.AddCell(cellDocHdr)
                pdfDoc.Add(tblHeader)

                Dim tblBlank As New PdfPTable(4)
                Dim cellBlank As New PdfPCell(New Phrase(" "))
                cellBlank.Colspan = 4
                cellBlank.HorizontalAlignment = 1
                cellBlank.Border = 0
                tblBlank.AddCell(cellBlank)
                pdfDoc.Add(tblBlank)
                '--

                Dim PdfTable As New PdfPTable(dt.Columns.Count)
                'PdfTable.SetWidths(New Single(6) {0.5, 0.9, 1.5, 2.5, 1, 1, 0.8})
                PdfTable.SetWidths(New Single(6) {2.0, 1.5, 1.5, 1.5, 2.0, 2.0, 2.0})
                PdfTable.WidthPercentage = 102
                Dim PdfPCell As PdfPCell = Nothing

                'Here we create PDF file tables

                For rows As Integer = 0 To dt.Rows.Count - 1
                    If rows = 0 Then
                        For column As Integer = 0 To dt.Columns.Count - 1
                            PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Columns(column).ColumnName.ToString(), fnt)))
                            PdfPCell.HorizontalAlignment = 1
                            PdfTable.AddCell(PdfPCell)
                        Next
                    End If
                    For column As Integer = 0 To dt.Columns.Count - 1
                        PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Rows(rows)(column).ToString(), fnt)))
                        PdfPCell.HorizontalAlignment = 1
                        PdfTable.AddCell(PdfPCell)
                    Next
                Next

                ' Finally Add pdf table to the document 
                pdfDoc.Add(PdfTable)
            End If

            pdfDoc.Close()

            Response.ContentType = "application/pdf"

            'Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" & DateTime.Now.ToString("yyyyMMdd") & ".pdf")

            System.Web.HttpContext.Current.Response.Write(pdfDoc)

            Response.Flush()

            Response.End()
        Catch ex As Exception
            Response.Write(ex.ToString())
        End Try
    End Sub

推荐答案

"System.Web.HttpContext.Current.Response.Write(pdfDoc)"

为什么在这里将响应"从上下文"中拉出,而不是在其他所有地方?您是否尝试过使此代码使用相同的Response对象?我知道它们应该相同,但这似乎是一个潜在的问题,至少是不合逻辑的.
"System.Web.HttpContext.Current.Response.Write(pdfDoc)"

Why pull the Response out of the Context here, but not everywhere else ? Have you tried making this code use the same Response object ? I know they SHOULD be the same, but it seems like a potential issue and, at least, illogical.


这篇关于打开pdf文档时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆