Convert/Display PDF & Word Files as Images in .NET

Recently I had to write some code as a sample to be able to display Word and Acrobat files on a Web page in a 2-page view. We couldn’t simply use a plugin or application on the user’s machine to do so. The solution was to export each page out to an image and then display the pages in any way we needed. We therefore needed to do some document export code from our application.

Word to Images

Converting Word files (.DOC & .DOCX) to images was fairly simple, although I do think I probably took a longer approach. The problem is that the older DOC format and the newer DOCX format have different APIs to work with. So instead of doing this, I simply exported them both to XPS and then used the XPS API to retrieve images for each page. This last part was not obvious to me till I found the one line of code required to do this on a forum – I apologize for not linking to this as I can’t find that link anymore. The credit for that part is wholly the original author’s.

PDF to Images

Converting PDF to images was a major issue. There is no direct way of doing this. There are many 3rd party components that are available to do this, but most of them cost a bomb. Some free ones like PDFSharp are able to iterate pages but there is no way to export a complete page to an image without walking through the entire structure of the page and redrawing everything.

This is where I found the GFLAX library. This requires GhostScript for Windows to be installed on the machine as well. You can register the DLL and then reference it in your .NET code.

Code Sample

I’m attaching the entire code sample to this post as a download. The code is released with the open source BSD license. All external components (Word and Office interop assemblies, GFL, etc.) have the copyright of their owners and must be adhered to.

Usage

Once you download the attachment and extract it, open it in Visual Studio 2008. Make sure you’ve installed GhostScript from the link above and run a “regsvr32 GFLAX.dll” for the GFLAX Library. Add reference to Microsoft.Office.Interop.Word on your machine (and remove the marked lines from Web.config) from the .NET tab and to GFLAX from the COM tab.

Run the application and upload .doc, .docx and .pdf files and you can then view them in the browser directly.

WordDisplay 
Two page display of an uploaded Word file. Works with PDF files too.

Download:
Tags: , , ,
Categories: ASP.NET | Development | Office | Tips | Download

6 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

WebsiteSpark and What It Means

Yesterday Microsoft made an announcement that has extremely important implications if you are into Web application development. The WebsiteSpark program offers a ton of Microsoft software for free for web developers and web development companies that qualify. You get all these software for 3 years for both development and production usage. The list of software is:

  • 3 licenses of Visual Studio 2008 Professional Edition
  • 1 license of Expression Studio 3
  • 2 licenses of Expression Web 3
  • 4 processor licenses of Windows Web Server 2008 R2
  • 4 processor licenses of SQL Server 2008 Web Edition
  • DotNetPanel control panel

You can read more about the program on Scott Guthrie’s blog post and at the main site.

What I want to explore in this post is what it means for Web development in general. Currently, there is a trend or impression that if you need to create a software for a SME, you need to use the LAMP stack to remain competitive cost-wise. While true to some extent, what you do lose out is in terms of productivity and time-to-market with tools such as VS2008 and the Expression suite. But the cost of entry to use these has often been a pitfall to getting individual Web developers or smaller Web project companies as well as for the SME.

With WebsiteSpark this so-called “advantage” of the LAMP stack goes for a toss. Not only do you get all the tools you need for free for developing projects for SME companies you also get a number of other additional options that help ease your development, such as:

  • The Web Application Toolkits: A set of pre-packaged templates, samples, source code, etc. that can be simply plugged into your site to extend it with some extra features.
  • The Web Platform Installer: Allows you to quickly set up a developer or even production machine with not just the base requirements but also a number of free Web Applications such as WordPress, Umbraco, SugarDRM, DotNetNuke, Moodle etc. by simply following the setup prompts. Take a look at the Web Application Gallery and see how easy it is to setup these without having to mess around with configuration files and database connection settings on Windows.

This basically means that setting up and using Windows as a platform for Web application development not only is easier and faster but also is now available for free! You can also run most of the open source Web apps on Windows with better performance and scalability. So is there any real reason to still go for the LAMP stack? Try the above stuff out and then decide.


Tags: , , , ,
Categories: ASP.NET | Development | Internet | Microsoft | Rave

12 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Three Articles in August 2009 PCQuest

PCQuest has just published 3 of my articles in their current issue. It was actually supposed to be 4, but the one on ASP.NET 4 got lost in the mail and a resend was received too late for this issue. You can read these articles here:

I’m also going to try and compile a list of articles I’ve written in PCQuest over the last few years and link to them here.


Tags: , , , ,
Categories: Windows 7 | SilverLight | Office | Development | ASP.NET

4 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Loading Child Table Data in SilverLight & RIA Services

I’m currently working with the .NET RIA Services SDK with Silverlight 3 for getting and modifying data from the server using LINQ. Now there are a ton of samples and blog posts available that show you how to work with data from the server. However, one thing in common with all these posts is that they usually work with only one table at a time. What if you want to show the result of a join on the table? This is where a relatively unknown feature called the IncludeAttribute comes in. Let’s take an example for this to explain it in detail.

More...


Tags: , ,
Categories: Development | Tips | SilverLight

6 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

SilverLight 3.0 Out of Browser Mini Tutorial

SilverLight 3.0 has just been released to the web. And there is now an even more important reason to start using this great technology – running apps written for the browser can now be run as a standalone application from the user’s desktop as well. This post takes a detailed look at how you can start using this new feature. More...


Tags: , ,
Categories: Development | SilverLight

5 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Windows 7 and Application Compatibilty

I’m updating my blog after a long time since I’ve been tied up in doing a large number of Windows 7 Application Compatibility workshops all across India with a number of Independent Software Vendors (ISVs) for their products. We’re basically testing out their products on the Windows 7 RC to see any issues that might surface and what they need to do to resolve them.

Most application run just fine on Windows 7. However, in the cases where there is an issue, it almost invariably turns out that the product has not been tested on Windows Vista and therefore it also fails on Windows XP. Vista and 7 have a tighter security mechanism called UAC that needs to be addressed in applications – and is something that many developers face problems with.

So here are two of the biggest issues that I’ve seen and their solutions: More...


Tags: , ,
Categories: Windows 7 | Tips | Development

8 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Oracle Java and Oracle MySQL

Ever since news broke about the surprising takeover of Sun Microsystems by Oracle Corporation, people have started discussing the future of some of the important platforms in this context. This is what I think:

The Future of Java

Oracle is not traditionally a framework platform company. Sun, on the other hand, is the creator of one of the most widely used application frameworks – Java. So what happens once this takeover is done?

  • Oracle continues innovating in Java: This is fairly possible – however may be unlikely. After all for Oracle, Java is more of a application platform that they use rather than something they contribute to.
  • Oracle kills off Java slowly: This is very unlikely as they do have something that is used by a large number in the developer community and is also a competitor to Microsoft’s .NET.
  • Oracle makes Java completely open source under the GPL*: this is something that I see as very likely. Not only will they win a ton of brownie points with the FOSS folks, but to a great extent get rid of the development and maintenance overhead of keeping Java fully internal.

* Java is quite open sourced already. However as the FAQ states, there are still a bunch of things that remain to be opened.

The Future of MySQL

This is where I see more of an issue. After all, MySQL was a competitor in the same space (or at least, that’s what MySQL followers claim). So will Oracle continue to develop and support MySQL? If they do, not only will they have different “divisions” competing with each other in the same space, but also will need to provide an “official” migration path forward.

As I see it, MySQL as a “brand” will probably go away in a couple of years. Oracle will incorporate some of its features into the lower end database platforms it currently offers. Open Source “forks” of the last MySQL version to be released will come up and try to take up the place let vacant by its departure.

Others

Solaris, though now fully open source, has not really got the momentum of something like Linux. And I don’t see Oracle changing that too much. I think Oracle will downplay the Solaris significance and slowly end it mercifully. However…

Sun’s cloud computing initiative on Solaris combined with Java and the Oracle database make it a powerful stack – quite enough to compete with the Amazon and Microsoft Azure platforms.

It’ll be interesting to note the reactions from companies like IBM, HP, Microsoft and others to see what they think of this as well.


Tags: , , , ,
Categories: Development

9 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Changing Aspect Ratio for Videos

If you use a large LCD HDTV for watching movies you might come across a situation where you might see black bars around your playing video. Vertical bars (pillarboxing) or horizontal bars (letterboxing) are caused by wrong encoding of videos for the aspect ratio of your widescreen television. I was having this issue with a number of older DVDs that I ripped to watch on my HDTV. There are a few different ways to correct these.

Usually the playing device (DVD player, Media Tank, etc.) should take care of this if the encoding has least anamorphic encoding on. If this is not there, you will not see the aspect ration getting corrected automatically.

You can try and re-encode the video using Super or AutoMKV to the correct aspect ratio. This will require a while in case you have a number of videos to correct but will give the best possible conversion.

The third way is probably the easiest – at least when it comes to converting Divx videos. There is a small utility called MPEG4 Modifier that you can download. This utility changes the AVI header to match the widescreen format that you wish. Take a look at the screen below:

mpeg4modifier  Simply select the Display AR to 16:9 and save

The important thing to remember is to change the DISPLAY ASPECT RATIO and not the PIXEL ASPECT RATIO for the conversion. The PAR changes the aspect ratio of the pixels comprising the video whereas the DAR only changes the header so that the video can be auto-sized in the player.

If you wish to convert a bunch of files, there is a command line version of the application as well available from the same link as above. However, there is one difference between the GUI and console version. The latter doesn’t have an option to modify the DAR, only the PAR.

Since source code is available for both, I downloaded the one for the console app and added the calculations required to use DAR from the GUI version of the app. I take NO credit for any of these calculations – all of it goes to the original author of the program. The program is now compiled in .NET 3.5SP1 for x64. You can now convert an AVI file from the command line like this:

mmcl.exe –-dar 16:9 in.avi out.avi

Simply run the console app to see the different options. You can download the binaries and source code below:

MPEG4 Modifier Console: Source x64 x32 (coming soon)


Tags: , , ,
Categories: Development | Movies | Tips

7 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Managed Win7 Taskbar Enhancements

Windows 7 has a ton of new enhancements right there in the Taskbar itself. A quick list of them includes:

  • Unified Launching and Switching
  • Jump Lists
  • Destinations
  • Tasks
  • Thumbnail Toolbars
  • Icon Overlays
  • Progress Bars
  • Notification Area
  • Interactive Thumbnails

Your applications too can start using these features’ in them if you want. The documentation for these are available at MSDN here. The only issue is that this is currently possible only with un-managed APIs – COM & Win32.

So what happens if you’re a .NET WinForms/WPF developer and want these features in your applications as well? Well, fear not there is a way out. There are two open source projects that allow you to use the Win7 taskbar enhancements in your managed code applications as well.

The first is a project on CodePlex called Windows 7 TaskBar Extensions. This is a new project with only a few things currently implemented but seems to be heading in the right direction for a small, lean implementation of the Taskbar features in .NET.

The second is a much more comprehensive project called very simply “Windows 7 Taskbar”. This is based upon the excellent Vista Bridge library and offers all the features that are currently possible. There are a number of sample applications as well that demonstrate many of the different features. The only part that didn’t work for me is creating Jump Lists.

So if you’re a Windows application developer and want to add the new features of Vista and Windows 7 into your application, Vista Bridge and these other two libraries are surely something that you will want to try out.


Tags: , , ,
Categories: Windows 7 | Rave | Development | Windows Vista

6 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Writing FOSS During the Recession

A few months back I had written a post on how FOSS might suffer during the period of recession. My point was that FOSS depends on contributions – whether in terms of actual donations of resources mostly on their free time (code, writing, design, etc.)  or in terms of money. However in this time of extreme recession the world over, contributors will probably have much more important things to do.

  • If a contributor (developer, technical writer, designer) already has a job, he/she is probably going to work twice as hard to keep that job – maybe working extra time and taking on more responsibilities – reducing time to contribute to FOSS.
  • If a contributor is out of a job (whether out of college or been laid off), he/she is going to be busy making ends meet and looking for other employment. In such a case, contributing to FOSS is probably going to be last thing on their mind.

Now, it seems that others are thinking the same – including A Sr. VP of Novell and ex-CTO of the Linux Foundation, Markus Rex. In an interview recently said exactly the same thing.

Q: What are the challenges you see for the business moving forward?

I perceive a challenge always as an opportunity. It gives us the opportunity to change to a better way from the way things have been done in the past.

The other thing is in both Europe and the US the rise of the unemployment rate is something that is rather unprecedented. The open source community to a certain degree is dependant on the willingness of people to contribute. We see no indication that anything might change there, but who knows? People need something to live off.

“Something to live off” is right. We’ve already heard about the stagnation in main stream products like OpenOffice.org. We’ll probably see more such things happen soon – a little sad I would say. A lively discussion on this is going on over at Slashdot on this topic.


Tags: ,
Categories: Linux | Development

3 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed