Results 1 to 2 of 2

Thread: iTextSharp issues

  1. #1
    Lotok is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2012
    Location
    Scotland
    Posts
    193
    Reputation
    22

    iTextSharp issues

    I am rewriting a Sharepoint 2010 Webpart to move away from PDFSharp to iTextSharp. The formatting required on this projects seems to lend itself better to iTextSharp's capabilities. With that said, I am having a few issues getting iTextSharp to play ball this morning and hoping we maybe have someone around experienced with it. I have done a fair amount of googling before throwing in the towel and asking for help.

    I am looking to create the PDF in a memorystream, then output to the browser (Worked in PDFSharp and same principals). For examples sake I have removed all of the intended content and just added a test paragraph to make the code sample more readable.

    This is what SHOULD work. However upon calling the open method, it returns errors. I have found the reason is the document being created does not appear to be writable. I have tried removing the code to open, add paragraph and close the document. When that is done, the document does not give an error but prompts to open or save as expected. If you save and take a look you can see the metadata hasnt written and the document name is default.

    Code:
                
    Using ms = New IO.MemoryStream()
    
    
                    
    Dim pdfDoc AsNew iTextSharp.text.Document(PageSize.A4, 25, 25, 25, 25)
    
                    
    Dim writer AsPdfWriter = PdfWriter.GetInstance(pdfDoc, ms)
    
    
                    
    'add metadata
    
    
                    pdfDoc.AddAuthor(
    "EA")
    
                    pdfDoc.AddSubject(
    "Change Request " & viewID)
    
                    pdfDoc.AddTitle(
    "Change Document")
    
    
                    pdfDoc.Open()
    
                    pdfDoc.Add(
    NewParagraph("Test"))
    
                    pdfDoc.Close()
    
                    writer.Close()
    
    
                    Response.ContentType = 
    "application/pdf"
    
                    Response.AddHeader(
    "content-disposition", "attachment;filename=First PDF document.pdf")
    
                    Response.Buffer = 
    True
    
                    Response.Clear()
    
                    Response.OutputStream.Write(ms.GetBuffer, 0, ms.GetBuffer.Length)
    
                    Response.OutputStream.Flush()
    
                    Response.Close()
    
    
     
    
                
    EndUsing
    
    Last edited by Lotok; 05-07-2012 at 6:23 AM. Reason: xcode

  2. #2
    Lotok is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2012
    Location
    Scotland
    Posts
    193
    Reputation
    22
    I just solved this. I dont know why I specified the buffer to be true as its default anyway. I was getting desperate i suppose!
    Code:
    response.buffer = true
    I wanted the browser to wait until the PDF was compiled, which is why that was the intended setting anyway. However I was wrong to do so. The whole problem was because I wasnt allowing the browser to create anything before the PDF was ready. Once I switched the buffer off, it works just fine.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking