Create PDF in memory instead of physical file

后端 未结 4 1868
说谎
说谎 2020-11-27 04:23

How do one create PDF in memorystream instead of physical file using itextsharp.

The code below is creating actual pdf file.

Instead how can I create a byte[

4条回答
  •  青春惊慌失措
    2020-11-27 04:48

    Where your code has new FileStream, pass in a MemoryStream you've already created. (Don't just create it inline in the call to PdfWriter.GetInstance - you'll want to be able to refer to it later.)

    Then call ToArray() on the MemoryStream when you've finished writing to it to get a byte[]:

    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter wri = PdfWriter.GetInstance(doc, output);
    
        // Write to document
        // ...
        return output.ToArray();
    }
    

    I haven't used iTextSharp, but I suspect some of these types implement IDisposable - in which case you should be creating them in using statements too.

提交回复
热议问题