Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
PDF is a binary format. Before putting it in a database, we need to create a BLOB as temporary store. Fortunately, PDFOne supports loading and saving PDF documents from and to memory streams and byte arrays.
In this article, we will see how to create a simple database-based PDF document viewer using byte arrays. We will use the PDFDocument
and PDFViewer
components for this.
using Gnostice.PDFOne; using System.Data.OleDb;
PDFDocument
component as a class variable.
PDFDocument doc;
InitializeComponent(); doc = new PDFDocument("your-license-key"); openFileDialog1.Title = "Select a PDF file"; openFileDialog1.FileName = ""; openFileDialog1.Multiselect = false; openFileDialog1.Filter = "PDF files|*.pdf|All files|*.*";
byte[] getFileBytes(String sFileToBeRead) { byte[] ba1; System.IO.FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open); lFileSize = fs.Length; System.IO.BinaryReader br = new System.IO.BinaryReader(fs); ba1 = br.ReadBytes((Int32) lFileSize); br.Close(); fs.Close(); return (ba1); }
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { byte[] ba2; ba2 = getFileBytes(openFileDialog1.FileName); DataRow dr = ((DataRowView)bindingNavigator1.BindingSource.Current).Row; dr["doc_name"] = openFileDialog1.SafeFileName; dr["doc_binary"] = ba2; toolStripLabel1.Text = openFileDialog1.SafeFileName; pdfViewer1.LoadDocument(ba2); } }This will help the end-user to select a PDF file. The PDF file is then stored in a new record in the "docs" table and also displayed on the viewer.
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) { byte[] ba2; if (bindingNavigator1.BindingSource == null) { return; } if (bindingNavigator1.BindingSource.Current == null) { return; } DataRow dr = ((DataRowView)bindingNavigator1.BindingSource.Current).Row; if (dr.IsNull(2)) { return; } if (bindingNavigator1.BindingSource.Count > 0) { toolStripLabel1.Text = (string)dr["doc_name"]; ba2 = (byte[])dr["doc_binary"]; pdfViewer1.LoadDocument(ba2); } }Here we load the PDF document from the current record as a byte array and display it in the viewer.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { bindingSource1.EndEdit(); if (this._doc_storeDataSet.docs != null) { docsTableAdapter.Update(this._doc_storeDataSet.docs); } }This will ensure that changes to the database are saved at the end.
if (bindingNavigator1.BindingSource.Count > 0) { DataRowView drv = (DataRowView)bindingNavigator1.BindingSource.Current; toolStripLabel1.Text = drv["doc_name"].ToString(); }
In this article, you learned how to save and retrieve PDF documents to and from a database. You also learned how to browse documents from a database and display them in a PDF viewer component.
Modifying PDF documents in the database would be just as easy as displaying it in the viewer. Load the same byte array in a PDFDocument
instance, make changes to it using PDFDocument
methods, make the PDFDocument
instance save the modified document back to the byte array, and finally set the updated byte array to the doc_binary field of the current record.
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |