On 28 August, 2013 13:52:44,
by Dirk Strauss
DevExpress Document Server – A couple of years ago (long, long ago in a galaxy far, far away) I had to take over a project from a developer that had left the company I worked for. Unfortunately, she left in the middle of an implementation. The application was developed and installed, but there were many change requests and I had to develop these change requests into working solutions.
One of the biggest headaches was the fact that the application did some really funky Office Word manipulation. So much so that when the company upgraded to a newer version of Office, it broke many of the installed application features. More recently though we needed to give a customer the ability to export data to Excel. The only catch was that the customer wasn’t going to install Microsoft Office.
We could obviously not use Microsoft Office automation and DevExpress Document Server is a perfect solution for times like these.
DexExpress Document Server – Spreadsheets
DexExpress Document Server is a .Net Library that you can add in to your C#, VB.Net or ASP.Net applications to automate Excel and Word without the need to use Microsoft Office automation. Adding this functionality is really very simple, and I’ll show you how to do this below. After installing DevExpress fire up Visual Studio. Create a Console Application and add the following references.
Add the following namespace to your application.
Importing the above namespace will expose the Workbook class to your application.
Anyone familliar with Excel knows that a workbook can contain many work sheets, and that a work sheet can contain multiple cells.
You can reference a worksheet by it’s name or by index. For this example we will just use it’s index.
To set a specific cell to a certain value, just reference it by it’s location. Therefore, if we want to set the text of cell A1, the code above will do that for you.
The last two lines you add will save the spreadsheet and start Excel. Save your application and run your console app.
Excel starts up in all its glory and you will see the text you set cell A1 to displayed in your spreadsheet. You can also specify other data types such as dates, boolean and numeric values. You can also set the width, alignment, border style and colour, background colour and font in a cell.
Lastly, head on over to the C:\Temp output location you set in code for your spreadsheet. There you will see the generated spreadsheet.
DevExpress Document Server – Rich Document To PDF
One of the requirements of the project I mentioned above was to create documents in PDF format. This was so that these documents could not be easily edited. If I think back at how we had to hack the code to create a PDF, I am amazed at how easy DevExpress Document Server makes this process now. To do this, follow these steps below.
Start by adding the Word document to convert to your console application. Be sure to set the property ‘Copy to Output Directory’ to ‘Copy always’. Next we need to add the following references to the Console Application (If you are continuing the previous example, just add the DevExpress.RichEdit.Core reference).
To be able to work with rich documents, add the required using statement DevExpress.XtraRichEdit.
As a side note, you will notice that in my code examples, the added using is light grey. This is because DevExpress CodeRush is installed and is indicating redundant namespaces to me. The DevExpress.XtraRichEdit namespace is redundant because it isn’t utilized anywhere in code yet.
Hover your mouse over the redundant namespace, and CodeRush will pop up an information dialog. I am so in love with CodeRush, but let me not digress.
Add the following code above to your console app. The code is self explanitory, but for completedness sake this is what the code is doing. Adding the DevExpress.XtraRichEdit namespace exposed the RichEditDocumentServer class. Load the Word document and create a stream object to create a file called ‘springcarnival.pdf’. Export the loaded Word document to PDF using the stream object. Lastly, run Process.Start which will start the file with the associated Adobe Reader application.
Your Word document is converted perfectly to a PDF file and opened in Adobe Reader. It couldn’t be any easier.
DevExpress Document Server – Compression And Archive Generation
Several years ago I had to work on an application that generated documents from an MRP run. These documents would obviously be quite large and numerous. One of the requirement was to compress them and process these files appropriately. I wish I had DevExpress Document Server back then. Let’s use the same console application and add the following reference.
Seeing as we have already added this reference above, we obviously don’t need to add it again.
Becore we can start using the compression classes, we need to add the DevExpress.Compression namespace to our console application. You will notice that the DevExpress.XtraRichEdit namespace is not grey anymore. This is because it was referenced in our code.
I have a folder with images of Endeavour’s launch into space. Let’s use that to illustrate the DevExpress Document Server compression functionality.
With the code sample above, we can see that DevExpress Document Server exposes several encryption types for us. For this example I’ll just use PkZip.
The rest of the code creates the archive and sets a password for the archive. After creating the archive, it saves it to the output directory.
Opening the archive and clicking on a file will prompt you for the password.
Because the files are encrypted, you won’t even see a thumbnail if you change the folder view.
This shows how easily we can create an encrypted archive file in minutes with DevExpress Document Server.
Combining the PDF file creation and archive creation is also really easy. String together some of the previous code we created and we have a nice app that creates a PDF file and archives it including encryption.
DevExpress Document Server – Unit Conversion
Another great feature of the DevExpress Document Server is unit conversion. How many times have you had to work with converting values and having no standard way to do this. To work with this you need to add the DevExpress.UnitConversion namespace.
This will expose the Unit Conversion classes for you to use in your code.
There are several unit types to convert to. Among these are a few interesting types. For our example, we will use temperature.
DevExpress Document Server makes this process really easy. You can simply just call the Temperature method of the Units class and pass the value to convert, the unit to convert from (Celcius) and the unit to convert to (Fahrenheit). Literally one line of code does this for you.
DevExpress Document Server – Barcode Generation
Something that has become very popular in recent years are QR codes. Honestly, how many times have you seen one on a business card or in a magazine? So it would make sense to be able to generate one easily for one of the documents we create using DevExpress Document Server.
Expose the Barcode classes by adding the DevExpress.Barcodes namespace.
The code needed to generate the QR code is slightly longer than the previous examples we looked at, but it is no more complex. We are creating a new object of type Barcode and setting the Symbology to a QR Code. We then set the color and other properties associated to a QR Code and then save the QR Code to a png.
The QR code is saved to a png file in the location we specified in code.
Using an application on my smartphone, I scan the QR Code directly from my computer screen. You can see that the application prompts me to open the URL specified in the QR Code.
Tapping ok opens up the URL specified in the QR code.
DevExpress Document Server is definitely something you strongly need to consider buying if you develop software. DevExpress developed this product with the developer in mind, making it really easy for you to incorporate into your code. The last thing a developer needs is to have to fiddle code just to make it work. DevExpress recognises this and creates high quality products that are very easy to use. DevExpress Document Server is only a small part of a very extensive DevExpress product range. Head on over to their site and have a look at what DevExpress can do for your applications.
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.