|
-
Jul 17th, 2017, 06:15 AM
#1
Thread Starter
Fanatic Member
Converting files to PDF
I seem to recall asking this before but can't find it....
We have an old application that runs as a scheduled task on an very old server. A major part of its job is converting files into PDFs, and it does this by using the MS Office object model. Essentially, it uses the relevant application to open the file (".DOC", ".TXT", ".HTM", ".HTML", ".RTF", ".MHT", ".MHTML" files are opened in Word, ".XLS", ".CSV" in Excel), and then uses that to print them into a PDF file. Very basic, but it works well (please ignore the naming conventions!).
Code:
Select Case UCase(mFileExt)
Case ".DOC", ".TXT", ".HTM", ".HTML", ".RTF", ".MHT", ".MHTML"
Dim oWordApp As Word.Application
Dim oDocument As Word.Document
oWordApp = CreateObject("Word.Application")
oWordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
oWordApp.Visible = False
oWordApp.Application.WindowState = Word.WdWindowState.wdWindowStateMinimize
oDocument = CreateObject("Word.Document")
oDocument = oWordApp.Documents.Open(mTempFilePath, PasswordDocument:=mPassword, ReadOnly:=True)
Dim holdprtrdefault As String = oWordApp.ActivePrinter
oWordApp.ActivePrinter = "Adobe PDF"
oWordApp.Application.PrintOut(FileName:=mTempFilePath, _
Copies:=1, _
Range:=Word.WdPrintOutRange.wdPrintAllDocument, _
Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
Pages:="", _
PageType:=Word.WdPrintOutPages.wdPrintAllPages, _
Collate:=True, _
Background:=False, _
PrintToFile:=True, _
OutputFileName:=mPSFilePath)
oWordApp.ActivePrinter = holdprtrdefault
oWordApp.Application.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
oWordApp = Nothing
oDocument = Nothing
Case ".XLS", ".CSV"
Dim oXLApp As Excel.Application
Dim oWkBook As Excel.Workbook
oXLApp = CreateObject("Excel.Application")
oXLApp.DisplayAlerts = False
oXLApp.Visible = False
oXLApp.Application.WindowState = Excel.XlWindowState.xlMinimized
oWkBook = oXLApp.Workbooks.Open(mTempFilePath, Password:=Trim(mPassword), ReadOnly:=True, UpdateLinks:=False, Converter:=True, IgnoreReadOnlyRecommended:=True)
Dim holdprtrdefault As String = oXLApp.ActivePrinter
Dim nSheets As Integer = oWkBook.Worksheets.Count
oWkBook.PrintOut(1, nSheets, 1, ActivePrinter:="Adobe PDF", PrintToFile:=True, Collate:=True, PrToFileName:=mPSFilePath)
oXLApp.ActivePrinter = holdprtrdefault
oXLApp.Application.Quit()
oXLApp = Nothing
oWkBook = Nothing
Case ".MDI"
Dim oDIApp As MODI.Document = New MODI.Document
oDIApp.Create(mTempFilePath)
oDIApp.PrintOut(PrinterName:="Adobe PDF", PrintToFileName:=mPSFilePath, FitMode:=MODI.MiPRINT_FITMODES.miPRINT_ACTUALSIZE)
oDIApp.Close()
oDIApp = Nothing
Case Else
mIsPDF = False
mConvToPDF = False
mLogMsg = "Invalid File Format"
mLogType = MergeLogType.Failure
WriteLog(" - Invalid File Format", False)
Exit Sub
End Select
However, we now want to move this application to a new server and would like to avoid having to install Office on that new server. This means we need an alternative way of converting all of these files into PDFs. Unfortunately buying new software is out of the question, because getting approval for anything not in our current development stack is a looooooooooooooooong process in this place. Even freeware could be a problem, although I'd be willing to look into it.
Anyone have any suggestions? I've googled quite a bit and haven't found anything suitable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|