6

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?
                }
            }
MeanGreen
  • 3,098
  • 5
  • 37
  • 63
Hunar
  • 399
  • 3
  • 7
  • 27
  • 2
    Assuming WinForms as opposed to web: [write the MemoryStream to a file](http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file) and [start the system's default PDF viewer](http://stackoverflow.com/questions/6742720/how-to-open-or-launch-pdf-files-in-c-net). Edit: sure, convert my trivial answer to a trivial question to a comment. I'm not going to copy the existing answers. – CodeCaster Mar 13 '15 at 11:13

1 Answers1

10

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);
    }
Community
  • 1
  • 1
Morphed
  • 3,527
  • 2
  • 29
  • 55
  • thak you for your good answer, but can you give me an article how to add this package to visual studio 2010, it can be add like adding a reference? – Hunar Mar 13 '15 at 11:57
  • @honar.cs PDFium is available as a [Nuget package](http://www.nuget.org/packages/PdfiumViewer). The Nuget package manager can be installed into VS2010 following [these instructions](https://docs.nuget.org/consume/installing-nuget#visual-studio-2010,-2012-and-2013) – Morphed Mar 13 '15 at 14:04
  • dear friend thank, i think that i do what you said, see the the pic in this link http://postimg.org/image/h8e4p627d/, did you do install this two things? if yes, now how i use the PDFium where i found it?. thank you again – Hunar Mar 13 '15 at 17:41
  • and i want to display a pdf viewer in specific location on my form because it has some other components ? how to do that? – Hunar Mar 13 '15 at 18:41
  • 1
    @honar.cs you are best searching for and asking these questions separately. The comments section isn't the place to have extended discussions. Thanks – Morphed Mar 13 '15 at 19:26