Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a stored PDF from memory stream

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?
                }
            }
like image 880
Hunar Avatar asked Mar 13 '15 11:03

Hunar


People also ask

Can a PDF be streamed?

Well, PDF streaming is in fact possible on virtually any device using linearized PDFs and PDFTron SDK's Open URL method -- including an option in iOS , Android , Xamarin , and UWP to restrict downloads to only those pages displayed in the viewer.

How do I view PDF data?

Find the PDF you want to open in your Files and double click to open. Select Adobe Acrobat (or whichever reader you downloaded) from the list of available options. If no list appears or the page opens in another application, you can right-click the file and select Open With to choose your PDF reader. Click Open.

What are PDF content streams?

A page in a PDF document has one or more content stream parts that together contain all the PDF page description commands for the page. So content streams are essentially the contents of the pages - the text and any line drawings.

Can any device open a PDF?

To open a PDF file on a Windows computer, you'll need to download Adobe Reader from get.adobe.com/reader.


1 Answers

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);
    }
like image 116
Morphed Avatar answered Oct 19 '22 10:10

Morphed