Having already achieved automatized MsOffice-independent Docx report generation in a client-server architecture following the approach explained in my previous article “A SOA approach to dynamic DOCX-PDF report generation – part 1”, now we’ll look into automatically printing those docx files into PDF from managed code and transmitting the PDF bytes through HTTP.
The PDF conversion is based on a free BullZip PDF product, which offers a free, full-featured, programmable and very well documented PDF printer that can print any file to PDF, including Docx files.
Needless to say that PDF is probably the most used document exchange format between different platforms, therefore the need to have PDF reports of some kind of data is common to most data-centric applications.
1. Installing the PDF Printer
The first thing to do is to download and install BullZipPdf. It will create a PDF printer in the system and it will include the help file in the installation directory. Read through the help file to learn how to use the
2. Adding the PDF Conversion to an Existing Visual Studio Solution
First of all, we need to import the package into the solution. As sweet as it can be, we can find the package in the GAC, so just go on Add Reference -> .NET and find BullZip Pdf Writer. This will add the
Bullzip.PDFWriter assembly to the solution, which exposes its classes and methods under the
Bullzip.PDFWriter namespace. The next thing to do is configuring the PDF printer. This can be achieved through a .ini file, but I’m not going to enter into this, you can read a lot about it in the
Bullzip documentation. The printer settings are managed by a class called PdfSettings, whilst the PDF creation methods are in a class called PdfUtils. Everything is ready now, we can already start converting to PDF!
3. Converting to PDF
Here’s what the test application does:
- It includes some docx templates with sample data in a templates directory
- Generates customized docx reports based on the docx templates and some XML-serialized Business-Logic data whose structure corresponds to the custom XML parts in the docx templates
- Saves the docx reports into a temporary directory
- Prints the docx reports into PDF
- Sends the PDF bytes through HTTP
- Destroys the docx and PDF files
PrintToPdf method loads the printer settings from an “.ini” file, it “reads” a docx file from a temporary directory, creates the PDF file and then destroys the original docx and PDF.
public class PdfMaker
internal static byte PrintToPdf(string appFolder, string tempDocxFileName)
string tempFolder = appFolder + @"\temp";
string tempDocxFilePath = tempFolder + @"\" + tempDocxFileName;
PdfSettings pdfSettings = new PdfSettings();
pdfSettings.PrinterName = ConfigurationManager.AppSettings["PdfPrinter"];
string settingsFile = pdfSettings.GetSettingsFilePath(PdfSettingsFileType.Settings);
pdfSettings.LoadSettings(appFolder + @"\App_Data\printerSettings.ini");
pdfSettings.SetValue("Output", tempFolder + @"\<docname>.pdf");
string tempPdfFilePath = tempFolder + @"\Microsoft Word - " + tempDocxFileName + ".pdf";
bool fileCreated = false;
fileCreated = PdfUtil.WaitForFile(tempPdfFilePath, 1000);
byte pdfBytes = File.ReadAllBytes(tempPdfFilePath);
catch (Exception ex)
throw new FaultException("WCF ERROR!\r\n" + ex.Message);
Points of Interest
The scope of this article is limited to a mere illustration of what can be achieved through this architecture. With a little bit of head-scratching, you can extend this and make it into a PDF conversion server (did anyone think of a free version Adobe Distiller ???), a scheduled batch printer, an archiving system, etc.
If integrated in the SOA report generation solution mentioned above this permits you to get rid of the docx files and use PDF as the document exchange format.
The previous (must-read to understand the SOA integration concepts) article that brought to this: “A SOA approach to dynamic DOCX-PDF report generation – part 1”
Click here to view this article on CodeProject.
Click here to download the test application’s source code.