Create PDF in memory instead of physical file

后端 未结 4 1858
说谎
说谎 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条回答
  •  Happy的楠姐
    2020-11-27 04:40

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
    
    byte[] pdfBytes;
    using(var mem = new MemoryStream())
    {
        using(PdfWriter wri = PdfWriter.GetInstance(doc, mem)) 
        {
            doc.Open();//Open Document to write
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");
    
            doc.Add(paragraph);
    
            doc.Add(pharse);
    
            doc.Add(chunk); 
        }
        pdfBytes = mem.ToArray();
    }
    

提交回复
热议问题