Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i use Visual Studio 2017 and it's possible to load PDF to databse and then view this PDF?
I use AcroPDFLib.

I opened pdf in AcroPDF and then i try to save this file to correctly row in database table... but it's not working.

What I have tried:

All what i know :(

I tried:
- connect axAcrobatPDF to table row...
- convert PDF to image and then save image to database and then convert image to pdf and open new pdf in axAcrobatPDF
Posted
Updated 28-Mar-18 7:30am
Comments
Richard MacCutchan 28-Mar-18 9:41am    
Why are you converting between PDF and Image? Just save the PDF as is.
[no name] 28-Mar-18 10:02am    
Because it's easier to save image and then view image in PictureBox from database than save and view PDF in axAcrobatPDF...

This method and directly save pdf to databse not working.
[no name] 28-Mar-18 10:31am    
Now i have program like this:

SqlConnection connection = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = ...\\Database1.mdf; Integrated Security = True");
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into [Table] (Id,cos,plik) values ('" + idTextBox.Text + "','" + cosTextBox.Text + "','" + axAcroPDF1.src + "')";
cmd.ExecuteNonQuery();
connection.Close();
idTextBox.Text = "";
cosTextBox.Text = "";
axAcroPDF1.src = "";
display_data();
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void display_data()
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [Table]";
cmd.ExecuteNonQuery();
DataTable dta = new DataTable();
SqlDataAdapter dataadp = new SqlDataAdapter(cmd);
dataadp.Fill(dta);
tableDataGridView.DataSource = dta;
connection.Close();
}

string imgLocation = "";
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All files(*.*)|*.*|pdf files(*.pdf)|*.pdf|jpg files(*.jpg)|*.pdf|png files(*.png)|*.png";
if (dialog.ShowDialog() == DialogResult.OK)
{
imgLocation = dialog.FileName.ToString();
axAcroPDF1.src = dialog.FileName.ToString();
}
}
}

And when i tried to add to database i got error:

ERROR
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

1 solution

See the answer here: Save and view pdf file from SQL server database in c# WinForms[^]

The code in the example seems to have some garbage in it though, like:
<strong class="highlight">

It should look like this I think:

Imports System.Data.SqlClient

'IF OBJECT_ID('PDF', 'U') IS NOT NULL DROP TABLE PDF
'CREATE TABLE PDF
'(
' RecordId int identity(1000, 1) PRIMARY KEY,
' PDF varbinary(max)
')

Public Class frmPDF

Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
Using conn As New SqlConnection("Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Insert Into PDF (PDF) Values (@PDF)", conn)
cmd.Parameters.Add(New SqlParameter("@PDF", SqlDbType.VarBinary)).Value = System.IO.File.ReadAllBytes("C:\file.pdf")
cmd.ExecuteNonQuery()
End Using
conn.Close()
End Using
End Sub

Private Sub ButtonDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownload.Click
Dim sFilePath As String
Dim buffer As Byte()
Using conn As New SqlConnection("Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Select Top 1 PDF From PDF", conn)
buffer = cmd.ExecuteScalar()
End Using
conn.Close()
End Using
sFilePath = System.IO.Path.GetTempFileName()
System.IO.File.Move(sFilePath, System.IO.Path.ChangeExtension(sFilePath, ".pdf"))
sFilePath = System.IO.Path.ChangeExtension(sFilePath, ".pdf")
System.IO.File.WriteAllBytes(sFilePath, buffer)
Dim act As Action(Of String) = New Action(Of String)(AddressOf OpenPDFFile)
act.BeginInvoke(sFilePath, Nothing, Nothing)
End Sub

Private Shared Sub OpenPDFFile(ByVal sFilePath)
Using p As New System.Diagnostics.Process
p.StartInfo = New System.Diagnostics.ProcessStartInfo(sFilePath)
p.Start()
p.WaitForExit()
Try
System.IO.File.Delete(sFilePath)
Catch
End Try
End Using
End Sub
End Class
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900