Custom previewing (VB.Net / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\vb\VS2022\netframework\25.Printing and Exporting\20.CustomPreview and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/vb/VS2022/netframework/Modules/25.Printing and Exporting/20.CustomPreview
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.vb
Imports System.Reflection
Imports 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 - 2014 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("6.2.1.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.Designer.vb
Imports System.ComponentModel
Imports System.IO
Imports System.Drawing.Drawing2D
Imports System.Threading
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Winforms
Imports FlexCel.Render
Imports FlexCel.Pdf
Namespace CustomPreview
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.IContainer = Nothing
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#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 Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(mainForm))
Me.flexCelImgExport1 = New FlexCel.Render.FlexCelImgExport()
Me.openFileDialog = New System.Windows.Forms.OpenFileDialog()
Me.panel1 = New System.Windows.Forms.Panel()
Me.MainPreview = New FlexCel.Winforms.FlexCelPreview()
Me.thumbs = New FlexCel.Winforms.FlexCelPreview()
Me.splitter1 = New System.Windows.Forms.Splitter()
Me.panelLeft = New System.Windows.Forms.Panel()
Me.cbAllSheets = New System.Windows.Forms.CheckBox()
Me.label2 = New System.Windows.Forms.Label()
Me.sheetSplitter = New System.Windows.Forms.Splitter()
Me.lbSheets = New System.Windows.Forms.ListBox()
Me.label1 = New System.Windows.Forms.Label()
Me.PdfSaveFileDialog = New System.Windows.Forms.SaveFileDialog()
Me.toolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.mainToolbar = New System.Windows.Forms.ToolStrip()
Me.openFile = New System.Windows.Forms.ToolStripButton()
Me.toolStripSeparator2 = New System.Windows.Forms.ToolStripSeparator()
Me.btnFirst = New System.Windows.Forms.ToolStripButton()
Me.btnPrev = New System.Windows.Forms.ToolStripButton()
Me.edPage = New System.Windows.Forms.ToolStripTextBox()
Me.btnNext = New System.Windows.Forms.ToolStripButton()
Me.btnLast = New System.Windows.Forms.ToolStripButton()
Me.toolStripSeparator1 = New System.Windows.Forms.ToolStripSeparator()
Me.btnAutofit = New System.Windows.Forms.ToolStripDropDownButton()
Me.noneToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.fitToWidthToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.fitToHeightToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.fitToPageToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.btnZoomOut = New System.Windows.Forms.ToolStripButton()
Me.edZoom = New System.Windows.Forms.ToolStripTextBox()
Me.btnZoomIn = New System.Windows.Forms.ToolStripButton()
Me.toolStripSeparator3 = New System.Windows.Forms.ToolStripSeparator()
Me.btnGridLines = New System.Windows.Forms.ToolStripButton()
Me.btnHeadings = New System.Windows.Forms.ToolStripButton()
Me.toolStripSeparator4 = New System.Windows.Forms.ToolStripSeparator()
Me.btnRecalc = New System.Windows.Forms.ToolStripButton()
Me.btnPdf = New System.Windows.Forms.ToolStripButton()
Me.btnClose = New System.Windows.Forms.ToolStripButton()
Me.panel1.SuspendLayout()
Me.panelLeft.SuspendLayout()
Me.mainToolbar.SuspendLayout()
Me.SuspendLayout()
'
' flexCelImgExport1
'
Me.flexCelImgExport1.AllVisibleSheets = False
Me.flexCelImgExport1.PageSize = Nothing
Me.flexCelImgExport1.ResetPageNumberOnEachSheet = False
Me.flexCelImgExport1.Resolution = 96R
Me.flexCelImgExport1.Workbook = Nothing
'
' openFileDialog
'
Me.openFileDialog.DefaultExt = "xls"
Me.openFileDialog.Filter = "Excel Files|*.xls|All files|*.*"
Me.openFileDialog.Title = "Select a file to preview"
'
' panel1
'
Me.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.panel1.Controls.Add(Me.MainPreview)
Me.panel1.Controls.Add(Me.splitter1)
Me.panel1.Controls.Add(Me.panelLeft)
Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.panel1.Location = New System.Drawing.Point(0, 46)
Me.panel1.Name = "panel1"
Me.panel1.Size = New System.Drawing.Size(808, 375)
Me.panel1.TabIndex = 8
'
' MainPreview
'
Me.MainPreview.AutoScrollMinSize = New System.Drawing.Size(40, 383)
Me.MainPreview.Dock = System.Windows.Forms.DockStyle.Fill
Me.MainPreview.Document = Me.flexCelImgExport1
Me.MainPreview.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
Me.MainPreview.Location = New System.Drawing.Point(144, 0)
Me.MainPreview.Name = "MainPreview"
Me.MainPreview.PageXSeparation = 20
Me.MainPreview.Size = New System.Drawing.Size(662, 373)
Me.MainPreview.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Me.MainPreview.StartPage = 1
Me.MainPreview.TabIndex = 2
Me.MainPreview.ThumbnailLarge = Nothing
Me.MainPreview.ThumbnailSmall = Me.thumbs
' Me.MainPreview.StartPageChanged += New System.EventHandler(Me.flexCelPreview1_StartPageChanged)
' Me.MainPreview.ZoomChanged += New System.EventHandler(Me.flexCelPreview1_ZoomChanged)
'
' thumbs
'
Me.thumbs.AutoScrollMinSize = New System.Drawing.Size(20, 10)
Me.thumbs.Dock = System.Windows.Forms.DockStyle.Fill
Me.thumbs.Document = Me.flexCelImgExport1
Me.thumbs.Location = New System.Drawing.Point(0, 115)
Me.thumbs.Name = "thumbs"
Me.thumbs.Size = New System.Drawing.Size(136, 258)
Me.thumbs.StartPage = 1
Me.thumbs.TabIndex = 3
Me.thumbs.ThumbnailLarge = Me.MainPreview
Me.thumbs.ThumbnailSmall = Nothing
Me.thumbs.Zoom = 0.1R
'
' splitter1
'
Me.splitter1.BackColor = System.Drawing.SystemColors.ControlLightLight
Me.splitter1.Location = New System.Drawing.Point(136, 0)
Me.splitter1.MinSize = 0
Me.splitter1.Name = "splitter1"
Me.splitter1.Size = New System.Drawing.Size(8, 373)
Me.splitter1.TabIndex = 11
Me.splitter1.TabStop = False
'
' panelLeft
'
Me.panelLeft.Controls.Add(Me.cbAllSheets)
Me.panelLeft.Controls.Add(Me.thumbs)
Me.panelLeft.Controls.Add(Me.label2)
Me.panelLeft.Controls.Add(Me.sheetSplitter)
Me.panelLeft.Controls.Add(Me.lbSheets)
Me.panelLeft.Controls.Add(Me.label1)
Me.panelLeft.Dock = System.Windows.Forms.DockStyle.Left
Me.panelLeft.Location = New System.Drawing.Point(0, 0)
Me.panelLeft.Name = "panelLeft"
Me.panelLeft.Size = New System.Drawing.Size(136, 373)
Me.panelLeft.TabIndex = 9
'
' cbAllSheets
'
Me.cbAllSheets.Location = New System.Drawing.Point(16, 16)
Me.cbAllSheets.Name = "cbAllSheets"
Me.cbAllSheets.Size = New System.Drawing.Size(104, 16)
Me.cbAllSheets.TabIndex = 14
Me.cbAllSheets.Text = "All Sheets"
' Me.cbAllSheets.CheckedChanged += New System.EventHandler(Me.cbAllSheets_CheckedChanged)
'
' label2
'
Me.label2.Dock = System.Windows.Forms.DockStyle.Top
Me.label2.Location = New System.Drawing.Point(0, 99)
Me.label2.Name = "label2"
Me.label2.Size = New System.Drawing.Size(136, 16)
Me.label2.TabIndex = 13
Me.label2.Text = "Thumbs"
'
' sheetSplitter
'
Me.sheetSplitter.BackColor = System.Drawing.SystemColors.ControlLightLight
Me.sheetSplitter.Dock = System.Windows.Forms.DockStyle.Top
Me.sheetSplitter.Location = New System.Drawing.Point(0, 91)
Me.sheetSplitter.Name = "sheetSplitter"
Me.sheetSplitter.Size = New System.Drawing.Size(136, 8)
Me.sheetSplitter.TabIndex = 11
Me.sheetSplitter.TabStop = False
'
' lbSheets
'
Me.lbSheets.Dock = System.Windows.Forms.DockStyle.Top
Me.lbSheets.Items.AddRange(New Object() { "No open file"})
Me.lbSheets.Location = New System.Drawing.Point(0, 35)
Me.lbSheets.Name = "lbSheets"
Me.lbSheets.Size = New System.Drawing.Size(136, 56)
Me.lbSheets.TabIndex = 10
' Me.lbSheets.SelectedIndexChanged += New System.EventHandler(Me.lbSheets_SelectedIndexChanged)
'
' label1
'
Me.label1.Dock = System.Windows.Forms.DockStyle.Top
Me.label1.Location = New System.Drawing.Point(0, 0)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(136, 35)
Me.label1.TabIndex = 12
Me.label1.Text = "Sheets"
'
' PdfSaveFileDialog
'
Me.PdfSaveFileDialog.DefaultExt = "pdf"
Me.PdfSaveFileDialog.Filter = "Pdf Files|*.pdf"
Me.PdfSaveFileDialog.Title = "Select the file to export to:"
'
' mainToolbar
'
Me.mainToolbar.ImageScalingSize = New System.Drawing.Size(24, 24)
Me.mainToolbar.Items.AddRange(New System.Windows.Forms.ToolStripItem() { Me.openFile, Me.toolStripSeparator2, Me.btnFirst, Me.btnPrev, Me.edPage, Me.btnNext, Me.btnLast, Me.toolStripSeparator1, Me.btnAutofit, Me.btnZoomOut, Me.edZoom, Me.btnZoomIn, Me.toolStripSeparator3, Me.btnGridLines, Me.btnHeadings, Me.toolStripSeparator4, Me.btnRecalc, Me.btnPdf, Me.btnClose})
Me.mainToolbar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow
Me.mainToolbar.Location = New System.Drawing.Point(0, 0)
Me.mainToolbar.Name = "mainToolbar"
Me.mainToolbar.Size = New System.Drawing.Size(808, 46)
Me.mainToolbar.TabIndex = 14
'
' openFile
'
Me.openFile.Image = My.Resources.open
Me.openFile.ImageAlign = System.Drawing.ContentAlignment.TopCenter
Me.openFile.ImageTransparentColor = System.Drawing.Color.Magenta
Me.openFile.Name = "openFile"
Me.openFile.Size = New System.Drawing.Size(61, 43)
Me.openFile.Text = "&Open File"
Me.openFile.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.openFile.ToolTipText = "Open an Excel file"
' Me.openFile.Click += New System.EventHandler(Me.openFile_Click)
'
' toolStripSeparator2
'
Me.toolStripSeparator2.AutoSize = False
Me.toolStripSeparator2.Name = "toolStripSeparator2"
Me.toolStripSeparator2.Size = New System.Drawing.Size(20, 46)
'
' btnFirst
'
Me.btnFirst.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnFirst.Enabled = False
Me.btnFirst.Image = (CType(resources.GetObject("btnFirst.Image"), System.Drawing.Image))
Me.btnFirst.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnFirst.Name = "btnFirst"
Me.btnFirst.Size = New System.Drawing.Size(27, 43)
Me.btnFirst.Text = "<<"
Me.btnFirst.ToolTipText = "First page"
' Me.btnFirst.Click += New System.EventHandler(Me.btnFirst_Click)
'
' btnPrev
'
Me.btnPrev.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnPrev.Enabled = False
Me.btnPrev.Image = (CType(resources.GetObject("btnPrev.Image"), System.Drawing.Image))
Me.btnPrev.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnPrev.Name = "btnPrev"
Me.btnPrev.Size = New System.Drawing.Size(23, 43)
Me.btnPrev.Text = "<"
Me.btnPrev.ToolTipText = "Previous page"
' Me.btnPrev.Click += New System.EventHandler(Me.btnPrev_Click)
'
' edPage
'
Me.edPage.AutoSize = False
Me.edPage.Enabled = False
Me.edPage.Name = "edPage"
Me.edPage.Size = New System.Drawing.Size(100, 18)
Me.edPage.TextBoxTextAlign = System.Windows.Forms.HorizontalAlignment.Right
' Me.edPage.Leave += New System.EventHandler(Me.edPage_Leave)
' Me.edPage.KeyPress += New System.Windows.Forms.KeyPressEventHandler(Me.edPage_KeyPress)
'
' btnNext
'
Me.btnNext.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnNext.Enabled = False
Me.btnNext.Image = (CType(resources.GetObject("btnNext.Image"), System.Drawing.Image))
Me.btnNext.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnNext.Name = "btnNext"
Me.btnNext.Size = New System.Drawing.Size(23, 43)
Me.btnNext.Text = ">"
Me.btnNext.ToolTipText = "Next page"
' Me.btnNext.Click += New System.EventHandler(Me.btnNext_Click)
'
' btnLast
'
Me.btnLast.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnLast.Enabled = False
Me.btnLast.Image = (CType(resources.GetObject("btnLast.Image"), System.Drawing.Image))
Me.btnLast.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnLast.Name = "btnLast"
Me.btnLast.Size = New System.Drawing.Size(27, 43)
Me.btnLast.Text = ">>"
Me.btnLast.ToolTipText = "Last page"
' Me.btnLast.Click += New System.EventHandler(Me.btnLast_Click)
'
' toolStripSeparator1
'
Me.toolStripSeparator1.Name = "toolStripSeparator1"
Me.toolStripSeparator1.Size = New System.Drawing.Size(6, 46)
'
' btnAutofit
'
Me.btnAutofit.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() { Me.noneToolStripMenuItem, Me.fitToWidthToolStripMenuItem, Me.fitToHeightToolStripMenuItem, Me.fitToPageToolStripMenuItem})
Me.btnAutofit.Image = My.Resources.autofit
Me.btnAutofit.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnAutofit.Name = "btnAutofit"
Me.btnAutofit.Size = New System.Drawing.Size(76, 43)
Me.btnAutofit.Text = "No Autofit"
Me.btnAutofit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
'
' noneToolStripMenuItem
'
Me.noneToolStripMenuItem.Name = "noneToolStripMenuItem"
Me.noneToolStripMenuItem.Size = New System.Drawing.Size(140, 22)
Me.noneToolStripMenuItem.Text = "No Autofit"
' Me.noneToolStripMenuItem.Click += New System.EventHandler(Me.noneToolStripMenuItem_Click)
'
' fitToWidthToolStripMenuItem
'
Me.fitToWidthToolStripMenuItem.Name = "fitToWidthToolStripMenuItem"
Me.fitToWidthToolStripMenuItem.Size = New System.Drawing.Size(140, 22)
Me.fitToWidthToolStripMenuItem.Text = "Fit to Width"
' Me.fitToWidthToolStripMenuItem.Click += New System.EventHandler(Me.fitToWidthToolStripMenuItem_Click)
'
' fitToHeightToolStripMenuItem
'
Me.fitToHeightToolStripMenuItem.Name = "fitToHeightToolStripMenuItem"
Me.fitToHeightToolStripMenuItem.Size = New System.Drawing.Size(140, 22)
Me.fitToHeightToolStripMenuItem.Text = "Fit to Height"
' Me.fitToHeightToolStripMenuItem.Click += New System.EventHandler(Me.fitToHeightToolStripMenuItem_Click)
'
' fitToPageToolStripMenuItem
'
Me.fitToPageToolStripMenuItem.Name = "fitToPageToolStripMenuItem"
Me.fitToPageToolStripMenuItem.Size = New System.Drawing.Size(140, 22)
Me.fitToPageToolStripMenuItem.Text = "Fit to Page"
' Me.fitToPageToolStripMenuItem.Click += New System.EventHandler(Me.fitToPageToolStripMenuItem_Click)
'
' btnZoomOut
'
Me.btnZoomOut.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnZoomOut.Enabled = False
Me.btnZoomOut.Image = (CType(resources.GetObject("btnZoomOut.Image"), System.Drawing.Image))
Me.btnZoomOut.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnZoomOut.Name = "btnZoomOut"
Me.btnZoomOut.Size = New System.Drawing.Size(23, 43)
Me.btnZoomOut.Text = "-"
Me.btnZoomOut.ToolTipText = "Zoom out"
' Me.btnZoomOut.Click += New System.EventHandler(Me.btnZoomOut_Click)
'
' edZoom
'
Me.edZoom.AutoSize = False
Me.edZoom.Enabled = False
Me.edZoom.Name = "edZoom"
Me.edZoom.Size = New System.Drawing.Size(40, 18)
Me.edZoom.TextBoxTextAlign = System.Windows.Forms.HorizontalAlignment.Right
' Me.edZoom.Enter += New System.EventHandler(Me.edZoom_Enter)
' Me.edZoom.KeyPress += New System.Windows.Forms.KeyPressEventHandler(Me.edZoom_KeyPress)
'
' btnZoomIn
'
Me.btnZoomIn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
Me.btnZoomIn.Enabled = False
Me.btnZoomIn.Image = (CType(resources.GetObject("btnZoomIn.Image"), System.Drawing.Image))
Me.btnZoomIn.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnZoomIn.Name = "btnZoomIn"
Me.btnZoomIn.Size = New System.Drawing.Size(23, 43)
Me.btnZoomIn.Text = "+"
Me.btnZoomIn.ToolTipText = "Zoom in"
' Me.btnZoomIn.Click += New System.EventHandler(Me.btnZoomIn_Click)
'
' toolStripSeparator3
'
Me.toolStripSeparator3.AutoSize = False
Me.toolStripSeparator3.Name = "toolStripSeparator3"
Me.toolStripSeparator3.Size = New System.Drawing.Size(20, 46)
'
' btnGridLines
'
Me.btnGridLines.CheckOnClick = True
Me.btnGridLines.Enabled = False
Me.btnGridLines.Image = My.Resources.grid
Me.btnGridLines.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnGridLines.Name = "btnGridLines"
Me.btnGridLines.Size = New System.Drawing.Size(57, 43)
Me.btnGridLines.Text = "&Gridlines"
Me.btnGridLines.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.btnGridLines.ToolTipText = "Show gridlines"
' Me.btnGridLines.Click += New System.EventHandler(Me.btnGridLines_Click)
'
' btnHeadings
'
Me.btnHeadings.CheckOnClick = True
Me.btnHeadings.Enabled = False
Me.btnHeadings.Image = My.Resources.Head
Me.btnHeadings.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnHeadings.Name = "btnHeadings"
Me.btnHeadings.Size = New System.Drawing.Size(61, 43)
Me.btnHeadings.Text = "&Headings"
Me.btnHeadings.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.btnHeadings.ToolTipText = "Show the headings"
' Me.btnHeadings.Click += New System.EventHandler(Me.btnHeadings_Click)
'
' toolStripSeparator4
'
Me.toolStripSeparator4.Name = "toolStripSeparator4"
Me.toolStripSeparator4.Size = New System.Drawing.Size(6, 46)
'
' btnRecalc
'
Me.btnRecalc.Enabled = False
Me.btnRecalc.Image = My.Resources.calc
Me.btnRecalc.ImageAlign = System.Drawing.ContentAlignment.TopCenter
Me.btnRecalc.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnRecalc.Name = "btnRecalc"
Me.btnRecalc.Size = New System.Drawing.Size(45, 43)
Me.btnRecalc.Text = "&Recalc"
Me.btnRecalc.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.btnRecalc.ToolTipText = "Recalculate the file"
' Me.btnRecalc.Click += New System.EventHandler(Me.btnRecalc_Click)
'
' btnPdf
'
Me.btnPdf.Enabled = False
Me.btnPdf.Image = My.Resources.pdf
Me.btnPdf.ImageAlign = System.Drawing.ContentAlignment.TopCenter
Me.btnPdf.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnPdf.Name = "btnPdf"
Me.btnPdf.Size = New System.Drawing.Size(79, 43)
Me.btnPdf.Text = "Export to &Pdf"
Me.btnPdf.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.btnPdf.ToolTipText = "Export the file to Pdf"
' Me.btnPdf.Click += New System.EventHandler(Me.btnPdf_Click)
'
' btnClose
'
Me.btnClose.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.btnClose.Image = My.Resources.close
Me.btnClose.ImageAlign = System.Drawing.ContentAlignment.TopCenter
Me.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(59, 43)
Me.btnClose.Text = " E&xit "
Me.btnClose.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me.btnClose.ToolTipText = "Exit from the application"
' Me.btnClose.Click += New System.EventHandler(Me.btnClose_Click)
'
' mainForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(808, 421)
Me.Controls.Add(Me.panel1)
Me.Controls.Add(Me.mainToolbar)
Me.Name = "mainForm"
Me.Text = "Custom Preview Demo"
' Me.Load += New System.EventHandler(Me.mainForm_Load)
Me.panel1.ResumeLayout(False)
Me.panelLeft.ResumeLayout(False)
Me.mainToolbar.ResumeLayout(False)
Me.mainToolbar.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Private openFileDialog As System.Windows.Forms.OpenFileDialog
Private panel1 As System.Windows.Forms.Panel
Private panelLeft As System.Windows.Forms.Panel
Private WithEvents lbSheets As System.Windows.Forms.ListBox
Private splitter1 As System.Windows.Forms.Splitter
Private label1 As System.Windows.Forms.Label
Private label2 As System.Windows.Forms.Label
Private PdfSaveFileDialog As System.Windows.Forms.SaveFileDialog
Private WithEvents cbAllSheets As System.Windows.Forms.CheckBox
Private sheetSplitter As System.Windows.Forms.Splitter
Private toolTip1 As System.Windows.Forms.ToolTip
Private flexCelImgExport1 As FlexCel.Render.FlexCelImgExport
Private WithEvents MainPreview As FlexCel.Winforms.FlexCelPreview
Private thumbs As FlexCel.Winforms.FlexCelPreview
Private mainToolbar As ToolStrip
Private WithEvents openFile As ToolStripButton
Private toolStripSeparator1 As ToolStripSeparator
Private WithEvents btnRecalc As ToolStripButton
Private WithEvents btnPdf As ToolStripButton
Private WithEvents btnClose As ToolStripButton
Private toolStripSeparator2 As ToolStripSeparator
Private WithEvents btnFirst As ToolStripButton
Private WithEvents btnPrev As ToolStripButton
Private WithEvents edPage As ToolStripTextBox
Private WithEvents btnNext As ToolStripButton
Private WithEvents btnLast As ToolStripButton
Private WithEvents btnZoomOut As ToolStripButton
Private WithEvents edZoom As ToolStripTextBox
Private WithEvents btnZoomIn As ToolStripButton
Private toolStripSeparator3 As ToolStripSeparator
Private WithEvents btnHeadings As ToolStripButton
Private WithEvents btnGridLines As ToolStripButton
Private toolStripSeparator4 As ToolStripSeparator
Private btnAutofit As ToolStripDropDownButton
Private WithEvents noneToolStripMenuItem As ToolStripMenuItem
Private WithEvents fitToWidthToolStripMenuItem As ToolStripMenuItem
Private WithEvents fitToHeightToolStripMenuItem As ToolStripMenuItem
Private WithEvents fitToPageToolStripMenuItem As ToolStripMenuItem
End Class
End Namespace
Form1.vb
Imports System.ComponentModel
Imports System.IO
Imports System.Drawing.Drawing2D
Imports System.Threading
Imports FlexCel.Core
Imports FlexCel.XlsAdapter
Imports FlexCel.Winforms
Imports FlexCel.Render
Imports FlexCel.Pdf
Namespace CustomPreview
''' <summary>
''' Previewer of files.
''' </summary>
Partial Public Class mainForm
Inherits System.Windows.Forms.Form
Public Sub New()
Me.New(New String(){})
End Sub
Public Sub New(ByVal Args() As String)
InitializeComponent()
ResizeToolbar(mainToolbar)
If Args.Length > 0 Then
LoadFile(Args(0))
End If
If ExcelFile.SupportsXlsx Then
Me.openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Excel 97/2003|*.xls|Excel 2007|*.xlsx;*.xlsm|All files|*.*"
End If
MainPreview.CenteredPreview = True
thumbs.CenteredPreview = True
End Sub
Private Sub ResizeToolbar(ByVal toolbar As ToolStrip)
Using gr As Graphics = CreateGraphics()
Dim xFactor As Double = gr.DpiX / 96.0
Dim yFactor As Double = gr.DpiY / 96.0
toolbar.ImageScalingSize = New Size(CInt(Fix(24 * xFactor)), CInt(Fix(24 * yFactor)))
toolbar.Width = 0 'force a recalc of the buttons.
End Using
End Sub
Private Sub UpdatePages()
edPage.Text = String.Format("{0} of {1}", MainPreview.StartPage, MainPreview.TotalPages)
End Sub
Private Sub flexCelPreview1_StartPageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MainPreview.StartPageChanged
UpdatePages()
End Sub
Private Sub ChangePages()
Dim s As String = edPage.Text.Trim()
Dim pos As Integer = 0
Do While pos < s.Length AndAlso s.Chars(pos) >= "0"c AndAlso s.Chars(pos) <= "9"c
pos += 1
Loop
If pos > 0 Then
Dim page As Integer = MainPreview.StartPage
Try
page = Convert.ToInt32(s.Substring(0, pos))
Catch e1 As Exception
End Try
MainPreview.StartPage = page
End If
UpdatePages()
End Sub
Private Sub edPage_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles edPage.Leave
ChangePages()
End Sub
Private Sub edPage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles edPage.KeyPress
If e.KeyChar = ChrW(13) Then
ChangePages()
End If
If e.KeyChar = ChrW(27) Then
UpdatePages()
End If
End Sub
Private Sub flexCelPreview1_ZoomChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MainPreview.ZoomChanged
UpdateZoom()
End Sub
Private Sub UpdateZoom()
edZoom.Text = String.Format("{0}%", CInt(Fix(Math.Round(MainPreview.Zoom * 100))))
If MainPreview.AutofitPreview = TAutofitPreview.None Then
UpdateAutofitText()
End If
End Sub
Private Sub ChangeZoom()
Dim s As String = edZoom.Text.Trim()
Dim pos As Integer = 0
Do While pos < s.Length AndAlso s.Chars(pos) >= "0"c AndAlso s.Chars(pos) <= "9"c
pos += 1
Loop
If pos > 0 Then
Dim zoom As Integer = CInt(Fix(Math.Round(MainPreview.Zoom * 100)))
Try
zoom = Convert.ToInt32(s.Substring(0, pos))
Catch e1 As Exception
End Try
MainPreview.Zoom = zoom / 100.0
End If
UpdateZoom()
End Sub
Private Sub edZoom_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles edZoom.KeyPress
If e.KeyChar = ChrW(13) Then
ChangeZoom()
End If
If e.KeyChar = ChrW(27) Then
UpdateZoom()
End If
End Sub
Private Sub edZoom_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles edZoom.Enter
ChangeZoom()
End Sub
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
Private Sub openFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles openFile.Click
If openFileDialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End If
LoadFile(openFileDialog.FileName)
End Sub
'The event that will actually provide the password to open the empty form.
Private Sub GetPassword(ByVal e As OnPasswordEventArgs)
Dim Pwd As New PasswordForm()
e.Password = String.Empty
If Pwd.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End If
e.Password = Pwd.Password
End Sub
Friend Sub LoadFile(ByVal FileName As String)
openFileDialog.FileName = FileName
lbSheets.Items.Clear()
Dim xls As New XlsFile()
xls.HeadingColWidth = -1
xls.HeadingRowHeight = -1
AddHandler xls.Protection.OnPassword, AddressOf GetPassword
xls.Open(FileName)
For i As Integer = 1 To xls.SheetCount
lbSheets.Items.Add(xls.GetSheetName(i))
Next 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
End Sub
Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
MainPreview.StartPage = 1
End Sub
Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click
MainPreview.StartPage -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
MainPreview.StartPage += 1
End Sub
Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
MainPreview.StartPage = MainPreview.TotalPages
End Sub
Private Sub btnZoomOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnZoomOut.Click
MainPreview.Zoom -= 0.1
End Sub
Private Sub btnZoomIn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnZoomIn.Click
MainPreview.Zoom += 0.1
End Sub
Private Sub lbSheets_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSheets.SelectedIndexChanged
If flexCelImgExport1.Workbook Is Nothing Then
Return
End If
If lbSheets.Items.Count > flexCelImgExport1.Workbook.SheetCount Then
Return
End If
flexCelImgExport1.Workbook.ActiveSheet = lbSheets.SelectedIndex + 1
MainPreview.InvalidatePreview()
End Sub
Private Sub btnPdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPdf.Click
If flexCelImgExport1.Workbook Is Nothing Then
MessageBox.Show("There is no open file")
Return
End If
If PdfSaveFileDialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End If
Using PdfExport As New FlexCelPdfExport(flexCelImgExport1.Workbook, True)
If Not DoExportToPdf(PdfExport) Then
Return
End If
End Using
If MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> System.Windows.Forms.DialogResult.Yes Then
Return
End If
Process.Start(PdfSaveFileDialog.FileName)
End Sub
Private Function DoExportToPdf(ByVal PdfExport As FlexCelPdfExport) As Boolean
Dim MyPdfThread As New PdfThread(PdfExport, PdfSaveFileDialog.FileName, cbAllSheets.Checked)
Dim PdfExportThread As New Thread(New ThreadStart(AddressOf MyPdfThread.ExportToPdf))
PdfExportThread.Start()
Using Pg As New PdfProgressDialog()
Pg.ShowProgress(PdfExportThread, PdfExport)
If Pg.DialogResult <> System.Windows.Forms.DialogResult.OK Then
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
End If
If MyPdfThread IsNot Nothing AndAlso MyPdfThread.MainException IsNot Nothing Then
Throw MyPdfThread.MainException
End If
End Using
Return True
End Function
Private Sub cbAllSheets_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbAllSheets.CheckedChanged
lbSheets.Visible = Not cbAllSheets.Checked
sheetSplitter.Visible = lbSheets.Visible
flexCelImgExport1.AllVisibleSheets = cbAllSheets.Checked
If flexCelImgExport1.Workbook Is Nothing Then
Return
End If
MainPreview.InvalidatePreview()
End Sub
Private Sub btnRecalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRecalc.Click
If flexCelImgExport1.Workbook Is Nothing Then
MessageBox.Show("Please open a file before recalculating.")
Return
End If
flexCelImgExport1.Workbook.Recalc(True)
MainPreview.InvalidatePreview()
End Sub
Private Sub mainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnHeadings_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHeadings.Click
Dim xls As ExcelFile = flexCelImgExport1.Workbook
If xls Is Nothing Then
Return
End If
If cbAllSheets.Checked Then
Dim SaveActiveSheet As Integer = xls.ActiveSheet
For sheet As Integer = 1 To xls.SheetCount
xls.ActiveSheet = sheet
xls.PrintHeadings = btnHeadings.Checked
Next sheet
xls.ActiveSheet = SaveActiveSheet
Else
xls.PrintHeadings = btnHeadings.Checked
End If
MainPreview.InvalidatePreview()
End Sub
Private Sub btnGridLines_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGridLines.Click
Dim xls As ExcelFile = flexCelImgExport1.Workbook
If xls Is Nothing Then
Return
End If
If cbAllSheets.Checked Then
Dim SaveActiveSheet As Integer = xls.ActiveSheet
For sheet As Integer = 1 To xls.SheetCount
xls.ActiveSheet = sheet
xls.PrintGridLines = btnGridLines.Checked
Next sheet
xls.ActiveSheet = SaveActiveSheet
Else
xls.PrintGridLines = btnGridLines.Checked
End If
MainPreview.InvalidatePreview()
End Sub
Private Sub noneToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles noneToolStripMenuItem.Click
MainPreview.AutofitPreview = TAutofitPreview.None
UpdateAutofitText()
End Sub
Private Sub fitToWidthToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles fitToWidthToolStripMenuItem.Click
MainPreview.AutofitPreview = TAutofitPreview.Width
UpdateAutofitText()
End Sub
Private Sub fitToHeightToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles fitToHeightToolStripMenuItem.Click
MainPreview.AutofitPreview = TAutofitPreview.Height
UpdateAutofitText()
End Sub
Private Sub fitToPageToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles fitToPageToolStripMenuItem.Click
MainPreview.AutofitPreview = TAutofitPreview.Full
UpdateAutofitText()
End Sub
Private Sub UpdateAutofitText()
Select Case MainPreview.AutofitPreview
Case TAutofitPreview.None
btnAutofit.Text = "No Autofit"
Case TAutofitPreview.Width
btnAutofit.Text = "Fit to Width"
Case TAutofitPreview.Height
btnAutofit.Text = "Fit to Height"
Case TAutofitPreview.Full
btnAutofit.Text = "Fit to Page"
Case Else
End Select
End Sub
End Class
#Region "PdfThread"
Friend Class PdfThread
Private PdfExport As FlexCelPdfExport
Private FileName As String
Private AllVisibleSheets As Boolean
Private FMainException As Exception
Friend Sub New(ByVal aPdfExport As FlexCelPdfExport, ByVal aFileName As String, ByVal aAllVisibleSheets As Boolean)
PdfExport = aPdfExport
FileName = aFileName
AllVisibleSheets = aAllVisibleSheets
End Sub
Friend Sub ExportToPdf()
Try
If AllVisibleSheets Then
Try
Using f As New FileStream(FileName, FileMode.Create, FileAccess.Write)
PdfExport.BeginExport(f)
PdfExport.PageLayout = TPageLayout.Outlines
PdfExport.ExportAllVisibleSheets(False, System.IO.Path.GetFileNameWithoutExtension(FileName))
PdfExport.EndExport()
End Using
Catch
Try
File.Delete(FileName)
Catch
'Not here.
End Try
Throw
End Try
Else
PdfExport.PageLayout = TPageLayout.None
PdfExport.Export(FileName)
End If
Catch ex As Exception
FMainException = ex
End Try
End Sub
Friend ReadOnly Property MainException() As Exception
Get
Return FMainException
End Get
End Property
End Class
#End Region
End Namespace
PasswordForm.Designer.vb
Imports System.ComponentModel
Namespace CustomPreview
Partial Public Class PasswordForm
Inherits System.Windows.Forms.Form
Private label1 As System.Windows.Forms.Label
Private btnOk As System.Windows.Forms.Button
Private label3 As System.Windows.Forms.Label
Private PasswordEdit As System.Windows.Forms.TextBox
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#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 Sub InitializeComponent()
Me.label1 = New System.Windows.Forms.Label()
Me.btnOk = New System.Windows.Forms.Button()
Me.label3 = New System.Windows.Forms.Label()
Me.PasswordEdit = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
' label1
'
Me.label1.Location = New System.Drawing.Point(24, 16)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(240, 23)
Me.label1.TabIndex = 0
Me.label1.Text = "Please enter the password to open this file:"
'
' btnOk
'
Me.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOk.Location = New System.Drawing.Point(152, 112)
Me.btnOk.Name = "btnOk"
Me.btnOk.TabIndex = 1
Me.btnOk.Text = "Ok"
'
' label3
'
Me.label3.Location = New System.Drawing.Point(40, 64)
Me.label3.Name = "label3"
Me.label3.Size = New System.Drawing.Size(64, 23)
Me.label3.TabIndex = 5
Me.label3.Text = "Password:"
'
' PasswordEdit
'
Me.PasswordEdit.Anchor = (CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
Me.PasswordEdit.Location = New System.Drawing.Point(112, 64)
Me.PasswordEdit.Name = "PasswordEdit"
Me.PasswordEdit.PasswordChar = "*"c
Me.PasswordEdit.Size = New System.Drawing.Size(200, 20)
Me.PasswordEdit.TabIndex = 0
Me.PasswordEdit.Text = ""
'
' PasswordForm
'
Me.AcceptButton = Me.btnOk
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(408, 154)
Me.Controls.Add(Me.label3)
Me.Controls.Add(Me.PasswordEdit)
Me.Controls.Add(Me.btnOk)
Me.Controls.Add(Me.label1)
Me.Name = "PasswordForm"
Me.ShowInTaskbar = False
Me.Text = "File is password protected."
Me.ResumeLayout(False)
End Sub
#End Region
End Class
End Namespace
PasswordForm.vb
Imports System.ComponentModel
Namespace CustomPreview
''' <summary>
''' Form for asking for a password when the file is password protected.
''' </summary>
Partial Public Class PasswordForm
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
Public ReadOnly Property Password() As String
Get
Return PasswordEdit.Text
End Get
End Property
End Class
End Namespace
PdfProgressDialog.Designer.vb
Imports System.ComponentModel
Imports System.Threading
Imports FlexCel.Render
Namespace CustomPreview
Partial Public Class PdfProgressDialog
Inherits System.Windows.Forms.Form
Private btnCancel As System.Windows.Forms.Button
Private statusBar1 As System.Windows.Forms.StatusBar
Private statusBarPanel1 As System.Windows.Forms.StatusBarPanel
Private statusBarPanelTime As System.Windows.Forms.StatusBarPanel
Private labelPages As System.Windows.Forms.Label
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing
''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#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 Sub InitializeComponent()
Me.btnCancel = New System.Windows.Forms.Button()
Me.statusBar1 = New System.Windows.Forms.StatusBar()
Me.statusBarPanel1 = New System.Windows.Forms.StatusBarPanel()
Me.statusBarPanelTime = New System.Windows.Forms.StatusBarPanel()
Me.labelPages = New System.Windows.Forms.Label()
Me.timer1 = New System.Timers.Timer()
CType(Me.statusBarPanel1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.statusBarPanelTime, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.timer1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' btnCancel
'
Me.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Location = New System.Drawing.Point(184, 64)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.TabIndex = 0
Me.btnCancel.Text = "Cancel"
'
' statusBar1
'
Me.statusBar1.Location = New System.Drawing.Point(0, 100)
Me.statusBar1.Name = "statusBar1"
Me.statusBar1.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() { Me.statusBarPanel1, Me.statusBarPanelTime})
Me.statusBar1.ShowPanels = True
Me.statusBar1.Size = New System.Drawing.Size(448, 22)
Me.statusBar1.TabIndex = 1
'
' statusBarPanel1
'
Me.statusBarPanel1.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None
Me.statusBarPanel1.Text = "Elapsed Time:"
Me.statusBarPanel1.Width = 80
'
' statusBarPanelTime
'
Me.statusBarPanelTime.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None
Me.statusBarPanelTime.Text = "0:00"
'
' labelPages
'
Me.labelPages.Anchor = (CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles))
Me.labelPages.Location = New System.Drawing.Point(16, 16)
Me.labelPages.Name = "labelPages"
Me.labelPages.Size = New System.Drawing.Size(408, 16)
Me.labelPages.TabIndex = 2
'
' timer1
'
Me.timer1.Enabled = True
Me.timer1.SynchronizingObject = Me
' Me.timer1.Elapsed += New System.Timers.ElapsedEventHandler(Me.timer1_Elapsed)
'
' PdfProgressDialog
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(448, 122)
Me.ControlBox = False
Me.Controls.Add(Me.labelPages)
Me.Controls.Add(Me.statusBar1)
Me.Controls.Add(Me.btnCancel)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "PdfProgressDialog"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Please wait..."
' Me.Closed += New System.EventHandler(Me.PdfProgressDialog_Closed)
CType(Me.statusBarPanel1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.statusBarPanelTime, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.timer1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
End Class
End Namespace
PdfProgressDialog.vb
Imports System.ComponentModel
Imports System.Threading
Imports FlexCel.Render
Namespace CustomPreview
''' <summary>
''' Shows progress as we are exporting to pdf.
''' </summary>
Partial Public Class PdfProgressDialog
Inherits System.Windows.Forms.Form
Private WithEvents timer1 As System.Timers.Timer
Public Sub New()
InitializeComponent()
End Sub
Private StartTime As Date
Private RunningThread As Thread
Private PdfExport As FlexCelPdfExport
Private Sub timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
UpdateStatus()
End Sub
Public Sub ShowProgress(ByVal aRunningThread As Thread, ByVal aPdfExport As FlexCelPdfExport)
RunningThread = aRunningThread
If Not RunningThread.IsAlive Then
DialogResult = System.Windows.Forms.DialogResult.OK
Return
End If
timer1.Enabled = True
StartTime = Date.Now
PdfExport = aPdfExport
ShowDialog()
End Sub
Private Sub UpdateStatus()
Dim ts As TimeSpan = Date.Now.Subtract(StartTime)
Dim hours As String
If ts.Hours = 0 Then
hours = ""
Else
hours = ts.Hours.ToString("00") & ":"
End If
statusBarPanelTime.Text = hours & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00")
If Not RunningThread.IsAlive Then
DialogResult = System.Windows.Forms.DialogResult.OK
End If
If PdfExport.Progress.TotalPage > 0 Then
labelPages.Text = String.Format("Generating Page {0} of {1}", PdfExport.Progress.Page, PdfExport.Progress.TotalPage)
End If
End Sub
Private Sub PdfProgressDialog_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
timer1.Enabled = False
End Sub
End Class
End Namespace
Program.vb
Namespace CustomPreview
Friend NotInheritable Class Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main(ByVal Args() As String)
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New mainForm(Args))
End Sub
End Class
End Namespace