Imports PREPTOOLLib Imports System Imports System.IO Imports System.Windows.Forms Imports Infragistics.Win Imports Infragistics.Win.FormattedLinkLabel.FormattedLinkEditorAction Public Class Utils Private bytFile() As Byte ' This is the byte array of the file. Private m_Filename, m_NewFileName As String ' Filename of incoming and outgoing files. Private fDecrypt As Boolean = False #Region " Public methods " Public Sub New(ByVal strFilename As String, ByVal strNewFilename As String) m_Filename = strFilename m_NewFileName = strNewFilename End Sub Public Sub ProcessUnlock(Optional ByVal Flatten As Boolean = False, Optional ByVal Decrypt As Boolean = False, Optional ByVal KeepVersion As Boolean = False) Dim thisFile As FileInfo thisFile = New FileInfo(m_Filename) Console.WriteLine(String.Concat("Unlocking ", thisFile.FullName)) If Decrypt = True Then If DecryptPDF() Then m_Filename = thisFile.FullName.Replace(".pdf", "_DECRYPT.pdf") End If End If Try ' Setup the file info to be processed If Not thisFile.Exists Then Exit Sub Catch ex As Exception MsgBox(Err.Description, MsgBoxStyle.Information, "Utils-ProcessUnlock(" _ & Err.Number & ")") End Try ' If the file exists process it by reading the file to memory so the ' file can be overwritten Try Dim fsFile As FileStream fsFile = New FileStream(m_Filename, FileMode.Open) Dim brFile As New BinaryReader(fsFile) brFile.BaseStream.Seek(0, SeekOrigin.Begin) bytFile = brFile.ReadBytes(fsFile.Length) brFile.Close() fsFile.Close() Catch MsgBox(Err.Description, MsgBoxStyle.Information, "Utils-ProcessUnlock-FileStreamRead(" _ & Err.Number & ")") Exit Sub End Try Try Dim fileInput As New IDoc ' Input PDF to repurpose Dim fileOutput As New PDoc ' Repurposed PDF fileInput.OpenMem(bytFile) ' If the file has no pages it is likely because it is encrypted If fileInput.NumPages = 0 Then ProcessUnlock(Flatten, True) Exit Sub End If Dim strKeys As String = fileInput.GetInfoKeys Dim arrKeys2 As Array = strKeys.Split(vbCrLf) Dim strMetaData As String = fileInput.GetMetaData fileOutput.[New](m_NewFileName) fileOutput.Attach(fileInput) fileOutput.SetFlatten(Flatten) Dim myenum As IEnumerator = arrKeys2.GetEnumerator ' Blank any of the attributes that are set, including non-standard ones. While myenum.MoveNext If myenum.Current.ToString.Length > 0 Then Select Case myenum.Current.ToString.Remove(0, 1) Case "Producer", "ModDate" Case "Creator" fileOutput.SetAttr(myenum.Current.ToString.Remove(0, 1), "PDFUtils") Case "CreationDate" fileOutput.SetAttr(myenum.Current.ToString.Remove(0, 1), Date.Now) Case Else fileOutput.SetAttr(myenum.Current.ToString.Remove(0, 1), "") End Select End If End While ' Determine the file version settings to use Dim ver_info As String = "1.4" If KeepVersion Then ver_info = fileInput.GetVersion End If fileOutput.SetPDFVersion(ver_info) fileOutput.AddViewerPreference("PrintScaling", "None", True) fileInput.SetFormFont(PTFormFontType.PTffHelv) Dim intI As Integer = 0 For intI = 1 To fileInput.NumPages fileOutput.InputCopyPages(intI, intI) Next fileOutput.Close() Catch ex As System.Exception If Err.Number = -2147467261 Then If Decrypt = True Then ' Decrypt didn't work so gracefully exit Exit Sub ElseIf Not fDecrypt Then ' This error is usually an encryption problem. Run Joe's utility fDecrypt = True ProcessUnlock(Flatten, True) End If Else MsgBox(Err.Description, MsgBoxStyle.Information, "ProcessUnlock (" & Err.Number & ")") End If End Try If Decrypt Then Dim decryptedfile As New FileInfo(m_Filename) If decryptedfile.Exists And decryptedfile.FullName.Contains("_DECRYPT.pdf") Then Try ' Delete the temp file decryptedfile.Delete() Catch ex As Exception End Try End If End If End Sub #End Region #Region " Private Functions " Private Function DecryptPDF() As Boolean Try Dim thisFile As FileInfo thisFile = New FileInfo(m_Filename) ' Setup the file info to be processed Dim decryptProcess As New Process With decryptProcess .StartInfo.FileName = String.Concat(My.Application.Info.DirectoryPath, "\", "STFDecryptPdf.exe") .StartInfo.Arguments = String.Concat("-i ", Chr(34), m_Filename, Chr(34)) .Start() .WaitForExit() Dim decryptedfile As New FileInfo(m_Filename.Replace(".pdf", "_DECRYPT.pdf")) If decryptedfile.Exists And decryptedfile.Length = 0 Then ' On some machines it appears it takes Joe's control a bit ' longer to respond and the file does not get created in time. ' Try and wait a couple seconds then to see if it gives it a chance to finish. Threading.Thread.Sleep(2000) End If End With Catch ex As Exception Return False End Try Return True End Function #End Region End Class