Read a stored PDF from memory stream

21,121

This answer should give you some options: How to render pdfs using C#


In the past I have used Googles open source PDF rendering project - PDFium

There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.

It works directly with Streams so doesn't require any data to be written to disk

This is my example from a WinForms app

    public void LoadPdf(byte[] pdfBytes)
    {
        var stream = new MemoryStream(pdfBytes);
        LoadPdf(stream)
    }

    public void LoadPdf(Stream stream)
    {
        // Create PDF Document
        var pdfDocument = PdfDocument.Load(stream);

        // Load PDF Document into WinForms Control
        pdfRenderer.Load(_pdfDocument);
    }
Share:
21,121
Hunar
Author by

Hunar

Intermediate expertise in Machine Learning, Deep Learning, Medical Image Analysis, Software Development, and Web Applications.

Updated on July 09, 2020

Comments

  • Hunar
    Hunar almost 4 years

    I'm working on a database project using C# and SQLServer 2012. In one of my forms I have a PDF file with some other information that is stored in a table. This is working successfully, but when I want to retrieve the stored information I have a problem with displaying the PDF file, because I can't display it and I don't know how to display it.

    I read some articles that said it can not be displayed with Adobe PDF viewer from a memory stream, is there any way to that?

    This is my code for retrieving the data from the database:

                sql_com.CommandText = "select * from incoming_boks_tbl where [incoming_bok_id]=@incoming_id and [incoming_date]=@incoming_date";
                sql_com.Parameters.AddWithValue("incoming_id",up_inco_num_txt.Text);
                sql_com.Parameters.AddWithValue("incoming_date", up_inco_date_txt.Text);
                sql_dr = sql_com.ExecuteReader();
                if(sql_dr.HasRows)
                {
                    while(sql_dr.Read())
                    {
                        up_incoming_id_txt.Text = sql_dr[0].ToString();
                        up_inco_num_txt.Text = sql_dr[1].ToString();
                        up_inco_date_txt.Text = sql_dr[2].ToString();
                        up_inco_reg_txt.Text = sql_dr[3].ToString();
                        up_inco_place_txt.Text = sql_dr[4].ToString();
                        up_in_out_txt.Text = sql_dr[5].ToString();
                        up_subj_txt.Text = sql_dr[6].ToString();
                        up_note_txt.Text = sql_dr[7].ToString();
                        string file_ext = sql_dr[8].ToString();//pdf file extension
                        byte[] inco_file = (byte[])(sql_dr[9]);//the pdf file
                        MemoryStream ms = new MemoryStream(inco_file);
                        //here I don't know what to do with memory stream file data and where to store it. How can i display it?
                    }
                }