打开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.