- 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 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);
}
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);
}
}
}
}
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;
}
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);
}
}