We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to store and retrieve a PDF document from database

Platform: WinForms |
Control: PDF |
Published Date: April 30, 2011 |
Last Revised Date: May 7, 2019
Tags: pdf, database

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can store and retrieve the PDF document from data using C# and VB.NET.

Steps to store and retrieve PDF document programmatically:

  1. Create a new C# Windows Forms application project. Create a Windows Forms application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespaces in the Form1.Designer.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Data.OleDb;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Data.OleDb
Imports System.Drawing

 

  1. Use the following code snippet to store PDF document to database.

C#

//Store the PDF document to database
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a new PDF page
PdfPage page = doc.Pages.Add();
//Draw the string
page.Graphics.DrawString("Hello World!", new PdfStandardFont(PdfFontFamily.Helvetica, 15, PdfFontStyle.Bold), PdfBrushes.Red, PointF.Empty);
//Save the PDF document
MemoryStream ms = new MemoryStream();
doc.Save(ms);
//Close the document
doc.Close(true);
string dbFile = "database.mdb";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile;
//Create a new OleDbConnection
OleDbConnection connection = new OleDbConnection(connectionString);
//Open
connection.Open();
string commandText = "insert into Table1 (FileName, FileContent) values('helloworld', @PDFFile)";
//Create an OleDbCommand
OleDbCommand command = new OleDbCommand(commandText, connection);
//Store the PDF stream to database
command.Parameters.AddWithValue("PDFFile", ms.ToArray());
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("PDF successfully save to DataBase");

 

VB.NET

'Store the PDF document to database
'Create a new PDF document
Dim doc As New PdfDocument()
'Add a new PDF page
Dim page As PdfPage = doc.Pages.Add()
'Draw the string
page.Graphics.DrawString("Hello World!..", New PdfStandardFont(PdfFontFamily.Helvetica, 15, PdfFontStyle.Bold), PdfBrushes.Red, PointF.Empty)
'Save the PDF document
Dim ms As New MemoryStream()
doc.Save(ms)
'Close the document
doc.Close(True)
Dim dbFile As String = "database.mdb"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile
'Create a new OleDbConnection
Dim connection As New OleDbConnection(connectionString)
'open
connection.Open()
Dim commandText As String = "insert into Table1 (FileName, FileContent) values('helloworld', @PDFFile)"
'Create an OleDbCommand
Dim command As New OleDbCommand(commandText, connection)
'Store the PDF stream to database
command.Parameters.AddWithValue("PDFFile", ms.ToArray())
command.ExecuteNonQuery()
connection.Close()
MessageBox.Show("PDF successfully save to DataBase")

 

  1. Use the following code snippet to retrieve the PDF document from database.

C#

//Retrieve and display the stream in PDF format
string dbFile = "database.mdb";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile;
//Create a new OleDbConnection
OleDbConnection connection = new OleDbConnection(connectionString);
//Open
connection.Open();
string commandText = "select * from Table1";
//Create a oleDbCommand
OleDbCommand command = new OleDbCommand(commandText, connection);
//Create the data adapter
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
//Create a new data table
DataTable dataTable = new DataTable();
//Fill the data table
adapter.Fill(dataTable);
//Retrieve the PDF file from the data table
byte[] buffer = (byte[])dataTable.Rows[0]["FileContent"];
//Save the PDF file
using (FileStream fstream = new FileStream("Helloworld.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    fstream.Write(buffer, 0, buffer.Length);
}
//This will open the PDF file so, the result will be seen in default PDF viewer
System.Diagnostics.Process.Start("Helloworld.pdf");

 

VB.NET

'Retrieve and display the stream in PDFformat
Dim dbFile As String = "database.mdb"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile
'Create a new OleDbConnection
Dim connection As New OleDbConnection(connectionString)
'Open
connection.Open()
Dim commandText As String = "select * from Table1"
'Create a oleDbCommand
Dim command As New OleDbCommand(commandText, connection)
'Create the data adapter
Dim adapter As New OleDbDataAdapter(command)
'Create a new data table
Dim dataTable As New DataTable()
'Fill the data table
adapter.Fill(dataTable)
'Retrieve the PDF file from the data table
Dim buffer As Byte() = DirectCast(dataTable.Rows(0)("FileContent"), Byte())
'Save the PDF file
Using fstream As New FileStream("Helloworld.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
    fstream.Write(buffer, 0, buffer.Length)
End Using
‘This will open the PDF file so, the result will be seen in default PDF viewer
System.Diagnostics.Process.Start("Helloworld.pdf")

 

A complete working sample can be downloaded from PDFDatabaseSample.zip.

By executing the program, you will get the PDF document as follows. Screenshot of output PDF document

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
tomer
Dec 23, 2016
1.Link to demo is broken
2.code for saving the pdf is missing
2.The type of column in the db table is not specified

Reply
Jordan Capa
Apr 23, 2019

thanks for the information! for the beginners and want to save time, I found zetpdf.com i heard a lot of this. I suggest it because it's the fastest pdf SKD for .NET applications.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon