Türkçe   |   English
Calendar
ArticleCategories
Article Archive
Most Read Articles
Links
Blogroll
Files

Document Preview Application
Publish Date 08.08.2009
Categories C#,Opensource
Views 332
Summary
PocketPcAgent is an application that can be installed on Windows Mobile Devices that are running Windows Mobile 5.0 or higher. While running, the application listens to events such as SMS Received, Call Missed, E-Mail Received, Battery Strength Changed, and much more on the Pocket PC. Users can subscribe to events and select to be notified when an event is raised by a channel such as email, SMS, or phone call.

Introduction

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.

Background

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

Preview Type File Types Components and Controls Used Execution Principle
Excel Preview *.Xlsx, *.Xls WebBrowser and Excel Interoperability The 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, *.Htm WebBrowser The principle is assigning path to the URL property of the WebBrowser control.
Image Preview *.Jpeg, *.Jpg, *.Tif, *.Gif, *.Bmp, *.Png PictureBox The principle is assigning an Image to the Image property of the PictureBox control.
Media Preview *.Mp3, *.Wav, *.Wma, *.Mid, *.Avi, *.Mpg, *.Wmv AxWMPLib.AxWindowsMediaPlayer The principle is adding a file's path to the playlist and executing the Play command of the WindowsMediaPlayer control.
PDF Preview *.Pdf AxAcroPDFLib.AxAcroPDF The principle is assigning a file's path to the src property of the control.
Power Point Preview *.Pptx, *.Ppt, *.Ppsx, *.Pps WebBrowser and PowerPoint Interoperability The 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, *.Rtf RichTextBox The 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, *.Doc WebBrowser and Word Interoperability The 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)
    {
        MessageBox.Show(errorMessage,"Error", 
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    public bool FileExists(string path)
    {
        if (File.Exists(path))
        {
            return true;
        }
        else
        {
            ShowError("File is not accessable");
            return false;
        }
    }
    public bool DirectoryExists(string path)
    {
        if (Directory.Exists(path))
        {
            return true;
        }
        else
        {
            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)
    {
        try
        {
            if (FileExists(path))
            {
                if (parFileType.PreviewType ==null)
                {
                    ShowError("Preview not available");
                }
                else
                {
                    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];
                        pOld.Dispose();
                    }
                    pnl.Controls.Clear();
                    sc.Panel2Collapsed = false;
                    IPreview p = (IPreview)Activator.CreateInstance(t);
                    pnl.Controls.Add((Control)p);
                    ((Control)p).Dock =DockStyle.Fill;
                    p.Preview(path);
                    frm.SetPreviewPanelButtonsVisibility();
                }
            }
        }
        catch (Exception ex)
        {
            ShowError("An error occured while loading preview control");
            frm.SetPreviewPanelButtonsVisibility();
        }
    }
}

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)
          t.GetCustomAttributes(typeof(ActionAttributes),true)[0]).IsDefaultAction;
        GroupTypes[] gt = ((ActionAttributes)
          t.GetCustomAttributes(typeof(ActionAttributes),true)[0]).ActionsGroupTypes;
        ActionType at = new ActionType(actionName, t, isDefault);
        foreach (GroupTypes g in gt)
        {
            at.ActionsGroupTypes.Add(g);
        }
        actionTypes.Add(at);
    }
}
Download Source

Contact

For bug reports and suggestions, feel free to contact me at oztamer@hotmail.com.

Codeproject

Add Comment
First Name Last Name
Web Site
E-Mail
Comment
Security Picture

Photos

A break for traditional American Breakfast
Show All
Me in MSDN Forums
-   Answered the question Close some open windows by name in the Visual C# General forum
-   Answered the question Close some open windows by name in the Visual C# General forum
-   Answered the question Datagridview Click Event in C#.net in the Visual C# General forum
-   Contributed a proposed answer to the question Close some open windows by name in the Visual C# General forum
-   Replied to the question Datagridview Click Event in C#.net in the Visual C# General forum
-   Replied to the question Close some open windows by name in the Visual C# General forum
-   Contributed a helpful post (total votes:2) to the forums thread Array of events? in the Visual C# General forum
-   Answered the question Displaying random images from resources in the project in the Visual C# Language forum
-   Contributed a helpful post (total votes:1) to the forums thread sorted dictionary (Can i get key from value) in the Visual C# General forum
-   Contributed a proposed answer to the question Non-existing Argument in format string in the Visual C# General forum
Entries
News
Articles