Return and Consume iTextSharp PDF from Class

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Return and Consume iTextSharp PDF from Class

Hello everyone and thanks for your help in advance.  I am developing a MVC application that utilizes iTextSharp to create dynamic PDF files.  These PDF files will need to be consumed through different parts of the application and may be either written to the browser of saved to the file system.  Therefore I need to create a help function or class that creates the PDF and returns it to he consuming application.  I am having problems with how to return the file.  Here is my sample code:

    Public Function BuildPDF(ByVal)

        ' open the reader
        Dim reader As New PdfReader(SourceFile)
        Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
        Dim document As New Document(size)

        ' open the writer
        Dim ms As MemoryStream = New MemoryStream()
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)

        ' the pdf content
        Dim cb As PdfContentByte = writer.DirectContent

        ' create the new page and add it to the pdf
        Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
        cb.AddTemplate(page, 0, 0)

        ' select the font properties
        Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
        cb.SetFontAndSize(bf, 12)

        ' write the Hello World
        ' put the alignment and coordinates here
        cb.ShowTextAligned(3, "Hello World", 300, 300, 0)

        ' close the stream


        Return ms.toarray

End Function

I then tried:

        Dim myByte As Byte() = ReferralForm.BuildPDF

        File.WriteAllBytes("E:\test.pdf", myByte)

Which writes the file to disk perfectly.  However, when I try something similar with trying to write to a browser:

        Dim ReferralForm As New PDFFactory.ReferralForm
        Dim myByte As Byte() = ReferralForm.BuildPDF()

        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=file.pdf")
        Response.Buffer = True
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length)

I get the following error:

MemoryStream's internal buffer cannot be accessed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: MemoryStream's internal buffer cannot be accessed.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

I know I can't be the only person trying to do this, but I am not sure what to do to get this working.  Any help would be appreciated.