Hi,

It looks like you needed to flush the writer. Also, you probably want to clear the response before the PDF and stop after it.  Here’s some code, hope it helps.

 

        Response.Clear();

        Response.ContentType = "application/pdf";

        System.IO.MemoryStream m = new System.IO.MemoryStream();

        iText.Document document = new iText.Document();

        iText.pdf.PdfWriter writer = iText.pdf.PdfWriter.GetInstance(document, m);

        document.Open();

        document.Add(new iText.Paragraph(DateTime.Now.ToString()));

        document.NewPage();

        document.Add(new iText.Paragraph("Hello World"));

        document.Close();

        writer.Flush();

        Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);

        Response.OutputStream.Flush();

        Response.OutputStream.Close();

        Response.End();

 

This works with iTextSharp 3.0.3

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of denver colorado
Sent: Friday, July 01, 2005 1:48 PM
To: [email protected]
Subject: [iText-questions] NewPage() does not work

 

Hi,

 

I am using iTextSharp to create a PDF doc and when I try to add a page I get an Acrobat Reader error error "The file is damaged and could not be repaired."

My code:

 

*******************************

// step 1

// need to write to memory first due to IE wanting

// to know the length of the pdf beforehand

MemoryStream m = new MemoryStream();

Document document = new Document();

try

{

// step 2: we set the ContentType and create an instance of the Writer

Response.ContentType = "application/pdf";

PdfWriter.GetInstance(document, m);

// step 3

document.Open();

// step 4

document.Add(new Paragraph(DateTime.Now.ToString()));

document.NewPage();

document.Add(new Paragraph("Hello World"));

catch (DocumentException ex)

{

Console.Error.WriteLine(ex.StackTrace);

Console.Error.WriteLine(ex.Message);

}

// step 5: Close document

document.Close();

// step 6: Write pdf bytes to outputstream

Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);

Response.OutputStream.Flush();

Response.OutputStream.Close();

*******************************

Could you help please

Thanks,

Vijit


Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.8/35 - Release Date: 6/30/2005


--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.8/35 - Release Date: 6/30/2005

Reply via email to