Monorepo/Controls.DocumentViewer/DocumentViewer.vb

484 lines
17 KiB
VB.net

Imports System.ComponentModel
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Logging
Imports Independentsoft.Msg
Imports DevExpress.Spreadsheet
Imports GdPicture14
Imports DevExpress
Imports DevExpress.Office.Utils
Imports System.IO
Public Class DocumentViewer
Private Enum ZoomMode
Zoom50
Zoom100
Zoom150
Zoom200
ZoomSelectedArea
ZoomFitToViewer
ZoomFitWidth
ZoomFitHeight
End Enum
Private _currentSearchOccurence As Integer = 0
Private _toggleGamma As Boolean = True
Private _licenseKey As String = String.Empty
Private _licenseManager As New GdPicture14.LicenseManager()
Private _logConfig As LogConfig
Private _logger As Logger
Private _hide_file_info_from_user As Boolean = False
Private _FilePath As String
Private _Fileinfo As FileInfo
' List of all created temp files when converting msg files
Private _TempFiles As New List(Of String)
Private Sub DocumentViewer_Load(sender As Object, e As EventArgs) Handles Me.Load
_TempFiles.Clear()
UpdateMainUi()
End Sub
Public Property FileLoaded As Boolean = False
''' <summary>
''' Initialize the Viewer
''' </summary>
''' <param name="LogConfig">A LogConfig object</param>
''' <param name="LicenseKey">The GDPicture.NET License Key</param>
Public Sub Init(LogConfig As LogConfig, LicenseKey As String)
_logConfig = LogConfig
_logger = LogConfig.GetLogger()
_licenseKey = LicenseKey
_licenseManager.RegisterKEY(_licenseKey)
End Sub
''' <summary>
''' Terminate Viewer, freeing up resources and deleting temp files
''' </summary>
Public Sub Done()
DeleteTempFiles()
FreeFile()
End Sub
''' <summary>
''' Load a file and display it
''' </summary>
''' <param name="FilePath"></param>
Public Sub LoadFile(FilePath As String)
FileLoaded = False
If _licenseKey = String.Empty Then
_logger.Warn("License key was not provided. File {0} not loaded.", FilePath)
Exit Sub
End If
_logger.Info("Loading File {0}", FilePath)
Dim oFileLoaded = DoLoadFile(FilePath)
_FilePath = FilePath
_Fileinfo = New FileInfo(FilePath)
If oFileLoaded = True Then
FileLoaded = True
End If
UpdateMainUi()
End Sub
Public Sub LoadFile(FileName As String, Stream As Stream)
FileLoaded = False
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)
Dim ofileloaded = DoLoadFile(Stream, oExtension)
If oFileLoaded = True Then
FileLoaded = True
End If
UpdateMainUi()
End Sub
Public Sub CloseDocument()
GdViewer.CloseDocument()
UpdateMainUi()
End Sub
Public Sub DeleteTempFiles()
For Each oFile In _TempFiles
Try
IO.File.Delete(oFile)
Catch ex As Exception
_logger.Warn("Could not delete temp file {0}", oFile)
End Try
Next
_TempFiles.Clear()
End Sub
Private Sub FreeFile()
Try
If Len(_FilePath) OrElse _Fileinfo Is Nothing Then
Exit Sub
End If
Dim oExtension As String = _Fileinfo.Extension.ToUpper
Select Case oExtension.ToUpper
Case ".MSG"
RichEditControl1.CreateNewDocument()
Case ".EML", ".DOC", ".DOCX", ".ODT", ".RTF", ".TXT"
RichEditControl1.CreateNewDocument()
Case ".XLSX", ".XLS", "CSV"
SpreadsheetControl1.CreateNewDocument()
Case Else
GdViewer.CloseDocument()
End Select
Catch ex As Exception
_logger.Warn($"Unexpected error in FreeFile: {ex.Message}")
End Try
End Sub
Private Function DoLoadFile(FilePath As String) As Boolean
Try
Dim oFileInfo = New IO.FileInfo(FilePath)
Dim oExtension As String = oFileInfo.Extension.ToUpper
RichEditControl1.Visible = False
RichEditControl1.Dock = DockStyle.None
SpreadsheetControl1.Visible = False
SpreadsheetControl1.Dock = DockStyle.None
ToolbarDocumentViewer.Visible = False
Select Case oExtension.ToUpper
Case ".MSG"
Dim oMsg As New Message(FilePath)
' 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(FilePath, GetDocumentFormat(oExtension))
RichEditControl1.Visible = True
RichEditControl1.Dock = DockStyle.Fill
Case ".XLSX", ".XLS", "CSV"
SpreadsheetControl1.LoadDocument(FilePath, GetSpreadsheetFormat(oExtension))
Dim oRange = SpreadsheetControl1.ActiveWorksheet.GetUsedRange()
oRange.AutoFitColumns()
SpreadsheetControl1.Visible = True
SpreadsheetControl1.Dock = DockStyle.Fill
Case Else
ToolbarDocumentViewer.Visible = True
GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer
GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter
GdViewer.DisplayFromFile(FilePath)
End Select
UpdateMainUi()
Return True
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
Private Function DoLoadFile(Stream As Stream, Extension As String) As Boolean
Try
RichEditControl1.Visible = False
RichEditControl1.Dock = DockStyle.None
SpreadsheetControl1.Visible = False
SpreadsheetControl1.Dock = DockStyle.None
ToolbarDocumentViewer.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
ToolbarDocumentViewer.Visible = True
GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer
GdViewer.DocumentAlignment = ViewerDocumentAlignment.DocumentAlignmentTopCenter
GdViewer.DisplayFromStream(Stream)
End Select
UpdateMainUi()
Return True
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
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) As XtraRichEdit.DocumentFormat
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
If OpenFileDialog.ShowDialog() = DialogResult.OK Then
GdViewer.DisplayFromFile(OpenFileDialog.FileName)
End If
GdViewer.Focus()
UpdateMainUi()
End Sub
''' <summary>
''' Configures the viewer to hide the file path to the end-user.
''' </summary>
''' <param name="ViewOnly">
''' True means that all file info should be hidden from the end-user
''' False means the end user may see the filepath or other info about the file
''' </param>
Public Sub SetViewOnly(ViewOnly As Boolean)
If ViewOnly Then
buttonPrint.Visibility = XtraBars.BarItemVisibility.Never
Else
buttonPrint.Visibility = XtraBars.BarItemVisibility.Always
End If
_hide_file_info_from_user = ViewOnly
End Sub
Public Sub SetAllowAnnotations(AllowAnnotations As Boolean)
End Sub
''' <summary>
''' DEPRECATED: Use SetViewOnly
''' </summary>
Public Sub RightViewOnly(ViewOnly As Boolean)
SetViewOnly(ViewOnly)
End Sub
''' <summary>
''' DEPRECATED: Use SetViewOnly
''' </summary>
Public Sub RightOnlyView(ViewOnly As Boolean)
SetViewOnly(ViewOnly)
End Sub
Private Sub UpdateMainUi()
End Sub
Private Sub btnFirstPage_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonFirstPage.ItemClick
GdViewer.DisplayFirstPage()
End Sub
Private Sub btnPreviousPage_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonPrevPage.ItemClick
GdViewer.DisplayPreviousPage()
End Sub
Private Sub btnNextPage_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonNextPage.ItemClick
GdViewer.DisplayNextPage()
End Sub
Private Sub btnLastPage_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonLastPage.ItemClick
GdViewer.DisplayLastPage()
End Sub
Private Sub tbCurrentPage_Leave(ByVal sender As System.Object, ByVal e As EventArgs) Handles txtCurrentPage.EditValueChanged
Dim page As Integer = 0
If Integer.TryParse(txtCurrentPage.EditValue, page) Then
If page > 0 And page <= GdViewer.PageCount Then
GdViewer.DisplayPage(page)
UpdateaNavigationToolbar()
End If
End If
End Sub
Private Sub GdViewer1_PageChanged() Handles GdViewer.PageChanged
UpdateaNavigationToolbar()
End Sub
Private Sub GdViewer1_AfterZoomChange() Handles GdViewer.AfterZoomChange
UpdateaNavigationToolbar()
If GdViewer.MouseMode = ViewerMouseMode.MouseModeAreaZooming Then
GdViewer.MouseMode = ViewerMouseMode.MouseModePan
End If
End Sub
Private Sub btnZoomOut_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonZoomOut.ItemClick
GdViewer.ZoomOUT()
End Sub
Private Sub btnZoomIn_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonZoomIn.ItemClick
GdViewer.ZoomIN()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonPrint.ItemClick
If GdViewer.PageCount = 0 Then
Return
End If
GdViewer.PrintDialog()
End Sub
Private Sub btnRotateLeft_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonRotateLeft.ItemClick
GdViewer.Rotate(RotateFlipType.Rotate270FlipNone)
End Sub
Private Sub btnRotateRight_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonRotateRight.ItemClick
GdViewer.Rotate(RotateFlipType.Rotate90FlipNone)
End Sub
Private Sub btnFlipX_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonFlipX.ItemClick
GdViewer.Rotate(RotateFlipType.RotateNoneFlipX)
End Sub
Private Sub btnFlipY_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles buttonFlipY.ItemClick
GdViewer.Rotate(RotateFlipType.RotateNoneFlipY)
End Sub
Private Sub GdViewer1_TransferEnded(ByVal status As GdPictureStatus, ByVal download As System.Boolean) Handles GdViewer.TransferEnded
GdViewer.Focus()
UpdateMainUi()
End Sub
Private Sub AddSearchRegion(ByVal occurence As Integer, ByVal leftCoordinate As Single, ByVal topCoordinate As Single, ByVal regionWidth As Single, ByVal regionheight As Single, ByVal ensureVisibility As Boolean)
Dim searchRegion As Integer = GdViewer.AddRegionInches("SearchResult" & occurence, leftCoordinate, topCoordinate, regionWidth, regionheight, ForegroundMixMode.ForegroundMixModeMASKPEN, Color.Yellow)
GdViewer.SetRegionEditable(searchRegion, False)
If ensureVisibility Then
GdViewer.EnsureRegionVisibility(searchRegion)
End If
End Sub
Private Sub btnSettings_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonSettings.ItemClick
Using frmSettings As New frmViewerSettings(GdViewer)
frmSettings.ShowDialog(Me)
End Using
UpdateaNavigationToolbar()
End Sub
Private Sub UpdateaNavigationToolbar()
Try
Dim oCurrentZoom As Double = GdViewer.Zoom
Dim oCurrentPage As Integer = GdViewer.CurrentPage()
Dim oPageCount As Integer = GdViewer.PageCount
txtCurrentPage.EditValue = oCurrentPage.ToString()
If oCurrentPage = oPageCount Then
buttonLastPage.Enabled = False
buttonNextPage.Enabled = False
buttonPrevPage.Enabled = True
buttonFirstPage.Enabled = True
ElseIf oCurrentPage = 1 Then
buttonPrevPage.Enabled = False
buttonFirstPage.Enabled = False
buttonLastPage.Enabled = True
buttonNextPage.Enabled = True
Else
buttonPrevPage.Enabled = True
buttonFirstPage.Enabled = True
buttonLastPage.Enabled = True
buttonNextPage.Enabled = True
End If
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
Private Sub btnFitWidth_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonFitWidth.ItemClick
GdViewer.ZoomMode = ViewerZoomMode.ZoomModeWidthViewer
End Sub
Private Sub btnFitPage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonFitPage.ItemClick
GdViewer.ZoomMode = ViewerZoomMode.ZoomModeFitToViewer
End Sub
Private Sub RichEditControl1_SizeChanged(sender As Object, e As EventArgs) Handles RichEditControl1.SizeChanged
Dim oControlWidth = RichEditControl1.Width - 100
Dim oPageWidth = Units.DocumentsToPixelsF(RichEditControl1.Document.Sections(0).Page.Width, RichEditControl1.DpiX)
RichEditControl1.Views.PrintLayoutView.ZoomFactor = oControlWidth / oPageWidth
End Sub
End Class