View Full Version : PDFCreator generates corrupt PDF file

2011-10-11, 10:11 PM
Hi @ all

I've created a windows forms application which should create a pdf out of specific excel file with selected worksheets! Unfortunately, the code below only generates corrupt PDF files and I can't see the reason: "Adobe Reader could not open 'Document.pdf' because it is either not a supported file type or because the file has been damaged..."

I've already asked in the pdfforge.org forum, but there seems to be nobody who can help! Why is this code not working???

Here's the situation:
- PDFCreator 1.2.3
- MS Excel 2010 / MS Office 2010
- Visual Studio Professional 2010

...and here's the modified code from Ken Puls (VBA to VB.NET):

Public Sub MainXLS()

' Declare variables
Dim ExcelInstance As New Microsoft.Office.Interop.Excel.Application
Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook

' Hide excel instance
ExcelInstance.Visible = False

' Open workbook
ExcelWorkbook = ExcelInstance.Workbooks.Open("C:\Users\ad\Desktop\411539_XXXXXX_CAM.XLS")

' Create PDF
PrintToPDF_SpecifiedSheetsToOne_Early(ExcelWorkbook, _
ExcelInstance, _
"C:\Users\ad\Desktop", _

' Close workbook and Excel instance

' Clean up
ExcelWorkbook = Nothing
ExcelInstance = Nothing

End Sub

Private Sub PrintToPDF_SpecifiedSheetsToOne_Early(ByRef ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook, _
ByRef ExcelInstance As Microsoft.Office.Interop.Excel.Application, _
ByVal SheetSelection As String, _
ByVal OutputPath As String, _
ByVal OutputName As String)

' Author : Ken Puls (www.excelguru.ca)
' Modified by : Olphi
' Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for early bind, set reference to PDFCreator

' Declare variables
Dim pdfJob As PDFCreator.clsPDFCreator
Dim strPDFName As String
Dim strPDFPath As String
Dim strSheets() As String
Dim boolRestart As Boolean
Dim longSheet As Long
Dim longTotalSheets As Long


' Define the output
strPDFName = OutputName & ".pdf"
strPDFPath = OutputPath & ExcelInstance.PathSeparator

' Split the sheets into an array
strSheets = SheetSelection.Split(",")

' Turn off screen updates
ExcelInstance.ScreenUpdating = False

' Create new PDFCreator instance
pdfJob = New PDFCreator.clsPDFCreator

' Check if PDFCreator is already running and attempt to kill the process if so
boolRestart = False
pdfJob = New PDFCreator.clsPDFCreator
If pdfJob.cStart("/NoProcessingAtStartup") = False Then

'PDF Creator is already running, kill the existing process
Shell("taskkill /f /im PDFCreator.exe", vbHide)
pdfJob = Nothing
boolRestart = True

End If
Loop Until boolRestart = False

'Assign settings for PDF job
With pdfJob
.cVisible = False
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = strPDFPath
.cOption("AutosaveFilename") = strPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
End With

' Delete the PDF if it already exists
If Dir(strPDFPath & strPDFName) = strPDFName Then Kill(strPDFPath & strPDFName)

'Print the document to PDF
For longSheet = LBound(strSheets) To UBound(strSheets)

' Check if there is printable data inside the selected worksheet
If Not IsNothing(ExcelInstance.Sheets(strSheets(longSheet)).UsedRange) Then

' Print worksheet
ExcelInstance.Sheets(strSheets(longSheet)).PrintOut(copies:=1, ActivePrinter:="PDFCreator auf NE01:")

' Increase sheet counter
longTotalSheets = longTotalSheets + 1

End If

Next longSheet

' Wait until all print jobs have entered the print queue
Do Until pdfJob.cCountOfPrintjobs = longTotalSheets

' Combine all PDFs into a single file and stop the printer
With pdfJob
.cPrinterStop = False
End With

' Wait until the file shows up before closing PDF Creator
Loop Until Dir(strPDFPath & strPDFName) = strPDFName

Catch ex As Exception

'Inform user of error, and go to cleanup section
MsgBox("There was an error encountered. PDFCreator has" & vbCrLf & _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error")


'Release objects and terminate PDFCreator
pdfJob = Nothing
Shell("taskkill /f /im PDFCreator.exe", vbHide)

' Turn on screen updates
ExcelInstance.ScreenUpdating = True

End Try

End Sub

2011-10-12, 11:58 AM
Hi Olphi...

I don't know, if this helps, as I am not a specialist for .Net. But if I understand the code right, are you passing workbook names to the function (like SPEZ_SW.XLS)?
I can also suggest to try to manually print an Excel document using the PDF Creator. If this also leads to a corrupted document, then I think, there may be a problem with the installation.

Best Regards :-)

2011-10-12, 12:48 PM
Hi and thank you for your time!

Yes, manually it always works, so the problem lies in the code i guess! Yes the workbook names are passing to the printer by this code:

strSheets = SheetSelection.Split(",")

The printer prints the files and shows up a file, but the file can't be opened! So I can see how the printer prints all pages within the specified sheets...! Anybody out there who can see it?

Cheers, Olphi

Ken Puls
2011-10-12, 04:48 PM
Hi Olphi,

I'm not really a .NET guy either, but I've spent a ton of time working with PDFCreator... not guaranteeing I can help/solve this mind you, but I'll give it a go.

Have you tried printing a single worksheet to a PDF via code to see if that works? I don't think that the splitting of the array should be an issue, but I'm thinking we might as well try to start somewhere...

Jessica Kloss
2011-11-24, 10:17 AM

2015-03-13, 03:51 AM