We are streaming our pdf to the browser and don't know how to get adobe reader to allow it to open. We get the following error when we run our code (using version 1.31 or 1.32)
file does not begin with %PDF-
Can anyone offer any suggestions? This is vb.net code
Thank you,
Code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.DataSet
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing.Layout
Partial Class PDFEnvelope
Inherits System.Web.UI.Page
Protected Function getNewPage() As PdfPage
Dim page As New PdfPage()
Dim pdfWidth As New XUnit(4.125, XGraphicsUnit.Inch)
Dim pdfHeight As New XUnit(9.5, XGraphicsUnit.Inch)
page.Height = pdfHeight
page.Width = pdfWidth
page.Orientation = PageOrientation.Landscape
Return page
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim MySQL As String = "SELECT FirstName, LastName, Address, City, State, Zip FROM Clients"
' Populate reader with sql from dr.
Dim ConnectStr As String = _
ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim MyConn As New SqlConnection(ConnectStr)
Dim objDR As SqlDataReader
Dim Cmd As New SqlCommand(MySQL, MyConn)
Cmd.CommandType = CommandType.Text
MyConn.Open()
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If objDR.HasRows Then
Response.Clear()
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf
Dim document As New PdfDocument()
Dim page As PdfPage = getNewPage()
document.AddPage(page)
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Create a font
Dim font As New XFont("Arial", 13)
' Draw the text
Dim x As Double = page.Width.Point / 3
Dim y As Double = page.Height.Point / 2
While objDR.Read()
gfx.DrawString(objDR.Item("First Name").ToString() + " " + objDR.Item("Last Name").ToString(), font, XBrushes.Black, New XPoint(x, y))
If (objDR("OrgName").ToString.Trim = "") Then
gfx.DrawString(objDR.Item("Address").ToString(), font, XBrushes.Black, New XPoint(x, y + 12 * 1.5))
gfx.DrawString(objDR.Item("City").ToString() + ", " + objDR.Item("State").ToString() + " " + objDR.Item("Zip").ToString(), font, XBrushes.Black, New XPoint(x, y + 12 * 3))
Else
gfx.DrawString(objDR.Item("CompanyName").ToString(), font, XBrushes.Black, New XPoint(x, y + 12 * 1.5))
gfx.DrawString(objDR.Item("Address").ToString(), font, XBrushes.Black, New XPoint(x, y + 12 * 3))
gfx.DrawString(objDR.Item("City").ToString() + ", " + objDR.Item("State").ToString() + " " + objDR.Item("Zip").ToString(), font, XBrushes.Black, New XPoint(x, y + 12 * 4.5))
End If
page = getNewPage()
document.AddPage(page)
gfx = XGraphics.FromPdfPage(page)
End While
Dim ms As New System.IO.MemoryStream()
document.Save(ms)
Response.Clear()
Response.ContentType = "application/pdf"
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(ms.ToArray())
Response.Flush()
ms.Close()
Response.[End]()
End If
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Class