Convert bytes to pdf using itextsharp and asp.net

I actually wanted to make an editable PDF, but was not able to. So I thought to give users a webform and they fill it out and then generate PDF from it. In that way I can save his/her entries in my SQL table for future reference.

I am using itextsharp.I have an aspx page. On that I have radgrids,textboxes. I have to convert this to pdf. I read to convert to byte and then it would be easy to convert to pdf. Well, i am not getting it.

  Protected Sub btnGeneratePDF_Click(sender As Object, e As EventArgs) Handles btnGeneratePDF.Click
    Dim strWriter As New StringWriter()
    Dim byteFinalPDF As Byte()
    Dim myClient As New Net.WebClient()
    byteFinalPDF = myClient.DownloadData(HttpContext.Current.Request.Url.AbsoluteUri)
    If byteFinalPDF Is Nothing Then
        Throw New ApplicationException("Could not generate Work Order PDF for Work Order# " & intWorkOrderID)
    End If
    Dim all As Byte()
    Dim fs As New FileStream("C:MyPathInspectionSheet.pdf", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)

    Using ms As New MemoryStream()
        Dim doc As New iTextSharp.text.Document()
        Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(doc, ms)
        Dim includeHeader As New TableHeader() 'ERROR

        doc.SetPageSize(PageSize.LETTER)
        doc.Open()
        Dim pcb As PdfContentByte = pdfWriter.DirectContent
        Dim page As PdfImportedPage = Nothing
        Dim pdfReader As PdfReader = Nothing
        pdfReader = New PdfReader(byteFinalPDF)
        Dim pages As Integer = pdfReader.NumberOfPages

        For i As Integer = 1 To pages
            doc.SetPageSize(PageSize.LETTER)
            doc.NewPage()
            page = pdfWriter.GetImportedPage(pdfReader, i)
            pcb.AddTemplate(page, 0, 0)
        Next

        doc.Close()
        all = ms.GetBuffer()
        ms.Flush()
        ms.Dispose()
    End Using
      End Sub

Here is what i have tried. Please see the comment "ERROR" in the above code.I actually commented it out in my Visual studio. Just put it here for you all to see.The error says "Type TableHeader not defined".

I want to click a button and see a pdf be available to download. I gave a path for my filestream. I think it will be saved there. I want user to see the generated pdf to be downloaded at the bottom . It was giving an exception like this when I click button

        iTextSharp.text.exceptions.InvalidPdfException was unhandled by user code
        HResult=-2146232800
         Message=PDF header signature not found.
        Source=itextsharp
           StackTrace:
       at iTextSharp.text.pdf.PdfReader..ctor(IRandomAccessSource byteSource, Boolean partialRead, Byte[] ownerPassword, X509Certificate certificate, ICipherParameters certificateKey, Boolean closeSourceOnConstructorError)
      at iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn, Byte[] ownerPassword)
      at iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn)
   at Web_FolderUser_InspectionForm.btnGeneratePDF_Click(Object sender, EventArgs e) in C:Users...InspectionForm.aspx.vb:line 128
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
         at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
      at  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
     at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,  Boolean includeStagesAfterAsyncPoint)
InnerException: 

Line 128 being

    pdfReader = New PdfReader(byteFinalPDF)

Where and what am I missing?If you need more info, please let me know. Thanks.


Please take a look at my answer to the following question: Adding page to existing PDF com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found

In your code you define byteFinalPDF like this:

byteFinalPDF = myClient.DownloadData(HttpContext.Current.Request.Url.AbsoluteUri)

It is unclear what DownloadData is doing, but in any case: you are not downloading a PDF file, because a PDF file starts with the bytes %PDF-1. It's also unclear why you're using PdfWriter in combination with PdfReader . You should read chapter 6 of my book and then explain on SO what you want to achieve.

If I had to guess right now, I'd assume that you want people to fill out an HTML form in the browser and you want to present them with a filled out form in PDF after they click a button. The way to do this, would be to retrieve the data parameter per parameter and fill out the PDF form using PdfReader , PdfStamper and AcroFields . If that's not what you want to do, you'll have to clarify.

链接地址: http://www.djcxy.com/p/46744.html

上一篇: C#文件流读取

下一篇: 使用itextsharp和asp.net将字节转换为pdf