Imports Ionic.Zip Imports System.IO Imports System.Drawing.Printing Imports Infragistics.Win.FormattedLinkLabel Public Class frmMain Private c_DistilledPDF As String = "C:\Temp\MagicFolder\doc.pdf" Private Sub utmUtils_ToolClick(ByVal sender As System.Object, _ ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) _ Handles utmUtils.ToolClick Select Case e.Tool.Key Case "About" ShowAboutMenu() Case "Flatten" SaveSetting(Application.ProductName, "Settings", "Flatten", _ CType(Me.utmUtils.Tools("Flatten"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) Case "KeepOnTop" SaveSetting(Application.ProductName, "Settings", "TopMost", _ CType(Me.utmUtils.Tools("KeepOnTop"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) Me.TopMost = CType(Me.utmUtils.Tools("KeepOnTop"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Case "ToggleOutput" If Me.udmUtils.PaneFromControl(Me.ufteStatus).Closed Then Me.udmUtils.PaneFromControl(Me.ufteStatus).Show() Else Me.udmUtils.PaneFromControl(Me.ufteStatus).Close() End If Case "Decrypt" Me.ugbDecrypt.Visible = CType(Me.utmUtils.Tools("Decrypt"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Case "MagicFolder" Me.ugbDistill.Visible = CType(Me.utmUtils.Tools("MagicFolder"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Case "PDFZip" Me.ugbZip.Visible = CType(Me.utmUtils.Tools("PDFZip"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Case "PWCpdf" Me.ugbPWC.Visible = CType(Me.utmUtils.Tools("PWCpdf"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked End Select End Sub Private Sub frmMain_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Try ' Save the settings SaveSetting(Application.ProductName, "Settings", "TopMost", Me.TopMost) SaveSetting(Application.ProductName, "Settings", "MimimizeRibbon", Me.utmUtils.Ribbon.IsMinimized.ToString) SaveSetting(Application.ProductName, "Settings", "Flatten", CType(Me.utmUtils.Tools("Flatten"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "OpenUponCompletion", CType(Me.utmUtils.Tools("OpenUponCompletion"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "OverwriteExt", CType(Me.utmUtils.Tools("SaveAsAppend"), Infragistics.Win.UltraWinToolbars.TextBoxTool).Text) SaveSetting(Application.ProductName, "Settings", "DisplayDecrypt", CType(Me.utmUtils.Tools("Decrypt"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "DisplayMagicFolder", CType(Me.utmUtils.Tools("MagicFolder"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "DisplayPDFZip", CType(Me.utmUtils.Tools("PDFZip"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "DisplayPWCpdf", CType(Me.utmUtils.Tools("PWCpdf"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked) SaveSetting(Application.ProductName, "Settings", "Version", CType(Me.utmUtils.Tools("Version"), Infragistics.Win.UltraWinToolbars.ComboBoxTool).Text) SaveSetting(Application.ProductName, "Settings", "Height", Me.Height) SaveSetting(Application.ProductName, "Settings", "Width", Me.Width) Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Try Me.ufteStatus.ContextMenuItems = FormattedTextMenuItems.All ' ' Load the settings ' Me.TopMost = CType(GetSetting(Application.ProductName, "Settings", "TopMost", True), Boolean) CType(Me.utmUtils.Tools("KeepOnTop"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked = Me.TopMost Me.utmUtils.Ribbon.IsMinimized = IIf(GetSetting(Application.ProductName, "Settings", "MimimizeRibbon", "False") = "False", False, True) CType(Me.utmUtils.Tools("Flatten"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked _ = CType(GetSetting(Application.ProductName, "Settings", "Flatten", True), Boolean) CType(Me.utmUtils.Tools("OpenUponCompletion"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked _ = CType(GetSetting(Application.ProductName, "Settings", "OpenUponCompletion", True), Boolean) CType(Me.utmUtils.Tools("SaveAsAppend"), Infragistics.Win.UltraWinToolbars.TextBoxTool).Text = GetSetting(Application.ProductName, "Settings", "OverwriteExt", "_UNLOCK") CType(Me.utmUtils.Tools("Decrypt"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked = CType(GetSetting(Application.ProductName, "Settings", "DisplayDecrypt", True), Boolean) CType(Me.utmUtils.Tools("MagicFolder"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked = CType(GetSetting(Application.ProductName, "Settings", "DisplayMagicFolder", True), Boolean) CType(Me.utmUtils.Tools("PDFZip"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked = CType(GetSetting(Application.ProductName, "Settings", "DisplayPDFZip", True), Boolean) CType(Me.utmUtils.Tools("PWCpdf"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked = CType(GetSetting(Application.ProductName, "Settings", "DisplayPWCpdf", False), Boolean) CType(Me.utmUtils.Tools("Version"), Infragistics.Win.UltraWinToolbars.TextBoxTool).Text = GetSetting(Application.ProductName, "Settings", "Version", "1.4") Me.Height = CType(GetSetting(Application.ProductName, "Settings", "Height", Me.Height), Integer) Me.Width = CType(GetSetting(Application.ProductName, "Settings", "Width", Me.Width), Integer) Catch ex As System.Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Private Sub ugbDropZone_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ugbDropZone.DragDrop, ugbDecrypt.DragDrop, _ ugbDistill.DragDrop, ugbZip.DragDrop, ugbPWC.DragDrop Me.Cursor = Cursors.WaitCursor Me.ufteStatus.Text = "" Try If CType(sender, _ Infragistics.Win.Misc.UltraGroupBox).Name = "ugbDistill" Then Distill(e.Data.GetData("FileDrop", False)) ElseIf CType(sender, _ Infragistics.Win.Misc.UltraGroupBox).Name = "ugbZip" Then PdfToZip(e.Data.GetData("FileDrop", False)) ElseIf CType(sender, _ Infragistics.Win.Misc.UltraGroupBox).Name = "ugbPWC" Then PWCPdf(e.Data.GetData("FileDrop", False)) Else Fix(e.Data.GetData("FileDrop", False), CType(sender, _ Infragistics.Win.Misc.UltraGroupBox).Name = "ugbAnalyst") End If If Not Me.ufteStatus.IsInEditMode Then Me.ufteStatus.Focus() End If Me.ufteStatus.Value = Me.ufteStatus.Text & "-= FINISHED PROCESSING FILE(S) =-" & vbCrLf Me.ufteStatus.Refresh() Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try Me.Cursor = Cursors.Default Beep() End Sub Private Sub ugbDropZone_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ugbDropZone.DragEnter, ugbDecrypt.DragEnter, _ ugbDistill.DragEnter, ugbZip.DragEnter, ugbPWC.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.All Else e.Effect = DragDropEffects.None End If End Sub Private Sub ShowAboutMenu() MsgBox(Application.ProductName & vbCrLf _ & "Version " & Application.ProductVersion & vbCrLf _ & Application.CompanyName, MsgBoxStyle.Information, "About " _ & Application.ProductName) End Sub Private Sub Fix(ByVal s() As String, Optional ByVal Decrypt As Boolean = False) Dim i As Int32 Try TempFileCleanup() For i = 0 To s.Length - 1 Dim fi As New FileInfo(s(i)) Dim fiTempPdf As New FileInfo(TempFilename(fi.Name)) Dim fiRenamed As New FileInfo(fi.FullName) Dim IsZip As Boolean = False If Not ".pdf,.zip".Contains(fi.Extension.ToLower) Then MsgBox("This program is designed only to work with files " _ & "that have a .pdf or .zip extension.", MsgBoxStyle.Information, "Cannot process file") Exit Sub End If If fi.Extension.ToLower = ".zip" Then IsZip = True UnzipPDF(fi, fiTempPdf.Directory) fiTempPdf = New FileInfo(fiTempPdf.Directory.FullName & "\doc.pdf") Else If Not fiTempPdf.Directory.Exists Then fiTempPdf.Directory.Create() fi.CopyTo(fiTempPdf.FullName) End If Dim myUnlock As New Utils(fiTempPdf.FullName, fiTempPdf.FullName) With myUnlock .ProcessUnlock(CType(Me.utmUtils.Tools("Flatten"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked, Decrypt) End With If CType(Me.utmUtils.Tools("SaveAsAppend"), Infragistics.Win.UltraWinToolbars.TextBoxTool).Text.TrimEnd <> "" Then fiRenamed = New FileInfo(fi.FullName.Replace(fi.Extension, CType(Me.utmUtils.Tools("SaveAsAppend"), Infragistics.Win.UltraWinToolbars.TextBoxTool).Text.ToString & fi.Extension)) If IsZip Then fi.CopyTo(fiRenamed.FullName, True) ZipPDF(fiRenamed, fiTempPdf) Else fiTempPdf.CopyTo(fiRenamed.FullName, True) End If Else If IsZip Then ' Add the zip back ZipPDF(fi, fiTempPdf) End If End If If CType(Me.utmUtils.Tools("OpenUponCompletion"), Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Then If fiRenamed.Extension.ToLower = ".zip" Then Dim lqo As New frmLaunchClickOnce lqo.LaunchURL(New Uri(My.Settings.PathToSuperBoxing & "?File=" & fiRenamed.FullName)) Else Process.Start(fiRenamed.FullName) End If End If Next Catch ex As System.Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Public Sub FixSingle(fi As FileInfo, OutputFile As FileInfo, Optional Flatten As Boolean = False) Try modFileFunctions.TempFileCleanup() Dim fileInfo As New FileInfo(modFileFunctions.TempFilename(fi.Name)) If (fi.Extension.ToLower.Contains("pdf")) Then If (Not fileInfo.Directory.Exists) Then fileInfo.Directory.Create() End If fi.CopyTo(fileInfo.FullName) Dim util As New Utils(fileInfo.FullName, OutputFile.FullName) util.ProcessUnlock(Flatten, False, True) End If Catch ex As Exception End Try End Sub Public Sub FillablePDF(fi As FileInfo, OutputFile As FileInfo) Try If fi.Exists And fi.Extension.ToLower = ".zip" Then modFileFunctions.TempFileCleanup() Dim zipFiles As ZipFile = New ZipFile(fi.FullName) If zipFiles.ContainsEntry("doc.pdf") And zipFiles.ContainsEntry("doc.xml") Then Dim fileInfo As New FileInfo(modFileFunctions.TempFilename(fi.Name)) If Not fileInfo.Directory.Exists Then fileInfo.Directory.Create() End If zipFiles.ExtractAll(fileInfo.Directory.FullName) Dim fillable As New PWCUtils(New FileInfo(String.Concat(fileInfo.Directory.FullName, "\doc.pdf")), New FileInfo(String.Concat(fileInfo.Directory.FullName, "\doc.xml")), fi, OutputFile) fillable.MakePWCPDF() End If zipFiles.Dispose() modFileFunctions.TempFileCleanup() End If Catch ex As Exception End Try End Sub #Region " Zip Functions " Private Sub PWCPdf(ByVal s() As String) For intI As Integer = 0 To s.Length - 1 Dim fi As New FileInfo(s(intI)) If fi.Exists Then If fi.Extension = ".zip" Then ' Can only process SuperForm ZIPs Dim nz As New ZipFile(fi.FullName) ' These two SuperForm files are necessary to make a PWC PDF If nz.ContainsEntry("doc.pdf") And nz.ContainsEntry("doc.xml") Then ' Make working copies for safety Dim fiTempPdf As New FileInfo(TempFilename(fi.Name)) If Not fiTempPdf.Directory.Exists Then fiTempPdf.Directory.Create() nz.ExtractAll(fiTempPdf.Directory.FullName) ' Start processing file Dim PWC As New PWCUtils(New FileInfo(String.Concat(fiTempPdf.Directory.FullName, "\doc.pdf")), _ New FileInfo(String.Concat(fiTempPdf.Directory.FullName, "\doc.xml")), _ fi) Dim fiNew As New FileInfo(PWC.MakePWCPDF.FullName) If CType(Me.utmUtils.Tools("OpenUponCompletion"), _ Infragistics.Win.UltraWinToolbars.StateButtonTool).Checked Then Process.Start(fiNew.FullName) End If End If nz.Dispose() TempFileCleanup() End If End If Next End Sub Private Sub PdfToZip(ByVal s() As String) Dim i As Integer = 0 Dim PdfFileDropped As String = "" Dim ZipFilenameDropped As String = "" Dim HasZip As Boolean = False Dim HasPdf As Boolean = False If s.Length = 2 Then For i = 0 To s.Length - 1 If s(i).ToLower.EndsWith(".zip") Then HasZip = True ZipFilenameDropped = s(i) End If If s(i).ToLower.EndsWith(".pdf") Then HasPdf = True PdfFileDropped = s(i) End If Next ElseIf s.Length = 1 Then If s(i).ToLower.EndsWith(".pdf") Then HasPdf = True PdfFileDropped = s(i) End If End If If HasPdf = False Then Exit Sub Else Dim fiFileToZip As New FileInfo(PdfFileDropped) Dim fiTempPdf As New FileInfo(TempFilename("doc.pdf")) Dim fiZipFile As FileInfo If HasZip Then ' User dropped their own zip file fiZipFile = New FileInfo(ZipFilenameDropped) Else ' Create a new zip file fiZipFile = New FileInfo(fiFileToZip.FullName.ToLower.Replace(".pdf", ".zip")) Try If fiZipFile.Exists Then fiZipFile.Delete() Catch ex As Exception End Try End If TempFileCleanup() Try If Not fiTempPdf.Directory.Exists Then fiTempPdf.Directory.Create() fiFileToZip.CopyTo(fiTempPdf.FullName, True) Dim nz As New ZipFile(fiZipFile.FullName) If HasZip Then nz.UpdateFile(fiTempPdf.FullName, "") Else nz.AddFile(fiTempPdf.FullName, "") End If nz.Save() nz.Dispose() Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End If End Sub Private Sub ZipPDF(ByVal fiZipFile As FileInfo, ByVal fiFileToZip As FileInfo) ' Used to add a file back to a zip after distilling, etc Try If fiFileToZip.Exists Then Dim nz As New ZipFile(fiZipFile.FullName) nz.UpdateFile(fiFileToZip.FullName, "") nz.Save() nz.Dispose() End If Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Private Sub UnzipPDF(ByVal fiToUnzip As FileInfo, ByVal diUnzipTo As DirectoryInfo) Try Dim nz As New ZipFile(fiToUnzip.FullName) nz.Password = "5e$ACre7ru" If nz.ContainsEntry("doc.pdf") Then If Not diUnzipTo.Exists Then diUnzipTo.Create() nz.ExtractAll(diUnzipTo.FullName) End If nz.Dispose() Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub #End Region #Region " Magic Folder Functions " Private Sub Distill(ByVal s() As String) Dim i As Int32 Try TempFileCleanup() For i = 0 To s.Length - 1 Dim fi As New FileInfo(s(i)) Dim fiTempPdf As New FileInfo(TempFilename(fi.Name)) Dim fiDistilled As New FileInfo(c_DistilledPDF) Try If Not fiDistilled.Directory.Exists Then fiDistilled.Directory.Create() End If Catch ex As Exception End Try If fi.Extension.ToLower = ".zip" Then UnzipPDF(fi, fiTempPdf.Directory) fiTempPdf = New FileInfo(fiTempPdf.Directory.FullName & "\doc.pdf") PrintToPDF(fiTempPdf) ZipPDF(fi, fiDistilled) fiDistilled.Delete() Dim lqo As New frmLaunchClickOnce lqo.LaunchURL(New Uri(My.Settings.PathToSuperBoxing & "?File=" & fi.FullName)) Else Exit Try End If Next TempFileCleanup() Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Private Sub PrintToPDF(ByVal fi As FileInfo) Dim AcrobatPrinter As String = "Adobe PDF" 'Name Of printer Dim DefaultPrinter As String = GetDefaultPrinter() Dim myAcrobat As New Acrobat.AcroApp Dim av As New Acrobat.AcroAVDoc Dim pd As New Acrobat.AcroPDDoc Try SetDefaultPrinter(AcrobatPrinter) myAcrobat.Show() av.Open(fi.FullName, "doc") pd = av.GetPDDoc av.PrintPages(0, pd.GetNumPages - 1, 2, 0, 0) av.Close(False) myAcrobat.Exit() ' Wait until the file is free Do While (FileIsLocked(c_DistilledPDF)) Debug.Print("Waiting for unlock...") Loop Catch ex As Exception DisplayErrorMessage(ex, Me.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name) Finally SetDefaultPrinter(DefaultPrinter) End Try End Sub Public Function FileIsLocked(ByVal sFile As String) As Boolean Dim thisFileInUse As Boolean = False If System.IO.File.Exists(sFile) Then Try Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None) ' thisFileInUse = False End Using Catch thisFileInUse = True End Try End If Return thisFileInUse End Function Private Function GetDefaultPrinter() As String Dim pd As New PrintDocument Return pd.PrinterSettings.PrinterName '/ Get the system default printer End Function Private Function SetDefaultPrinter(ByVal PrinterName As String) As Boolean 'Store the new printer information in the '[WINDOWS] section of the WIN.INI file for 'the DEVICE= item Call WriteProfileString("windows", "Device", PrinterName & ", , ") 'Cause all applications to reload the INI file Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows") End Function Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" _ (ByVal lpszSection As String, ByVal lpszKeyName As String, _ ByVal lpszString As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lparam As String) As Long Private Const HWND_BROADCAST As Long = &HFFFF& Private Const WM_WININICHANGE As Long = &H1A #End Region End Class