Read a stored PDF from memory stream

断了今生、忘了曾经 提交于 2019-12-01 20:13:25

问题


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?
                }
            }

回答1:


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);
    }


来源:https://stackoverflow.com/questions/29030723/read-a-stored-pdf-from-memory-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!