+ 2

How to upload and view PDF file to and from MySQL database directly on windows desktop application?

I've managed to convert the PDF file to array of byte and stored this byte to database. my problem is that I couldn't figure out how to view it on the form whenever a user click on any row on the datagridview!!

10th Feb 2018, 2:35 PM
Doyo
Doyo - avatar
13 Answers
+ 2
First of all you need a control that shows the PDF. Then you need to be sure that the method you use to put the pdf in the database does not alter the PDF. Last you need to reconvert the PDF from your database to its original format. 1 ) There are c# controls that show PDF. Like PDF Sharp, Sharp PDF and iTextSharp are excellent. They are all OpenSource. Didn't use it my self but google it. https://stackoverflow.com/questions/2516262/c-sharp-pdf-control-library 2 and 3) I have to less information to know where the method you use does affect the PDF and how to reconvert. Pleasse elaborate more Start with this code using a button when it works you execute the code when the user clicks on a row. Succes
10th Feb 2018, 9:10 PM
sneeze
sneeze - avatar
+ 2
Thanks. I installed the adobe PDF component. I like the idea of being able to show PDF in C# applications. I can help a lot with just common programming sense without knowledge of specific components. I do predict that the problem is not in the component but in saving and retrieving the pdf from the database. I think the datatype should be VARBINARY[MAX] instead of int. But I have been able to test it yet. Time to sleep now To be continued
14th Feb 2018, 10:47 PM
sneeze
sneeze - avatar
+ 1
OK, check this https://stackoverflow.com/questions/29030723/read-a-stored-pdf-from-memory-stream is the same problem as mine. I just want to render my PDF file on axAcroPDF from my database. I did this axAcroPDF.src = byteData and doesn't work
13th Feb 2018, 9:31 PM
Doyo
Doyo - avatar
+ 1
Please give more information, based on what you have written I cannot predict what your problem is. Take small steps. Can you open a document in a stream Assign this to your axAcroPDF and show it. This is gone tell you whether the problem is in assigning the source to the control or in saving and retrieving the data in the database
13th Feb 2018, 10:24 PM
sneeze
sneeze - avatar
+ 1
private void savePDF_Click(object sender, EventArgs e) { try { sqlConnection = new SqlConnection(strCon); sqlConnection.Open(); using (SqlCommand sqlCmd = new SqlCommand("INSERT INTO book (pdf,id) values (@pf,2)", sqlConnection)) { sqlCmd.Parameters.Add("@pf", bt); sqlCmd.ExecuteNonQuery(); } MessageBox.Show("Book Added successfully!"); bt = null; } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { sqlConnection.Close(); } } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { string id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[0].Value); try { sqlConnection = new SqlConnection(strCon); sqlConnection.Open(); SqlCommand cmd = new SqlCommand("select pdf from book where id = "+ int.Parse(id), sqlConnection); //cmd.Parameters.Add("@pf", id); using(SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.Default)) { if (dr.Read()) { bt = null; bt = (byte[])dr.GetValue(0); axAcroPDF1.src = bt; //error } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { sqlConnection.Close(); } }
13th Feb 2018, 10:46 PM
Doyo
Doyo - avatar
+ 1
//for loading PDF file to the control byte[] bt; private void Browse_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { ValidateNames = true, Multiselect = false, Filter = "PDF|*.pdf" }) { if(ofd.ShowDialog() == DialogResult.OK) { bt = null; FileStream fs = File.OpenRead(ofd.FileName); bt = new byte[fs.Length]; fs.Read(bt, 0, (int)fs.Length); axAcroPDF1.src = ofd.FileName; fs.Close(); } } }
13th Feb 2018, 10:52 PM
Doyo
Doyo - avatar
+ 1
on load, assume the table has only one column
13th Feb 2018, 10:59 PM
Doyo
Doyo - avatar
+ 1
Thank you. Now I can try to find out what the problem is. What is the type of the pdf field in the table ? Where can I get acAcropdf ? Is it free ?
13th Feb 2018, 11:05 PM
sneeze
sneeze - avatar
+ 1
Please start you own thread. Take small steps. Test every step. 1) can you create a pdf as file and read this file 2) can you save a valid pdf in a database 3) can you combine step 1 and 2 Please elaborate more by showing some of your code. Ask specific questions.
7th Sep 2020, 7:48 PM
sneeze
sneeze - avatar
0
int
13th Feb 2018, 11:08 PM
Doyo
Doyo - avatar
0
lol how can u help me if u don't know about acAcropdf? any I have found solution for my problem. however this is d step 1. install adobe PDF 2. go to visual and click on Toolbox and right click and choose Choose items 3. choose toolbox items windows will pop out and go to COM components and select adobe PDF reader and click OK 4. then it will show up on the tool list and you can drag and drop on ur form
14th Feb 2018, 1:50 PM
Doyo
Doyo - avatar
0
Thanks good I've solved d problem. i know Int is for id
15th Feb 2018, 9:23 AM
Doyo
Doyo - avatar
0
How are you doing? I see that I'm not the only one who needs to do that and it gets complicated, lol. I am a beginner. I started doing a program without prior knowledge and I'm doing quite well, but this has been driving me crazy for months. Can you explain to me with simple vocabulary how I should do to save the PDF in my MySql database? I did it but I'm sure I did it wrong. The format I gave to the column where the PDF is stored in the database is LONGBLOB, but surely I have to do something else (like encode the file before saving it and decode it before opening it in the AxacroPDF. The way I did it) I save it (I think so), and when I want to open it directly in the AxacroPDF it seems that it wants to start loading but the progress bar is stopped at the beginning. Also, when I enter a form where there is a Datagrid View with the content from the database (I excluded the PDF column in the Datagrid View) it also generates an error, for that reason I also think that I am also failing to save. I hope you can help me. THANK YOU MILLIONS !!!
6th Sep 2020, 10:31 PM
Fernando