Create PDF in memory instead of physical file

后端 未结 4 1866
说谎
说谎 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:56

    I've never used iTextPDF before but it sounded interesting so I took upon the challenge and did some research on my own. Here's how to stream the PDF document via memory.

    protected void Page_Load(object sender, EventArgs e)
    {
        ShowPdf(CreatePDF2());
    }
    
    private byte[] CreatePDF2()
    {
        Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
    
        using (MemoryStream output = new MemoryStream())
        {
            PdfWriter wri = PdfWriter.GetInstance(doc, output);
            doc.Open();
    
            Paragraph header = new Paragraph("My Document") {Alignment = Element.ALIGN_CENTER};
            Paragraph paragraph = new Paragraph("Testing the iText pdf.");
            Phrase phrase = new Phrase("This is a phrase but testing some formatting also. \nNew line here.");
            Chunk chunk = new Chunk("This is a chunk.");
    
            doc.Add(header);
            doc.Add(paragraph);
            doc.Add(phrase);
            doc.Add(chunk);
    
            doc.Close();
            return output.ToArray();
        }
    
    }
    
    private void ShowPdf(byte[] strS)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
    
        Response.BinaryWrite(strS);
        Response.End();
        Response.Flush();
        Response.Clear();
    }
    

提交回复
热议问题