Sample: Processing PDF files in a database using C#

The PDF Creator .NET Library enables you to create secure PDF documents on the fly and to view, print and process existing PDF documents. If you have any questions about PDF Creator .Net, please post them here.
Post Reply
Devteam
Posts: 115
Joined: Fri Oct 14 2005
Location: Montreal
Contact:

Sample: Processing PDF files in a database using C#

Post by Devteam » Sun Apr 10 2011

This is a commented sample that explains how to:
- Extract a PDF file from a database to a memory stream
- Reduce the PDF file size or apply some processing on the PDF file using C# and PDF Creator .NET
- Save the PDF stream back to the database

Step 1: Download Amyuni PDF Creator .NET from http://www.amyuni.com/en/developer/pdfcreator.

Step 2: Start a new Visual Studio project and add a reference to acPDFCreatorLib.net.dll
add reference.png
add reference.png (25.42 KiB) Viewed 5429 times
Reminder: acPDFCreatorLib.net.dll contains a set of .NET classes that constitute the core of PDF Creator .NET. The assembly acPDFCreator.net.dll contains a visual control that can be placed on a form to preview or visually interact with the PDF file. This latter assembly is not needed for this sample.

Step 3: Activate the PDF Creator library by setting the license key. This is done in the constructor of the main class or form

Code: Select all

    public partial class Form1 : Form
    {
        string Company = "MyCompanyName";
        string LicKey = "07EFCDA...176349";    // license key provided by Amyuni

        public Form1()
        {
            InitializeComponent();
            acPDFCreatorLib.SetLicenseKey(Company, LicKey);
        }
Step 4: Connect to the database and loop through all PDF files that need some processing. This can be done in the form load handler. In this sample, we are using an ODBC connection to a MySQL database, the same concept can be applied to any other database type.

Code: Select all

        private void Form1_Load(object sender, EventArgs e)
        {
            // The connection string assumes a connection to the "invoicing" ODBC data source (DSN)
            string connectionString = "DSN=invoicing;UID=;PWD=;";

            // Provide the query string, select all 2011 invoices
            string queryString = "SELECT * FROM invoices where invoicenum like '2011%'";

            // Create and open the connection in a using block
            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                // Connect to the database and create a DataReader to extract the binary blobs containing the invoices
                connection.Open();
                OdbcCommand cmd = new OdbcCommand(queryString, connection);
                OdbcDataReader reader = cmd.ExecuteReader();

                while( reader.Read() )
                {
                    try
                    {
                        Console.WriteLine("\t{0}", reader[0]);   // reader[0] contains the invoice number
                        
                        try
                        {
                            System.Int32 len1 = Convert.ToInt32(reader.GetBytes(1, 0, null, 0, 0));
                            if (len1 > 60000)   // optimize any PDF larger than 60k
                            {
                                // load the PDF data to a memory stream
                                byte[] blob1 = new byte[len1];
                                reader.GetBytes(1, 0, blob1, 0, len1);
                                System.IO.MemoryStream ms1 = new System.IO.MemoryStream(blob1, true);
                                // Optimize the PDF file and resave it to the DB only if the optimized file is smaller than the original
                                if (OptimizePDF(ms1) && (ms1.Position < len1))
                                {
                                    byte[] newpdf = new byte[(int)ms1.Position];
                                    ms1.Seek(0, System.IO.SeekOrigin.Begin);
                                    ms1.Read(newpdf, 0, newpdf.Length);
                                    UpdateInvoice(connection, (String)reader[0], newpdf, newpdf.Length);
                                    Console.WriteLine("\t\t*** Invoice Updated ***");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Step 5: Implement the optimization algorithm to your specifications. In this sample, we rasterize all pages to 150 DPI black and white images according to customer's specifications.

Code: Select all

        private bool OptimizePDF(System.IO.MemoryStream ms)
        {
            bool bRet = false;
            // instantiate the main PDF document object
            Amyuni.PDFCreator.IacDocument pdf = new Amyuni.PDFCreator.IacDocument();
            // Open from a memory stream
            if (pdf.Open(ms, ""))
            {
                // rasterize all pages to 150 DPI B&W
                pdf.RasterizePageRange(1, pdf.PageCount, 150, IacRasterizeColorOption.acColorOptionBW, IacImageCompressionConstants.acCompressionJPegLow);

                // resave to the memory stream
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                pdf.Save(ms, IacFileSaveOption.acFileSaveView);
                bRet = true;
            }
            pdf.Dispose();

            return bRet;
         }
Step 6: Resave the PDF from the stream to the database. The interesting part here is the use of a parameterized SQL statement to insert the binary blob using ODBC.

Code: Select all

        private void UpdateInvoice(OdbcConnection connection, String invoice, byte[] buffer, int length)
        {
            // Create an SQL statement with a single parameter. Note the syntax might differ depending on the type of connection to the database
            string queryString = "UPDATE invoices set invoice = ? where invoicenum = '" + invoice + "'";
            try
            {
                OdbcCommand cmd = new OdbcCommand(queryString, connection);
                // set the unique parameter to the binary data contained in the memory stream
                cmd.Parameters.Add("", OdbcType.VarBinary, length).Value = buffer;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 
Amyuni Development Team

Efficient and accurate conversion to PDF, XPS, PDF/A and more. Free trials at - http://www.amyuni.com

Post Reply