10

I am trying to create a pdf file with iTextSharp. My attempt writes the content of the pdf to a MemoryStream so I can write the result both into file and a database BLOB. The file gets created, has a size of about 21kB and it looks like a pdf when opend with Notepad++. But my PDF viewer says it's currupted. Here is a little code snippet (only tries to write to a file, not to a database):

Document myDocument = new Document();
MemoryStream myMemoryStream = new MemoryStream();
PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);
myDocument.Open();
// Content of the pdf gets inserted here
using (FileStream fs = File.Create("D:\\...\\aTestFile.pdf"))
{
    myMemoryStream.WriteTo(fs);
}
myMemoryStream.Close();

Where is the mistake I make?

Thank you, Norbert

1
  • do you need that MemoryStream? do you closed that myDocument before writing to FileStream? Commented Feb 2, 2010 at 19:07

5 Answers 5

31

I think your problem was that you weren't properly adding content to your PDF. This is done through the Document.Add() method and you finish up by calling Document.Close().

When you call Document.Close() however, your MemoryStream also closes so you won't be able to write it to your FileStream as you have. You can get around this by storing the content of your MemoryStream to a byte array.

The following code snippet works for me:

using (MemoryStream myMemoryStream = new MemoryStream()) {
    Document myDocument = new Document();
    PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);

    myDocument.Open();

    // Add to content to your PDF here...
    myDocument.Add(new Paragraph("I hope this works for you."));

    // We're done adding stuff to our PDF.
    myDocument.Close();

    byte[] content = myMemoryStream.ToArray();

    // Write out PDF from memory stream.
    using (FileStream fs = File.Create("aTestFile.pdf")) {
        fs.Write(content, 0, (int)content.Length);
    }
}
0
5

I had similar issue. My file gets downloaded but the file size will be 13Bytes. I resolved the issue when I used binary writer to write my file

byte[] bytes = new byte[0];
//pass in your API response into the bytes initialized

using (StreamWriter streamWriter = new StreamWriter(FilePath, true))
{
   BinaryWriter binaryWriter = new BinaryWriter(streamWriter.BaseStream);
   binaryWriter.Write(bytes);
}
1
  • 3
    This answer could be improved by explaining why this fixes the issue and what the mistake was.
    – Azsgy
    Commented Mar 28, 2018 at 18:49
0

Just some thoughts - what happens if you replace the memory stream with a file stream? Does this give you the result you need? This will at least tell you where the problem could be.

If this does work, how do the files differ (in size and binary representation)?

Just a guess, but have you tried seeking to the beginning of the memory stream before writing?

myMemoryStream.Seek(0, SeekOrigin.Begin);
1
  • The following didn't work either: Document myDocument = new Document(); PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, new FileStream(path, FileMode.Create)); myDocument.Open(); // Content ... myDocument.Close(); Corrupted file... :(
    – Norbert
    Commented Feb 2, 2010 at 20:50
0

Try double checking your code that manipulates the PDF with iText. Make sure you're calling the appropriate EndText method of any PdfContentByte objects, and make sure you call myDocument.Close() before writing the file to disk. Those are things I've had problems with in the past when generating PDFs with iTextSharp.

0
documentobject.Close();
using (FileStream fs = System.IO.File.Create(path)){                        
    Memorystreamobject.WriteTo(fs);
}         

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.