PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon May 06, 2024 7:03 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Dec 20, 2013 11:05 pm 
Offline

Joined: Fri Dec 20, 2013 10:58 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 24, 2013 7:14 am 
Offline

Joined: Fri Apr 13, 2012 6:33 am
Posts: 10
I would have written:

Code:
           Dim document As New PdfDocument()
           Dim page As PdfPage = document.AddPage()             


But I don't know if that's your problem, or if that's the correct VB.Net syntax.

Danny


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 24, 2013 2:27 pm 
Offline

Joined: Fri Dec 20, 2013 10:58 pm
Posts: 2
Danny,

much appreciate your reply. I changed my code to the following, but it did not help.

Code:
Dim page As PdfPage = document.AddPage() 'getNewPage()

                    '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


                    document.AddPage(page)


Any other suggestions?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 27, 2013 3:25 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 340
See this sample:
http://pdfsharp.net/wiki/Clock-sample.ashx

I don't see this line in your code:
Code:
Response.AddHeader("content-length", stream.Length.ToString());

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 75 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group