Articles in this section
Category / Section

How to store and retrieve a PDF document from database

4 mins read

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied