Click here to Skip to main content
12,953,479 members (52,853 online)
Click here to Skip to main content
Add your own
alternative version


86 bookmarked
Posted 2 Jul 2009

Document Preview Application

, 2 Jul 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An application to preview your documents and files such as PDF, Doc, JPG, PPT, XSL.


Document Preview is an application that allows users to preview files such as PDF, Doc, XLS, JPG, MP3, AVI while browsing with an interface like Windows Explorer but without opening an extra application. It also allows users to move, copy, and delete folders or files.

The idea to develop this software came to my mind while trying to find a PDF document that contained specific information. My goal was to develop this application with techniques like Abstract Classes, Inheritance, Interfaces, and LINQ, and provide community samples about usages of these techniques.


Here are some controls that I used to provide Preview functionality.

Preview TypeFile TypesComponents and Controls UsedExecution Principle
Excel Preview*.Xlsx, *.XlsWebBrowser and Excel InteroperabilityThe principle is opening the document with interoperability and saving it under Temporary Internet Files as HTML, displaying that HTML with the WebBrowser control.
HTML Preview*.Html, *.HtmWebBrowserThe principle is assigning path to the URL property of the WebBrowser control.
Image Preview*.Jpeg, *.Jpg, *.Tif, *.Gif, *.Bmp, *.PngPictureBoxThe principle is assigning an Image to the Image property of the PictureBox control.
Media Preview*.Mp3, *.Wav, *.Wma, *.Mid, *.Avi, *.Mpg, *.WmvAxWMPLib.AxWindowsMediaPlayerThe principle is adding a file's path to the playlist and executing the Play command of the WindowsMediaPlayer control.
PDF Preview*.PdfAxAcroPDFLib.AxAcroPDFThe principle is assigning a file's path to the src property of the control.
Power Point Preview*.Pptx, *.Ppt, *.Ppsx, *.PpsWebBrowser and PowerPoint InteroperabilityThe principle is opening the document with interoperability and saving it under Temporary Internet Files as HTML, and displaying that HTML with the WebBrowser control.
Text Preview*.Txt, *.RtfRichTextBoxThe principle is simply reading the contents of a text document with StreamReader and then assigning the content to the Text property of a RichTextBox.
Word Preview*.Docx, *.DocWebBrowser and Word InteroperabilityThe principle is opening the document with interoperability and saving it under Temporary Internet Files as HTML, and displaying that HTML with the WebBrowser control.

Using the Code

As I mentioned, all actions should derive from the Action abstract class. The code for the Action class can be found below. The most important point is that every child class should implement the DoAction method differently. The code of action should be written in this method.

public abstract class Action
    public abstract void DoAction(string path, FileType parFileType, frmMain frm);
    public void ShowError(string errorMessage)
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
    public bool FileExists(string path)
        if (File.Exists(path))
            return true;
            ShowError("File is not accessable");
            return false;
    public bool DirectoryExists(string path)
        if (Directory.Exists(path))
            return true;
            ShowError("Directory is not accessable");
            return false;

All preview controls should implement the IPreview interface; this interface contains the Preview method that should be implemented. The execution principle code for the preview controls should be written in this method.

public interface IPreview
    void Preview(string path);

Here, I provide a sample Preview Action class that inherits the Action class.

[ActionAttributes(ActionName ="Preview", IsDefaultAction =true, 
   ActionsGroupTypes =new GroupTypes[] { GroupTypes.File })]
public class Preview : DocExp.AbstractClasses.Action
    public override void DoAction(string path, FileType parFileType, frmMain frm)
            if (FileExists(path))
                if (parFileType.PreviewType ==null)
                    ShowError("Preview not available");
                    Type t = parFileType.PreviewType;
                    SplitContainer sc=(SplitContainer)frm.Controls.Find("sc",true)[0];
                    Panel pnl=(Panel)sc.Panel2.Controls.Find("pnlPreview",true)[0];
                    if (pnl.Controls.Count > 0)
                        Control pOld = pnl.Controls[0];
                    sc.Panel2Collapsed = false;
                    IPreview p = (IPreview)Activator.CreateInstance(t);
                    ((Control)p).Dock =DockStyle.Fill;
        catch (Exception ex)
            ShowError("An error occured while loading preview control");

Here is the code that I used to add file types as search criteria, and the actions as context menu items:

fileTypes.Add(new FileType("PDF Files", "*.Pdf", 
              typeof(PdfPreview), pdfFileTypeGroup));
fileTypes.Add(new FileType("JPG Files", "*.Jpg", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("JPEG Files", "*.Jpeg", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("TIFF Files", "*.Tif", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("GIF Files", "*.Gif", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("BMP Files", "*.Bmp", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("PNG Files", "*.Png", 
              typeof(ImagePreview), imageFileTypeGroup));
fileTypes.Add(new FileType("MP3 Files", "*.Mp3", 
              typeof(MediaPreview), musicFileTypeGroup));

foreach (Type t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
    if (t.BaseType == typeof(DocExp.AbstractClasses.Action))
        string actionName = ((ActionAttributes)
          t.GetCustomAttributes(typeof(ActionAttributes), true)[0]).ActionName;
        bool isDefault = ((ActionAttributes)
        GroupTypes[] gt = ((ActionAttributes)
        ActionType at = new ActionType(actionName, t, isDefault);
        foreach (GroupTypes g in gt)


For bug reports and suggestions, feel free to contact me at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tamer Oz
Team Leader
Turkey Turkey
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

You may also be interested in...

Comments and Discussions

QuestionHad a bad time on the upgrade to vs2012 Pin
DanielBar20-Oct-13 23:19
memberDanielBar20-Oct-13 23:19 
QuestionReg word search in this document preview tool Pin
kumaran m.3-Sep-12 22:55
memberkumaran m.3-Sep-12 22:55 

I have downloaded,tested the doc preview application and its working fine.

Great Job!

My query:-

is GOTO option is available in this tool. GOTO means, search and show option (Am not talking abt the default search which is available in the tool).

is this possible in ur tool.

with regards
Kumaran M.

QuestionWorking Executable Pin
Colby Seider25-Apr-12 3:52
memberColby Seider25-Apr-12 3:52 
QuestionHow to show icon image size more bigger. Pin
Rakesh Bhavsar16-Mar-12 1:11
memberRakesh Bhavsar16-Mar-12 1:11 
GeneralThanks Pin
Member 794631528-Feb-12 9:47
memberMember 794631528-Feb-12 9:47 
Generalthhnx Pin
abjain28-Feb-12 9:46
memberabjain28-Feb-12 9:46 
Questionhello I Have Question, about a Exception Pin
Member 469706821-Mar-11 16:25
memberMember 469706821-Mar-11 16:25 
AnswerRe: hello I Have Question, about a Exception Pin
Tamer Oz21-Mar-11 20:55
memberTamer Oz21-Mar-11 20:55 
GeneralMy vote of 3 Pin
Nishantha Hevavitharana17-Dec-10 14:06
memberNishantha Hevavitharana17-Dec-10 14:06 
Generalcool app! Pin
~Dim~28-Sep-10 5:04
member~Dim~28-Sep-10 5:04 
GeneralMy vote of 1 Pin
Abu Bakr El-seddiq16-Apr-10 5:41
memberAbu Bakr El-seddiq16-Apr-10 5:41 
QuestionCannot open source with MS Visual Studio 2005 Pin
Member 28715804-Apr-10 8:32
memberMember 28715804-Apr-10 8:32 
AnswerRe: Cannot open source with MS Visual Studio 2005 Pin
Tamer Oz4-Apr-10 19:52
memberTamer Oz4-Apr-10 19:52 
QuestionTakes a long time Pin
miridfd19-Dec-09 13:16
membermiridfd19-Dec-09 13:16 
GeneralPDF viewer runs out of memory Pin
Rick Hansen6-Jul-09 10:37
memberRick Hansen6-Jul-09 10:37 
GeneralRe: PDF viewer runs out of memory Pin
Tamer Oz6-Jul-09 20:01
memberTamer Oz6-Jul-09 20:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 2 Jul 2009
Article Copyright 2009 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid