diff --git a/Controls.DocumentViewer/DocumentViewer.vb b/Controls.DocumentViewer/DocumentViewer.vb index bf609268..694540c2 100644 --- a/Controls.DocumentViewer/DocumentViewer.vb +++ b/Controls.DocumentViewer/DocumentViewer.vb @@ -7,6 +7,7 @@ Imports DevExpress.Spreadsheet Imports GdPicture14 Imports DevExpress Imports DevExpress.Office.Utils +Imports System.IO Public Class DocumentViewer Private Enum ZoomMode @@ -83,9 +84,6 @@ Public Class DocumentViewer Exit Sub End If - GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer - GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter - _logger.Info("Loading File {0}", FilePath) DoLoadFile(FilePath) @@ -93,6 +91,21 @@ Public Class DocumentViewer UpdateMainUi() End Sub + Public Sub LoadFile(FileName As String, Stream As Stream) + If _licenseKey = String.Empty Then + _logger.Warn("License key was not provided. File [{0}] not loaded.", FileName) + Exit Sub + End If + + Dim oExtension As String = FileName.Substring(FileName.LastIndexOf(".")) + + _logger.Info("Loading File [{0}]", FileName) + + DoLoadFile(Stream, oExtension) + + UpdateMainUi() + End Sub + Public Sub CloseDocument() GdViewer.CloseDocument() UpdateMainUi() @@ -139,32 +152,13 @@ Public Class DocumentViewer RichEditControl1.Dock = DockStyle.Fill Case ".EML", ".DOC", ".DOCX", ".ODT", ".RTF", ".TXT" - Dim oFormat As XtraRichEdit.DocumentFormat = XtraRichEdit.DocumentFormat.Undefined - - Select Case oExtension.ToUpper - Case ".EML" : oFormat = XtraRichEdit.DocumentFormat.Mht - Case ".DOC" : oFormat = XtraRichEdit.DocumentFormat.Doc - Case ".DOCX" : oFormat = XtraRichEdit.DocumentFormat.OpenXml - Case ".ODT" : oFormat = XtraRichEdit.DocumentFormat.OpenDocument - Case ".RTF" : oFormat = XtraRichEdit.DocumentFormat.Rtf - Case ".TXT" : oFormat = XtraRichEdit.DocumentFormat.PlainText - End Select - - RichEditControl1.LoadDocument(FilePath, oFormat) + RichEditControl1.LoadDocument(FilePath, GetDocumentFormat(oExtension)) RichEditControl1.Visible = True RichEditControl1.Dock = DockStyle.Fill Case ".XLSX", ".XLS", "CSV" - Dim oFormat As Spreadsheet.DocumentFormat = Spreadsheet.DocumentFormat.Undefined - - Select Case oExtension.ToUpper - Case "XLSX" : oFormat = Spreadsheet.DocumentFormat.Xlsx - Case "XLS" : oFormat = Spreadsheet.DocumentFormat.Xls - Case "CSV" : oFormat = Spreadsheet.DocumentFormat.Csv - End Select - - SpreadsheetControl1.LoadDocument(FilePath, oFormat) + SpreadsheetControl1.LoadDocument(FilePath, GetSpreadsheetFormat(oExtension)) Dim oRange = SpreadsheetControl1.ActiveWorksheet.GetUsedRange() oRange.AutoFitColumns() @@ -175,13 +169,103 @@ Public Class DocumentViewer Case Else mainToolStrip.Visible = True + GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer + GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter + GdViewer.DisplayFromFile(FilePath) End Select + + UpdateMainUi() Catch ex As Exception _logger.Error(ex) End Try End Sub + Private Sub DoLoadFile(Stream As Stream, Extension As String) + Try + RichEditControl1.Visible = False + RichEditControl1.Dock = DockStyle.None + + SpreadsheetControl1.Visible = False + SpreadsheetControl1.Dock = DockStyle.None + + mainToolStrip.Visible = False + + Select Case Extension.ToUpper + Case ".MSG" + Dim oMsg As New Message(Stream) + + ' TODO: Improve Encoding, maybe convert based on encoding + oMsg.Encoding = System.Text.Encoding.UTF32 + Dim oMime = oMsg.ConvertToMimeMessage() + Dim oTempFileName = IO.Path.GetTempFileName() + oMime.Save(oTempFileName, True) + + RichEditControl1.LoadDocument(oTempFileName, XtraRichEdit.DocumentFormat.Mht) + + _TempFiles.Add(oTempFileName) + + RichEditControl1.Visible = True + RichEditControl1.Dock = DockStyle.Fill + + Case ".EML", ".DOC", ".DOCX", ".ODT", ".RTF", ".TXT" + RichEditControl1.LoadDocument(Stream, GetDocumentFormat(Extension)) + + RichEditControl1.Visible = True + RichEditControl1.Dock = DockStyle.Fill + + Case ".XLSX", ".XLS", "CSV" + SpreadsheetControl1.LoadDocument(Stream, GetSpreadsheetFormat(Extension)) + + Dim oRange = SpreadsheetControl1.ActiveWorksheet.GetUsedRange() + oRange.AutoFitColumns() + + SpreadsheetControl1.Visible = True + SpreadsheetControl1.Dock = DockStyle.Fill + + Case Else + mainToolStrip.Visible = True + + GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer + GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter + + GdViewer.DisplayFromStream(Stream) + End Select + + UpdateMainUi() + Catch ex As Exception + _logger.Error(ex) + End Try + End Sub + + Private Function GetSpreadsheetFormat(Extension) As Spreadsheet.DocumentFormat + Dim oFormat As Spreadsheet.DocumentFormat = Spreadsheet.DocumentFormat.Undefined + + Select Case Extension.ToUpper + Case "XLSX" : oFormat = Spreadsheet.DocumentFormat.Xlsx + Case "XLS" : oFormat = Spreadsheet.DocumentFormat.Xls + Case "CSV" : oFormat = Spreadsheet.DocumentFormat.Csv + End Select + + Return oFormat + End Function + + Private Function GetDocumentFormat(Extension) + Dim oFormat As XtraRichEdit.DocumentFormat = XtraRichEdit.DocumentFormat.Undefined + + Select Case Extension.ToUpper + Case ".EML" : oFormat = XtraRichEdit.DocumentFormat.Mht + Case ".DOC" : oFormat = XtraRichEdit.DocumentFormat.Doc + Case ".DOCX" : oFormat = XtraRichEdit.DocumentFormat.OpenXml + Case ".ODT" : oFormat = XtraRichEdit.DocumentFormat.OpenDocument + Case ".RTF" : oFormat = XtraRichEdit.DocumentFormat.Rtf + Case ".TXT" : oFormat = XtraRichEdit.DocumentFormat.PlainText + End Select + + Return oFormat + End Function + + Private Sub btnOpen_Click(sender As Object, e As EventArgs) GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter