Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DevExpress Document Server – Automate Office Without Microsoft Office Automation

0.00/5 (No votes)
10 Sep 2013 1  
DevExpress Document Server – Automate Office Without Microsoft Office Automation

This Review is from our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

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.

  • DevExpress.Data
  • DevExpress.Office.Core
  • DevExpress.Spreadsheet.Core
  • DevExpress.Docs

Add the following namespace to your application.

DevExpress Document Server using namespace

Importing the above namespace will expose the Workbook class to your application.

DevExpress Document Server Workbook Class

Anyone familliar with Excel knows that a workbook can contain many work sheets, and that a work sheet can contain multiple cells.

reference worksheet cells

You can reference a worksheet by it’s name or by index. For this example we will just use it’s index.

worksheet cell referenced

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.

devexpress document server save document

The last two lines you add will save the spreadsheet and start Excel. Save your application and run your console app.

excel document generated and started

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.

spreadsheet output location

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.

devexpress document server add document

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).

  • DevExpress.Docs
  • DevExpress.RichEdit.Core
  • DevExpress.Office.Core
  • DevExpress.Data

To be able to work with rich documents, add the required using statement DevExpress.XtraRichEdit.

add reference to assembly

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.

redundant namespaces

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.

complete code for pdf export

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.

devexpress document server export to pdf

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.

  • DevExpress.Docs

Seeing as we have already added this reference above, we obviously don’t need to add it again.

devexpress document server compression

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.

image folder to compress

I have a folder with images of Endeavour’s launch into space. Let’s use that to illustrate the DevExpress Document Server compression functionality.

DevExpress Document Server Encryption Type

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.

DevExpress Document Server compression code

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.

DevExpress Document Server zip file output

Opening the archive and clicking on a file will prompt you for the password.

archive file password prompt

Because the files are encrypted, you won’t even see a thumbnail if you change the folder view.

encrypted files no preview

This shows how easily we can create an encrypted archive file in minutes with DevExpress Document Server.

Combining pdf export and archive

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.

devexpress document server unit conversion

This will expose the Unit Conversion classes for you to use in your code.

DevExpress Document Server Conversion Types

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 temperature conversion

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.

generate qr barcode

Expose the Barcode classes by adding the DevExpress.Barcodes namespace.

DevExpress Document Server QR Code Logic

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.

QR Code Saved to png

The QR code is saved to a png file in the location we specified in code.

QR Code Scanned

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.

QR Code Opens Website

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.

Disclosure of Material Connection: I received one or more of the products or services mentioned above for free in the hope that I would mention it on my blog. Regardless, I only recommend products or services I use personally and believe my readers will enjoy. I am disclosing this in accordance with the Federal Trade Commission’s 16 CFR, Part 255: “Guides Concerning the Use of Endorsements and Testimonials in Advertising.”

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here