Retrieve pdf files from sql

Retrieve pdf files from sql

  • 611 Views
  • Last Post 2 weeks ago
  • Topic Is Solved
Stylus STYLUS posted this 3 weeks ago

Insert into

byte[] filedata = null;
MemoryStream ms = new MemoryStream();
filedata = ms.GetBuffer();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;

I need to retrieve PDF files from sql database

I try this

try
{
     if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)
     {
         byte[] ap = (byte[])(documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value);
         MemoryStream ms = new MemoryStream(ap);
         axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
         //axAcroPDF1.LoadFile(ms);
     }
     else
     {
         axAcroPDF1.src = null;
     }
}
catch
{
     axAcroPDF1.src = null;
}
Order By: Standard | Newest | Votes
lucy posted this 3 weeks ago

To read pdf file from stream in c#. I think you should save pdf to file, then read it. Exactly what we are going to do is convert from a byte array to a pdf file.

byte[] ap = (byte[])(documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value);
MemoryStream ms = new MemoryStream(ap);
ms.Position = 0;
File.WriteAllBytes(@"D:\mypdf.pdf", ms.ToArray());
Process.Start(@"D:\mypdf.pdf");

You can't read pdf directly from memory stream, but you can read it from file. I hope so you can solve your problem

Stylus STYLUS posted this 3 weeks ago

c# windows form application

lucy posted this 3 weeks ago

I think you should use databinding to retrieve the current row from your DataGridView. I've created a PdfFile table, then use the Entity Framework to save and fetch data from PdfFile table.

CREATE TABLE [dbo].[PdfFile](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Pdf] [image] NULL,
 CONSTRAINT [PK_PdfFile] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Add code to handle read and write your pdf file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UIDesign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog() { Filter = "PDF|*.pdf" };
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = File.OpenRead(ofd.FileName);
                MemoryStream ms = new MemoryStream();
                fs.CopyTo(ms);
                DbEntities db = new DbEntities();
                db.PdfFiles.Add(new PdfFile() { Pdf = ms.ToArray() });
                db.SaveChanges();
                MessageBox.Show("OK");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DbEntities db = new DbEntities();
            PdfFile pdf = db.PdfFiles.FirstOrDefault();
            MemoryStream ms = new MemoryStream(pdf.Pdf);
            ms.Position = 0;
            File.WriteAllBytes(@"D:\testpdf.pdf", ms.ToArray());
            Process.Start(@"D:\testpdf.pdf");
        }
    }
}
  • Liked by
  • Stylus STYLUS
Stylus STYLUS posted this 3 weeks ago

I try now. 10 minutes

  • Liked by
  • lucy
Stylus STYLUS posted this 3 weeks ago

Solved. Working. Thank you. Just litle help. I want to open pdf in axAcroPDF1   in windows form

lucy posted this 3 weeks ago

Please try

axAcroPDF1.LoadFile(@"D:\testpdf.pdf");
axAcroPDF1.Enabled = true;

Instead use

Process.Start(@"D:\testpdf.pdf");
  • Liked by
  • Stylus STYLUS
Stylus STYLUS posted this 3 weeks ago

Work. Thank you Lucy

Respect

Stylus STYLUS posted this 3 weeks ago

In my app think I have error in INSERT INTO db statement pdf document

Statement work but some is error when  read from db

byte[] filedata = null;
MemoryStream ms = new MemoryStream();
filedata = ms.GetBuffer();
axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());

INSERT into dbo.documents (pdf_file) VALUES(@pdf_file)

querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;

Retriieve from db use this

try
{
    if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)
    {
        byte[] ap = (byte[])(documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value);
        MemoryStream ms = new MemoryStream(ap);
        ms.Position = 0;
        File.WriteAllBytes(@"C:\PDFs.pdf", ms.ToArray());
        axAcroPDF1.LoadFile(@"C:\PDFs.pdf");
        axAcroPDF1.Enabled = true;
    }
    else
    {
        axAcroPDF1.src = null;
    }
}
catch
{
    axAcroPDF1.src = null;
}

But I have some error...Not understand

The folowing exception occured in the DataGridView

System.ArgumentException: Parameter not valid. at system.Drawing.Image.FromStream(Stream stream, Boolean validateImageData)

That is short error text. But why Drawing image when I insert PDF in database and I wan to retrieve pdf in axAcroPDF1

lucy posted this 3 weeks ago

You should add a bindingsource to the DataGridView, then remove the pdf data column. You got the error because you added the pdf_file column to your DataGridView. Your DataGridView tries to convert binary to image, but the current data is a pdf file, so the application has thrown the error.

Stylus STYLUS posted this 3 weeks ago

c# winforms

Stylus STYLUS posted this 3 weeks ago

I remove pdf file column from datagrid, documenstbindingsource   have....

I use data  DocumentsDataGridView_SelectionChanged

if (documentsDataGridView.SelectedRows.Count > 0)
{
    string id = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn1"].Value.ToString();
    string number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn2"].Value.ToString();
    string count_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn3"].Value.ToString();
    string label = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn4"].Value.ToString();
    string partner = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn5"].Value.ToString();
    string tax_number = documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn6"].Value.ToString();
    string file_location = documentsDataGridView.SelectedRows[0].Cells["file_location"].Value.ToString();
    idTextBox.Text = id;
    numberTextBox.Text = number;
    count_numberTextBox.Text = count_number;
    labelTextBox.Text = label;
    partnerComboBox.Text = partner;
    tax_numberTextBox.Text = tax_number;
    dateDateTimePicker.Value = Convert.ToDateTime(documentsDataGridView.SelectedRows[0].Cells["dataGridViewTextBoxColumn7"].Value);
    file_locationTextBox.Text = file_location;
}

and for pdf this

I think error is here...i dont se pdf focument in axAcroPDF1 when click on datagrid row

try
{
    if (documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value != null)
    {
        byte[] ap = (byte[])(documentsDataGridView.SelectedRows[0].Cells["pdf_file"].Value);
        MemoryStream ms = new MemoryStream(ap);
        ms.Position = 0;
        File.WriteAllBytes(@"C:\PDFs.pdf", ms.ToArray());
        axAcroPDF1.LoadFile(@"C:\PDFs.pdf");
        axAcroPDF1.Enabled = true;
    }
    else
    {
        axAcroPDF1.src = null;
    }
}
catch
{
    axAcroPDF1.src = null;
}

some help please

lucy posted this 3 weeks ago

Have you removed the pdf column?

Stylus STYLUS posted this 3 weeks ago

c# winforms

Stylus STYLUS posted this 3 weeks ago

Removed from datagrid

Stylus STYLUS posted this 3 weeks ago

Sql statement

c# sql database

Stylus STYLUS posted this 3 weeks ago

Sql values in table documents

sql database

lucy posted this 3 weeks ago

Do not add any binary columns in your DataGridView

Show More Posts
Close