Printing PDF from VB.NET class

A

annoir

I am trying to print PDF documents using the code below and I get the
following error:

"Object reference not set to an instance of an object."

Am totally stumped. Can anybody help? I am using VS .NET 2003 on
Windows XP.

Thanks
Annie

Private Sub printPDFFile(ByVal strOutputFile As String)
Dim objStartInfo As New ProcessStartInfo
Dim objProcess As New System.Diagnostics.Process

If IsValidPrinter() Then
' set start info properties
With objStartInfo
.CreateNoWindow = True
.FileName = AcrobatReaderExecutable
.Arguments = "/t " & strOutputFile & " " &
ServicePrinterName
.UseShellExecute = True
.WindowStyle = ProcessWindowStyle.Hidden
.ErrorDialog = True
'.RedirectStandardError = True
End With

' start process
Try
objProcess = Process.Start(objStartInfo)
objProcess.Close()

'If Not objProcess.HasExited Then
' ' wait 40 seconds for graceful exit then kill it
' 'objProcess.WaitForExit(40000)

' ' kill process -- keeps it from hanging service
if it has not finished
' 'objProcess.Kill()
'End If

' print to event log
AddToEventLog("Attempting to print generated " &
getWordString() & " letter '" & strOutputFile & "' to printer [" &
ServicePrinterName & "]", EventLogEntryType.Information)
Catch ex As Exception
' error occurred
AddToEventLog("Error encountered while attempting to
print PDF file to printer [" & ServicePrinterName & "] " & vbCrLf &
ex.Message & vbCrLf & ex.Source & vbCrLf & ex.TargetSite.Name,
EventLogEntryType.Error)
End Try
Else
' printer not valid
AddToEventLog("The file '" & strOutputFile & "' cannot be
printed. The printer [" & ServicePrinterName & "] either is not valid
or has not been setup as a printer on this machine.",
EventLogEntryType.Error)
End If

End Sub
 
L

Lucas

I am trying to print PDF documents using the code below and I get the
following error:

"Object reference not set to an instance of an object."

Am totally stumped. Can anybody help? I am using VS .NET 2003 on
Windows XP.

Thanks
Annie

Private Sub printPDFFile(ByVal strOutputFile As String)
Dim objStartInfo As New ProcessStartInfo
Dim objProcess As New System.Diagnostics.Process

If IsValidPrinter() Then
' set start info properties
With objStartInfo
.CreateNoWindow = True
.FileName = AcrobatReaderExecutable
.Arguments = "/t " & strOutputFile & " " &
ServicePrinterName
.UseShellExecute = True
.WindowStyle = ProcessWindowStyle.Hidden
.ErrorDialog = True
'.RedirectStandardError = True
End With

' start process
Try
objProcess = Process.Start(objStartInfo)
objProcess.Close()

'If Not objProcess.HasExited Then
' ' wait 40 seconds for graceful exit then kill it
' 'objProcess.WaitForExit(40000)

' ' kill process -- keeps it from hanging service
if it has not finished
' 'objProcess.Kill()
'End If

' print to event log
AddToEventLog("Attempting to print generated " &
getWordString() & " letter '" & strOutputFile & "' to printer [" &
ServicePrinterName & "]", EventLogEntryType.Information)
Catch ex As Exception
' error occurred
AddToEventLog("Error encountered while attempting to
print PDF file to printer [" & ServicePrinterName & "] " & vbCrLf &
ex.Message & vbCrLf & ex.Source & vbCrLf & ex.TargetSite.Name,
EventLogEntryType.Error)
End Try
Else
' printer not valid
AddToEventLog("The file '" & strOutputFile & "' cannot be
printed. The printer [" & ServicePrinterName & "] either is not valid
or has not been setup as a printer on this machine.",
EventLogEntryType.Error)
End If

End Sub

Is there an option to open Adobe Reader, then print? I am unaware of
any internal PDF printing members.
 
A

annoir

I believe the Adobe Reader is initiated by this statement:

.FileName = AcrobatReaderExecutable

AcrobatReaderExecutable is derived from the config file which points
to the location of Adobe Reader.

<!-- Location (absolute path) of Acrobat Reader Executable
(AcroRd32.exe). Leave blank and service will find it automatically --<add key="readerexecutable" value="C:\Program Files\Adobe\Reader
8.0\Reader" />

Annie
 
L

Lucas

I believe the Adobe Reader is initiated by this statement:

.FileName = AcrobatReaderExecutable

AcrobatReaderExecutable is derived from the config file which points
to the location of Adobe Reader.

<!-- Location (absolute path) of Acrobat Reader Executable
(AcroRd32.exe). Leave blank and service will find it automatically --

<add key="readerexecutable" value="C:\Program Files\Adobe\Reader
8.0\Reader" />

Annie





- Show quoted text -

I have a few applications that open PDFs in Reader. I simply do
this

Process.Start("C:\Path\test.pdf") <-- Opens PDF in Reader

but I am not quite sure how to access keys in Reader to perform a
print programatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top