iTextSharp + FileStream = Corrupt PDF file

前端 未结 5 1716
有刺的猬
有刺的猬 2020-12-05 16:04

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. Th

5条回答
  •  广开言路
    2020-12-05 16:52

    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);
        }
    }
    

提交回复
热议问题