Table of Contents

Custom previewing (C# / netframework)

Note

This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\netframework\25.Printing and Exporting\20.CustomPreview and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​NET-​demos/​tree/​master/​csharp/​VS2022/​netframework/​Modules/​25.​Printing and Exporting/​20.​Custom​Preview

Overview

FlexCel comes with a full featured viewer that you can use to display a preview of Excel files in your application, and without having any printer installed.

Concepts

  • How to create a preview form that you can embed inside your application, including thumbnails, navigation, etc.

  • How to Export to PDF from a thread, allowing the user to cancel it.

Files

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;

//
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("Custom Preview")]
[assembly: AssemblyDescription("A custom previewer of xls files using FlexCel engine.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("TMS Software")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("(c) 2002 - 2025 TMS Software")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

//
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:

[assembly: AssemblyVersion("7.25.0.0")]

//
// In order to sign your assembly you must specify a key to use. Refer to the 
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing. 
//
// Notes: 
//   (*) If no key is specified, the assembly is not signed.
//   (*) KeyName refers to a key that has been installed in the Crypto Service
//       Provider (CSP) on your machine. KeyFile refers to a file which contains
//       a key.
//   (*) If the KeyFile and the KeyName values are both specified, the 
//       following processing occurs:
//       (1) If the KeyName can be found in the CSP, that key is used.
//       (2) If the KeyName does not exist and the KeyFile does exist, the key 
//           in the KeyFile is installed into the CSP and used.
//   (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
//       When specifying the KeyFile, the location of the KeyFile should be
//       relative to the project output directory which is
//       %Project Directory%\obj\<configuration>. For example, if your KeyFile is
//       located in the project directory, you would specify the AssemblyKeyFile 
//       attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
//   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
//       documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]

Form1.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Drawing2D;

using System.Diagnostics;
using System.Threading;

using FlexCel.Core;
using FlexCel.XlsAdapter;
using FlexCel.Winforms;
using FlexCel.Render;
using FlexCel.Pdf;

namespace CustomPreview
{
    /// <summary>
    /// Previewer of files.
    /// </summary>
    public partial class mainForm: System.Windows.Forms.Form
    {
        public mainForm() : this(new string[0])
        {
        }

        public mainForm(string[] Args)
        {
            InitializeComponent();
            ResizeToolbar(mainToolbar);
            if (Args.Length > 0)
            {
                LoadFile(Args[0]);
            }

            if (ExcelFile.SupportsXlsx)
            {
                this.openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Excel 97/2003|*.xls|Excel 2007|*.xlsx;*.xlsm|All files|*.*";
            }

            MainPreview.CenteredPreview = true;
            thumbs.CenteredPreview = true;
        }

        private void ResizeToolbar(ToolStrip toolbar)
        {

            using (Graphics gr = CreateGraphics())
            {
                double xFactor = gr.DpiX / 96.0;
                double yFactor = gr.DpiY / 96.0;
                toolbar.ImageScalingSize = new Size((int)(24 * xFactor), (int)(24 * yFactor));
                toolbar.Width = 0; //force a recalc of the buttons.
            }
        }


        private void UpdatePages()
        {
            edPage.Text = String.Format("{0} of {1}", MainPreview.StartPage, MainPreview.TotalPages);
        }

        private void flexCelPreview1_StartPageChanged(object sender, System.EventArgs e)
        {
            UpdatePages();
        }

        private void ChangePages()
        {
            string s = edPage.Text.Trim();
            int pos = 0;
            while (pos < s.Length && s[pos] >= '0' && s[pos] <= '9') pos++;
            if (pos > 0)
            {
                int page = MainPreview.StartPage;
                try
                {
                    page = Convert.ToInt32(s.Substring(0, pos));
                }
                catch (Exception)
                {
                }

                MainPreview.StartPage = page;
            }
            UpdatePages();
        }

        private void edPage_Leave(object sender, System.EventArgs e)
        {
            ChangePages();
        }

        private void edPage_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                ChangePages();
            if (e.KeyChar == (char)27)
                UpdatePages();
        }

        private void flexCelPreview1_ZoomChanged(object sender, System.EventArgs e)
        {
            UpdateZoom();
        }

        private void UpdateZoom()
        {
            edZoom.Text = String.Format("{0}%", (int)Math.Round(MainPreview.Zoom * 100));
            if (MainPreview.AutofitPreview == TAutofitPreview.None) UpdateAutofitText();
        }

        private void ChangeZoom()
        {
            string s = edZoom.Text.Trim();
            int pos = 0;
            while (pos < s.Length && s[pos] >= '0' && s[pos] <= '9') pos++;
            if (pos > 0)
            {
                int zoom = (int)Math.Round(MainPreview.Zoom * 100);
                try
                {
                    zoom = Convert.ToInt32(s.Substring(0, pos));
                }
                catch (Exception)
                {
                }

                MainPreview.Zoom = zoom / 100.0;
            }
            UpdateZoom();
        }

        private void edZoom_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                ChangeZoom();
            if (e.KeyChar == (char)27)
                UpdateZoom();
        }

        private void edZoom_Enter(object sender, System.EventArgs e)
        {
            ChangeZoom();
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        private void openFile_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;
            LoadFile(openFileDialog.FileName);
        }

        //The event that will actually provide the password to open the empty form.
        private void GetPassword(OnPasswordEventArgs e)
        {
            PasswordForm Pwd = new PasswordForm();
            e.Password = string.Empty;
            if (Pwd.ShowDialog() != DialogResult.OK) return;
            e.Password = Pwd.Password;
        }


        internal void LoadFile(string FileName)
        {
            openFileDialog.FileName = FileName;
            lbSheets.Items.Clear();

            XlsFile xls = new XlsFile();
            xls.HeadingColWidth = -1;
            xls.HeadingRowHeight = -1;
            xls.Protection.OnPassword += new OnPasswordEventHandler(GetPassword);
            xls.Open(FileName);

            for (int i = 1; i <= xls.SheetCount; i++)
            {
                lbSheets.Items.Add(xls.GetSheetName(i));
            }

            lbSheets.SelectedIndex = xls.ActiveSheet - 1;

            flexCelImgExport1.Workbook = xls;
            MainPreview.InvalidatePreview();
            Text = "Custom Preview: " + openFileDialog.FileName;
            //btnHeadings.Checked = flexCelImgExport1.Workbook.PrintHeadings;
            //btnGridLines.Checked = flexCelImgExport1.Workbook.PrintGridLines;
            btnFirst.Enabled = true; btnPrev.Enabled = true; btnNext.Enabled = true; btnLast.Enabled = true; edPage.Enabled = true;
            btnZoomIn.Enabled = true; edZoom.Enabled = true; btnZoomOut.Enabled = true;
            btnGridLines.Enabled = true; btnHeadings.Enabled = true; btnRecalc.Enabled = true; btnPdf.Enabled = true;

        }

        private void btnFirst_Click(object sender, System.EventArgs e)
        {
            MainPreview.StartPage = 1;
        }

        private void btnPrev_Click(object sender, System.EventArgs e)
        {
            MainPreview.StartPage--;
        }

        private void btnNext_Click(object sender, System.EventArgs e)
        {
            MainPreview.StartPage++;
        }

        private void btnLast_Click(object sender, System.EventArgs e)
        {
            MainPreview.StartPage = MainPreview.TotalPages;
        }

        private void btnZoomOut_Click(object sender, System.EventArgs e)
        {
            MainPreview.Zoom -= 0.1;
        }

        private void btnZoomIn_Click(object sender, System.EventArgs e)
        {
            MainPreview.Zoom += 0.1;
        }

        private void lbSheets_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (flexCelImgExport1.Workbook == null) return;
            if (lbSheets.Items.Count > flexCelImgExport1.Workbook.SheetCount) return;
            flexCelImgExport1.Workbook.ActiveSheet = lbSheets.SelectedIndex + 1;
            MainPreview.InvalidatePreview();
        }

        private void btnPdf_Click(object sender, System.EventArgs e)
        {
            if (flexCelImgExport1.Workbook == null)
            {
                MessageBox.Show("There is no open file");
                return;
            }
            if (PdfSaveFileDialog.ShowDialog() != DialogResult.OK) return;

            using (FlexCelPdfExport PdfExport = new FlexCelPdfExport(flexCelImgExport1.Workbook, true))
            {
                if (!DoExportToPdf(PdfExport)) return;
            }

            if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
            Process.Start(PdfSaveFileDialog.FileName);
        }

        private bool DoExportToPdf(FlexCelPdfExport PdfExport)
        {
            PdfThread MyPdfThread = new PdfThread(PdfExport, PdfSaveFileDialog.FileName, cbAllSheets.Checked);
            Thread PdfExportThread = new Thread(new ThreadStart(MyPdfThread.ExportToPdf));
            PdfExportThread.Start();
            using (PdfProgressDialog Pg = new PdfProgressDialog())
            {
                Pg.ShowProgress(PdfExportThread, PdfExport);
                if (Pg.DialogResult != DialogResult.OK)
                {
                    PdfExport.Cancel();
                    PdfExportThread.Join(); //We could just leave the thread running until it dies, but there are 2 reasons for waiting until it finishes:
                                            //1) We could dispose it before it ends. This is workaroundable.
                                            //2) We might change its workbook object before it ends (by loading other file). This will surely bring issues.
                    return false;
                }

                if (MyPdfThread != null && MyPdfThread.MainException != null)
                {
                    throw MyPdfThread.MainException;
                }
            }
            return true;
        }

        private void cbAllSheets_CheckedChanged(object sender, System.EventArgs e)
        {
            lbSheets.Visible = !cbAllSheets.Checked;
            sheetSplitter.Visible = lbSheets.Visible;
            flexCelImgExport1.AllVisibleSheets = cbAllSheets.Checked;
            if (flexCelImgExport1.Workbook == null) return;
            MainPreview.InvalidatePreview();

        }

        private void btnRecalc_Click(object sender, System.EventArgs e)
        {
            if (flexCelImgExport1.Workbook == null)
            {
                MessageBox.Show("Please open a file before recalculating.");
                return;
            }
            flexCelImgExport1.Workbook.Recalc(true);
            MainPreview.InvalidatePreview();

        }


        private void mainForm_Load(object sender, System.EventArgs e)
        {
        }

        private void btnHeadings_Click(object sender, EventArgs e)
        {
            ExcelFile xls = flexCelImgExport1.Workbook;
            if (xls == null)
            {
                return;
            }

            if (cbAllSheets.Checked)
            {
                int SaveActiveSheet = xls.ActiveSheet;
                for (int sheet = 1; sheet <= xls.SheetCount; sheet++)
                {
                    xls.ActiveSheet = sheet;
                    xls.PrintHeadings = btnHeadings.Checked;
                }
                xls.ActiveSheet = SaveActiveSheet;
            }
            else
            {
                xls.PrintHeadings = btnHeadings.Checked;
            }
            MainPreview.InvalidatePreview();

        }

        private void btnGridLines_Click(object sender, EventArgs e)
        {
            ExcelFile xls = flexCelImgExport1.Workbook;
            if (xls == null)
            {
                return;
            }

            if (cbAllSheets.Checked)
            {
                int SaveActiveSheet = xls.ActiveSheet;
                for (int sheet = 1; sheet <= xls.SheetCount; sheet++)
                {
                    xls.ActiveSheet = sheet;
                    xls.PrintGridLines = btnGridLines.Checked;
                }
                xls.ActiveSheet = SaveActiveSheet;
            }
            else
            {
                xls.PrintGridLines = btnGridLines.Checked;
            }
            MainPreview.InvalidatePreview();

        }

        private void noneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainPreview.AutofitPreview = TAutofitPreview.None;
            UpdateAutofitText();
        }

        private void fitToWidthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainPreview.AutofitPreview = TAutofitPreview.Width;
            UpdateAutofitText();
        }

        private void fitToHeightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainPreview.AutofitPreview = TAutofitPreview.Height;
            UpdateAutofitText();
        }

        private void fitToPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainPreview.AutofitPreview = TAutofitPreview.Full;
            UpdateAutofitText();
        }

        private void UpdateAutofitText()
        {
            switch (MainPreview.AutofitPreview)
            {
                case TAutofitPreview.None:
                    btnAutofit.Text = "No Autofit";
                    break;
                case TAutofitPreview.Width:
                    btnAutofit.Text = "Fit to Width";
                    break;
                case TAutofitPreview.Height:
                    btnAutofit.Text = "Fit to Height";
                    break;
                case TAutofitPreview.Full:
                    btnAutofit.Text = "Fit to Page";
                    break;
                default:
                    break;
            }

        }

    }

    #region PdfThread
    class PdfThread
    {
        private FlexCelPdfExport PdfExport;
        private string FileName;
        private bool AllVisibleSheets;
        private Exception FMainException;

        internal PdfThread(FlexCelPdfExport aPdfExport, string aFileName, bool aAllVisibleSheets)
        {
            PdfExport = aPdfExport;
            FileName = aFileName;
            AllVisibleSheets = aAllVisibleSheets;
        }

        internal void ExportToPdf()
        {
            try
            {
                if (AllVisibleSheets)
                {
                    try
                    {
                        using (FileStream f = new FileStream(FileName, FileMode.Create, FileAccess.Write))
                        {
                            PdfExport.BeginExport(f);
                            PdfExport.PageLayout = TPageLayout.Outlines;
                            PdfExport.ExportAllVisibleSheets(false, System.IO.Path.GetFileNameWithoutExtension(FileName));
                            PdfExport.EndExport();
                        }
                    }
                    catch
                    {
                        try
                        {
                            File.Delete(FileName);
                        }
                        catch
                        {
                            //Not here.
                        }
                        throw;
                    }
                }
                else
                {
                    PdfExport.PageLayout = TPageLayout.None;
                    PdfExport.Export(FileName);
                }
            }
            catch (Exception ex)
            {
                FMainException = ex;
            }
        }

        internal Exception MainException
        {
            get
            {
                return FMainException;
            }
        }
    }
    #endregion


}

Form1.Designer.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Threading;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using FlexCel.Winforms;
using FlexCel.Render;
using FlexCel.Pdf;
namespace CustomPreview
{
    public partial class mainForm: System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm));
            this.flexCelImgExport1 = new FlexCel.Render.FlexCelImgExport();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.panel1 = new System.Windows.Forms.Panel();
            this.MainPreview = new FlexCel.Winforms.FlexCelPreview();
            this.thumbs = new FlexCel.Winforms.FlexCelPreview();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.panelLeft = new System.Windows.Forms.Panel();
            this.cbAllSheets = new System.Windows.Forms.CheckBox();
            this.label2 = new System.Windows.Forms.Label();
            this.sheetSplitter = new System.Windows.Forms.Splitter();
            this.lbSheets = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.PdfSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.mainToolbar = new System.Windows.Forms.ToolStrip();
            this.openFile = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFirst = new System.Windows.Forms.ToolStripButton();
            this.btnPrev = new System.Windows.Forms.ToolStripButton();
            this.edPage = new System.Windows.Forms.ToolStripTextBox();
            this.btnNext = new System.Windows.Forms.ToolStripButton();
            this.btnLast = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnAutofit = new System.Windows.Forms.ToolStripDropDownButton();
            this.noneToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fitToWidthToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fitToHeightToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fitToPageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.btnZoomOut = new System.Windows.Forms.ToolStripButton();
            this.edZoom = new System.Windows.Forms.ToolStripTextBox();
            this.btnZoomIn = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.btnGridLines = new System.Windows.Forms.ToolStripButton();
            this.btnHeadings = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnRecalc = new System.Windows.Forms.ToolStripButton();
            this.btnPdf = new System.Windows.Forms.ToolStripButton();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.panel1.SuspendLayout();
            this.panelLeft.SuspendLayout();
            this.mainToolbar.SuspendLayout();
            this.SuspendLayout();
            // 
            // flexCelImgExport1
            // 
            this.flexCelImgExport1.AllVisibleSheets = false;
            this.flexCelImgExport1.PageSize = null;
            this.flexCelImgExport1.ResetPageNumberOnEachSheet = false;
            this.flexCelImgExport1.Resolution = 96D;
            this.flexCelImgExport1.Workbook = null;
            // 
            // openFileDialog
            // 
            this.openFileDialog.DefaultExt = "xls";
            this.openFileDialog.Filter = "Excel Files|*.xls|All files|*.*";
            this.openFileDialog.Title = "Select a file to preview";
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.MainPreview);
            this.panel1.Controls.Add(this.splitter1);
            this.panel1.Controls.Add(this.panelLeft);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 46);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(808, 375);
            this.panel1.TabIndex = 8;
            // 
            // MainPreview
            // 
            this.MainPreview.AutoScrollMinSize = new System.Drawing.Size(40, 383);
            this.MainPreview.Dock = System.Windows.Forms.DockStyle.Fill;
            this.MainPreview.Document = this.flexCelImgExport1;
            this.MainPreview.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            this.MainPreview.Location = new System.Drawing.Point(144, 0);
            this.MainPreview.Name = "MainPreview";
            this.MainPreview.PageXSeparation = 20;
            this.MainPreview.Size = new System.Drawing.Size(662, 373);
            this.MainPreview.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            this.MainPreview.StartPage = 1;
            this.MainPreview.TabIndex = 2;
            this.MainPreview.ThumbnailLarge = null;
            this.MainPreview.ThumbnailSmall = this.thumbs;
            this.MainPreview.StartPageChanged += new System.EventHandler(this.flexCelPreview1_StartPageChanged);
            this.MainPreview.ZoomChanged += new System.EventHandler(this.flexCelPreview1_ZoomChanged);
            // 
            // thumbs
            // 
            this.thumbs.AutoScrollMinSize = new System.Drawing.Size(20, 10);
            this.thumbs.Dock = System.Windows.Forms.DockStyle.Fill;
            this.thumbs.Document = this.flexCelImgExport1;
            this.thumbs.Location = new System.Drawing.Point(0, 115);
            this.thumbs.Name = "thumbs";
            this.thumbs.Size = new System.Drawing.Size(136, 258);
            this.thumbs.StartPage = 1;
            this.thumbs.TabIndex = 3;
            this.thumbs.ThumbnailLarge = this.MainPreview;
            this.thumbs.ThumbnailSmall = null;
            this.thumbs.Zoom = 0.1D;
            // 
            // splitter1
            // 
            this.splitter1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.splitter1.Location = new System.Drawing.Point(136, 0);
            this.splitter1.MinSize = 0;
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(8, 373);
            this.splitter1.TabIndex = 11;
            this.splitter1.TabStop = false;
            // 
            // panelLeft
            // 
            this.panelLeft.Controls.Add(this.cbAllSheets);
            this.panelLeft.Controls.Add(this.thumbs);
            this.panelLeft.Controls.Add(this.label2);
            this.panelLeft.Controls.Add(this.sheetSplitter);
            this.panelLeft.Controls.Add(this.lbSheets);
            this.panelLeft.Controls.Add(this.label1);
            this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelLeft.Location = new System.Drawing.Point(0, 0);
            this.panelLeft.Name = "panelLeft";
            this.panelLeft.Size = new System.Drawing.Size(136, 373);
            this.panelLeft.TabIndex = 9;
            // 
            // cbAllSheets
            // 
            this.cbAllSheets.Location = new System.Drawing.Point(16, 16);
            this.cbAllSheets.Name = "cbAllSheets";
            this.cbAllSheets.Size = new System.Drawing.Size(104, 16);
            this.cbAllSheets.TabIndex = 14;
            this.cbAllSheets.Text = "All Sheets";
            this.cbAllSheets.CheckedChanged += new System.EventHandler(this.cbAllSheets_CheckedChanged);
            // 
            // label2
            // 
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(0, 99);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(136, 16);
            this.label2.TabIndex = 13;
            this.label2.Text = "Thumbs";
            // 
            // sheetSplitter
            // 
            this.sheetSplitter.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.sheetSplitter.Dock = System.Windows.Forms.DockStyle.Top;
            this.sheetSplitter.Location = new System.Drawing.Point(0, 91);
            this.sheetSplitter.Name = "sheetSplitter";
            this.sheetSplitter.Size = new System.Drawing.Size(136, 8);
            this.sheetSplitter.TabIndex = 11;
            this.sheetSplitter.TabStop = false;
            // 
            // lbSheets
            // 
            this.lbSheets.Dock = System.Windows.Forms.DockStyle.Top;
            this.lbSheets.Items.AddRange(new object[] {
            "No open file"});
            this.lbSheets.Location = new System.Drawing.Point(0, 35);
            this.lbSheets.Name = "lbSheets";
            this.lbSheets.Size = new System.Drawing.Size(136, 56);
            this.lbSheets.TabIndex = 10;
            this.lbSheets.SelectedIndexChanged += new System.EventHandler(this.lbSheets_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(136, 35);
            this.label1.TabIndex = 12;
            this.label1.Text = "Sheets";
            // 
            // PdfSaveFileDialog
            // 
            this.PdfSaveFileDialog.DefaultExt = "pdf";
            this.PdfSaveFileDialog.Filter = "Pdf Files|*.pdf";
            this.PdfSaveFileDialog.Title = "Select the file to export to:";
            // 
            // mainToolbar
            // 
            this.mainToolbar.ImageScalingSize = new System.Drawing.Size(24, 24);
            this.mainToolbar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openFile,
            this.toolStripSeparator2,
            this.btnFirst,
            this.btnPrev,
            this.edPage,
            this.btnNext,
            this.btnLast,
            this.toolStripSeparator1,
            this.btnAutofit,
            this.btnZoomOut,
            this.edZoom,
            this.btnZoomIn,
            this.toolStripSeparator3,
            this.btnGridLines,
            this.btnHeadings,
            this.toolStripSeparator4,
            this.btnRecalc,
            this.btnPdf,
            this.btnClose});
            this.mainToolbar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
            this.mainToolbar.Location = new System.Drawing.Point(0, 0);
            this.mainToolbar.Name = "mainToolbar";
            this.mainToolbar.Size = new System.Drawing.Size(808, 46);
            this.mainToolbar.TabIndex = 14;
            // 
            // openFile
            // 
            this.openFile.Image = global::CustomPreview.Properties.Resources.open;
            this.openFile.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.openFile.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.openFile.Name = "openFile";
            this.openFile.Size = new System.Drawing.Size(61, 43);
            this.openFile.Text = "&Open File";
            this.openFile.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.openFile.ToolTipText = "Open an Excel file";
            this.openFile.Click += new System.EventHandler(this.openFile_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.AutoSize = false;
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(20, 46);
            // 
            // btnFirst
            // 
            this.btnFirst.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnFirst.Enabled = false;
            this.btnFirst.Image = ((System.Drawing.Image)(resources.GetObject("btnFirst.Image")));
            this.btnFirst.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnFirst.Name = "btnFirst";
            this.btnFirst.Size = new System.Drawing.Size(27, 43);
            this.btnFirst.Text = "<<";
            this.btnFirst.ToolTipText = "First page";
            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
            // 
            // btnPrev
            // 
            this.btnPrev.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnPrev.Enabled = false;
            this.btnPrev.Image = ((System.Drawing.Image)(resources.GetObject("btnPrev.Image")));
            this.btnPrev.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrev.Name = "btnPrev";
            this.btnPrev.Size = new System.Drawing.Size(23, 43);
            this.btnPrev.Text = "<";
            this.btnPrev.ToolTipText = "Previous page";
            this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
            // 
            // edPage
            // 
            this.edPage.AutoSize = false;
            this.edPage.Enabled = false;
            this.edPage.Name = "edPage";
            this.edPage.Size = new System.Drawing.Size(100, 18);
            this.edPage.TextBoxTextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.edPage.Leave += new System.EventHandler(this.edPage_Leave);
            this.edPage.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.edPage_KeyPress);
            // 
            // btnNext
            // 
            this.btnNext.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnNext.Enabled = false;
            this.btnNext.Image = ((System.Drawing.Image)(resources.GetObject("btnNext.Image")));
            this.btnNext.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(23, 43);
            this.btnNext.Text = ">";
            this.btnNext.ToolTipText = "Next page";
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // btnLast
            // 
            this.btnLast.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnLast.Enabled = false;
            this.btnLast.Image = ((System.Drawing.Image)(resources.GetObject("btnLast.Image")));
            this.btnLast.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnLast.Name = "btnLast";
            this.btnLast.Size = new System.Drawing.Size(27, 43);
            this.btnLast.Text = ">>";
            this.btnLast.ToolTipText = "Last page";
            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 46);
            // 
            // btnAutofit
            // 
            this.btnAutofit.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.noneToolStripMenuItem,
            this.fitToWidthToolStripMenuItem,
            this.fitToHeightToolStripMenuItem,
            this.fitToPageToolStripMenuItem});
            this.btnAutofit.Image = global::CustomPreview.Properties.Resources.autofit;
            this.btnAutofit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAutofit.Name = "btnAutofit";
            this.btnAutofit.Size = new System.Drawing.Size(76, 43);
            this.btnAutofit.Text = "No Autofit";
            this.btnAutofit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            // 
            // noneToolStripMenuItem
            // 
            this.noneToolStripMenuItem.Name = "noneToolStripMenuItem";
            this.noneToolStripMenuItem.Size = new System.Drawing.Size(140, 22);
            this.noneToolStripMenuItem.Text = "No Autofit";
            this.noneToolStripMenuItem.Click += new System.EventHandler(this.noneToolStripMenuItem_Click);
            // 
            // fitToWidthToolStripMenuItem
            // 
            this.fitToWidthToolStripMenuItem.Name = "fitToWidthToolStripMenuItem";
            this.fitToWidthToolStripMenuItem.Size = new System.Drawing.Size(140, 22);
            this.fitToWidthToolStripMenuItem.Text = "Fit to Width";
            this.fitToWidthToolStripMenuItem.Click += new System.EventHandler(this.fitToWidthToolStripMenuItem_Click);
            // 
            // fitToHeightToolStripMenuItem
            // 
            this.fitToHeightToolStripMenuItem.Name = "fitToHeightToolStripMenuItem";
            this.fitToHeightToolStripMenuItem.Size = new System.Drawing.Size(140, 22);
            this.fitToHeightToolStripMenuItem.Text = "Fit to Height";
            this.fitToHeightToolStripMenuItem.Click += new System.EventHandler(this.fitToHeightToolStripMenuItem_Click);
            // 
            // fitToPageToolStripMenuItem
            // 
            this.fitToPageToolStripMenuItem.Name = "fitToPageToolStripMenuItem";
            this.fitToPageToolStripMenuItem.Size = new System.Drawing.Size(140, 22);
            this.fitToPageToolStripMenuItem.Text = "Fit to Page";
            this.fitToPageToolStripMenuItem.Click += new System.EventHandler(this.fitToPageToolStripMenuItem_Click);
            // 
            // btnZoomOut
            // 
            this.btnZoomOut.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnZoomOut.Enabled = false;
            this.btnZoomOut.Image = ((System.Drawing.Image)(resources.GetObject("btnZoomOut.Image")));
            this.btnZoomOut.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnZoomOut.Name = "btnZoomOut";
            this.btnZoomOut.Size = new System.Drawing.Size(23, 43);
            this.btnZoomOut.Text = "-";
            this.btnZoomOut.ToolTipText = "Zoom out";
            this.btnZoomOut.Click += new System.EventHandler(this.btnZoomOut_Click);
            // 
            // edZoom
            // 
            this.edZoom.AutoSize = false;
            this.edZoom.Enabled = false;
            this.edZoom.Name = "edZoom";
            this.edZoom.Size = new System.Drawing.Size(40, 18);
            this.edZoom.TextBoxTextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.edZoom.Enter += new System.EventHandler(this.edZoom_Enter);
            this.edZoom.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.edZoom_KeyPress);
            // 
            // btnZoomIn
            // 
            this.btnZoomIn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnZoomIn.Enabled = false;
            this.btnZoomIn.Image = ((System.Drawing.Image)(resources.GetObject("btnZoomIn.Image")));
            this.btnZoomIn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnZoomIn.Name = "btnZoomIn";
            this.btnZoomIn.Size = new System.Drawing.Size(23, 43);
            this.btnZoomIn.Text = "+";
            this.btnZoomIn.ToolTipText = "Zoom in";
            this.btnZoomIn.Click += new System.EventHandler(this.btnZoomIn_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.AutoSize = false;
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(20, 46);
            // 
            // btnGridLines
            // 
            this.btnGridLines.CheckOnClick = true;
            this.btnGridLines.Enabled = false;
            this.btnGridLines.Image = global::CustomPreview.Properties.Resources.grid;
            this.btnGridLines.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnGridLines.Name = "btnGridLines";
            this.btnGridLines.Size = new System.Drawing.Size(57, 43);
            this.btnGridLines.Text = "&Gridlines";
            this.btnGridLines.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnGridLines.ToolTipText = "Show gridlines";
            this.btnGridLines.Click += new System.EventHandler(this.btnGridLines_Click);
            // 
            // btnHeadings
            // 
            this.btnHeadings.CheckOnClick = true;
            this.btnHeadings.Enabled = false;
            this.btnHeadings.Image = global::CustomPreview.Properties.Resources.Head;
            this.btnHeadings.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnHeadings.Name = "btnHeadings";
            this.btnHeadings.Size = new System.Drawing.Size(61, 43);
            this.btnHeadings.Text = "&Headings";
            this.btnHeadings.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnHeadings.ToolTipText = "Show the headings";
            this.btnHeadings.Click += new System.EventHandler(this.btnHeadings_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 46);
            // 
            // btnRecalc
            // 
            this.btnRecalc.Enabled = false;
            this.btnRecalc.Image = global::CustomPreview.Properties.Resources.calc;
            this.btnRecalc.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.btnRecalc.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRecalc.Name = "btnRecalc";
            this.btnRecalc.Size = new System.Drawing.Size(45, 43);
            this.btnRecalc.Text = "&Recalc";
            this.btnRecalc.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnRecalc.ToolTipText = "Recalculate the file";
            this.btnRecalc.Click += new System.EventHandler(this.btnRecalc_Click);
            // 
            // btnPdf
            // 
            this.btnPdf.Enabled = false;
            this.btnPdf.Image = global::CustomPreview.Properties.Resources.pdf;
            this.btnPdf.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.btnPdf.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPdf.Name = "btnPdf";
            this.btnPdf.Size = new System.Drawing.Size(79, 43);
            this.btnPdf.Text = "Export to &Pdf";
            this.btnPdf.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnPdf.ToolTipText = "Export the file to Pdf";
            this.btnPdf.Click += new System.EventHandler(this.btnPdf_Click);
            // 
            // btnClose
            // 
            this.btnClose.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.btnClose.Image = global::CustomPreview.Properties.Resources.close;
            this.btnClose.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(59, 43);
            this.btnClose.Text = "     E&xit     ";
            this.btnClose.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            this.btnClose.ToolTipText = "Exit from the application";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // mainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(808, 421);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.mainToolbar);
            this.Name = "mainForm";
            this.Text = "Custom Preview Demo";
            this.Load += new System.EventHandler(this.mainForm_Load);
            this.panel1.ResumeLayout(false);
            this.panelLeft.ResumeLayout(false);
            this.mainToolbar.ResumeLayout(false);
            this.mainToolbar.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private System.Windows.Forms.OpenFileDialog openFileDialog;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panelLeft;
        private System.Windows.Forms.ListBox lbSheets;
        private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.SaveFileDialog PdfSaveFileDialog;
        private System.Windows.Forms.CheckBox cbAllSheets;
        private System.Windows.Forms.Splitter sheetSplitter;
        private System.Windows.Forms.ToolTip toolTip1;
        private FlexCel.Render.FlexCelImgExport flexCelImgExport1;
        private FlexCel.Winforms.FlexCelPreview MainPreview;
        private FlexCel.Winforms.FlexCelPreview thumbs;
        private ToolStrip mainToolbar;
        private ToolStripButton openFile;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton btnRecalc;
        private ToolStripButton btnPdf;
        private ToolStripButton btnClose;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripButton btnFirst;
        private ToolStripButton btnPrev;
        private ToolStripTextBox edPage;
        private ToolStripButton btnNext;
        private ToolStripButton btnLast;
        private ToolStripButton btnZoomOut;
        private ToolStripTextBox edZoom;
        private ToolStripButton btnZoomIn;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripButton btnHeadings;
        private ToolStripButton btnGridLines;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripDropDownButton btnAutofit;
        private ToolStripMenuItem noneToolStripMenuItem;
        private ToolStripMenuItem fitToWidthToolStripMenuItem;
        private ToolStripMenuItem fitToHeightToolStripMenuItem;
        private ToolStripMenuItem fitToPageToolStripMenuItem;
    }
}


PasswordForm.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace CustomPreview
{
    /// <summary>
    /// Form for asking for a password when the file is password protected.
    /// </summary>
    public partial class PasswordForm: System.Windows.Forms.Form
    {

        public PasswordForm()
        {
            InitializeComponent();
        }

        public string Password
        {
            get
            {
                return PasswordEdit.Text;
            }
        }
    }
}

PasswordForm.Designer.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
namespace CustomPreview
{
    public partial class PasswordForm: System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox PasswordEdit;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.btnOk = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.PasswordEdit = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(24, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(240, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Please enter the password to open this file:";
            // 
            // btnOk
            // 
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOk.Location = new System.Drawing.Point(152, 112);
            this.btnOk.Name = "btnOk";
            this.btnOk.TabIndex = 1;
            this.btnOk.Text = "Ok";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(40, 64);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(64, 23);
            this.label3.TabIndex = 5;
            this.label3.Text = "Password:";
            // 
            // PasswordEdit
            // 
            this.PasswordEdit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            this.PasswordEdit.Location = new System.Drawing.Point(112, 64);
            this.PasswordEdit.Name = "PasswordEdit";
            this.PasswordEdit.PasswordChar = '*';
            this.PasswordEdit.Size = new System.Drawing.Size(200, 20);
            this.PasswordEdit.TabIndex = 0;
            this.PasswordEdit.Text = "";
            // 
            // PasswordForm
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(408, 154);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.PasswordEdit);
            this.Controls.Add(this.btnOk);
            this.Controls.Add(this.label1);
            this.Name = "PasswordForm";
            this.ShowInTaskbar = false;
            this.Text = "File is password protected.";
            this.ResumeLayout(false);

        }
        #endregion
    }
}


PdfProgressDialog.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

using System.Threading;

using FlexCel.Render;

namespace CustomPreview
{
    /// <summary>
    /// Shows progress as we are exporting to pdf.
    /// </summary>
    public partial class PdfProgressDialog: System.Windows.Forms.Form
    {
        private System.Timers.Timer timer1;

        public PdfProgressDialog()
        {
            InitializeComponent();
        }


        private DateTime StartTime;
        private Thread RunningThread;
        private FlexCelPdfExport PdfExport;

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            UpdateStatus();
        }

        public void ShowProgress(Thread aRunningThread, FlexCelPdfExport aPdfExport)
        {
            RunningThread = aRunningThread;

            if (!RunningThread.IsAlive) { DialogResult = DialogResult.OK; return; }
            timer1.Enabled = true;
            StartTime = DateTime.Now;
            PdfExport = aPdfExport;
            ShowDialog();
        }

        private void UpdateStatus()
        {
            TimeSpan ts = DateTime.Now - StartTime;
            string hours;
            if (ts.Hours == 0) hours = ""; else hours = ts.Hours.ToString("00") + ":";
            statusBarPanelTime.Text = hours + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");

            if (!RunningThread.IsAlive) DialogResult = DialogResult.OK;

            if (PdfExport.Progress.TotalPage > 0) labelPages.Text = String.Format("Generating Page {0} of {1}", PdfExport.Progress.Page, PdfExport.Progress.TotalPage);
        }

        private void PdfProgressDialog_Closed(object sender, System.EventArgs e)
        {
            timer1.Enabled = false;
        }


    }
}

PdfProgressDialog.Designer.cs

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using FlexCel.Render;
namespace CustomPreview
{
    public partial class PdfProgressDialog: System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.StatusBar statusBar1;
        private System.Windows.Forms.StatusBarPanel statusBarPanel1;
        private System.Windows.Forms.StatusBarPanel statusBarPanelTime;
        private System.Windows.Forms.Label labelPages;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnCancel = new System.Windows.Forms.Button();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
            this.statusBarPanelTime = new System.Windows.Forms.StatusBarPanel();
            this.labelPages = new System.Windows.Forms.Label();
            this.timer1 = new System.Timers.Timer();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelTime)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
            this.SuspendLayout();
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(184, 64);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 0;
            this.btnCancel.Text = "Cancel";
            // 
            // statusBar1
            // 
            this.statusBar1.Location = new System.Drawing.Point(0, 100);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                          this.statusBarPanel1,
                                                                                          this.statusBarPanelTime});
            this.statusBar1.ShowPanels = true;
            this.statusBar1.Size = new System.Drawing.Size(448, 22);
            this.statusBar1.TabIndex = 1;
            // 
            // statusBarPanel1
            // 
            this.statusBarPanel1.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
            this.statusBarPanel1.Text = "Elapsed Time:";
            this.statusBarPanel1.Width = 80;
            // 
            // statusBarPanelTime
            // 
            this.statusBarPanelTime.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
            this.statusBarPanelTime.Text = "0:00";
            // 
            // labelPages
            // 
            this.labelPages.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            this.labelPages.Location = new System.Drawing.Point(16, 16);
            this.labelPages.Name = "labelPages";
            this.labelPages.Size = new System.Drawing.Size(408, 16);
            this.labelPages.TabIndex = 2;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.SynchronizingObject = this;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
            // 
            // PdfProgressDialog
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(448, 122);
            this.ControlBox = false;
            this.Controls.Add(this.labelPages);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.btnCancel);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "PdfProgressDialog";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Please wait...";
            this.Closed += new System.EventHandler(this.PdfProgressDialog_Closed);
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanelTime)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion
    }
}


Program.cs

using System;
using System.Windows.Forms;

namespace CustomPreview
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] Args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new mainForm(Args));
        }
    }
}