How to Store and Retrieve WinForms PDF Document from Database?
Syncfusion Essential WinForms 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:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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")
- 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.
Refer here to explore the rich set of Syncfusion Essential PDF features.
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.
Conclusion
I hope you enjoyed learning about how to store and retrieve WinForms PDF document from database.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!