Search code examples
c#asp.net.netitextmemorystream

How can i show a pdf on webbrowser using byte Array. C#


im trying to create a pdf file and im trying to return this file on imsonia or webbrowser, but i dont how to do this.

here is my code:

string beneficiarioRelatorio = "Test"

var stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
//Paragraph header = new Paragraph(beneficiarioRelatorio);

document.Add(new Paragraph(beneficiarioRelatorio));
document.Close();

//return stream.ToArray();
//System.IO.File.WriteAllBytes("hello.pdf", stream.ToArray());
var teste = new FileStream(stream, FileMode.Open); 
return new FileStreamResult(teste,"application/pdf");

Solution

  • try it

            [HttpGet(Name = "GetPDF")]
        public ActionResult  GetPDF()
        {
            string beneficiarioRelatorio = "Test";
    
            MemoryStream ms = new MemoryStream();
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            doc.Open();
            doc.Add(new Paragraph(beneficiarioRelatorio));
            doc.Close();
            byte[] bytes = ms.ToArray();
    
            return File(bytes, "application/pdf");
        }