STAMPARE UN PDF CON System.Drawing.Printing

martedì 27 novembre 2007 - 17.03

AndreaKpr Profilo | Senior Member

Ciao ragazzi.
Ho già smanettato a nastro, ma ho bisogno di un consulto con voi.

Ho un Pdf salvato su disco e devo effettuare la stampa passanfo da System.Drawing.Printing.
Ma è possibile effettuare tal operazione ???
Ho trovato poco in rete...

Io immaginavo che all' oggetto System.Drawing.Printing.PrintDocument, bastava passagli il Pdf
in formato binario da stampare ed il gioco era fatto...

Ma mi sa che che non è cosi semplice. Qualche dritta ???


RAGA DATEME NA MANO !

fabrizio. Profilo | Newbie

Ciao.
Io ho utilizzato il namespace System.Diagnostics per far partire un processo.
Impostando il .Verb = "Print" invio automaticamente in stampa un file passato come path alla sub PrintAdobePDF.
In pratica si apre la finestra dell'Acrobat Reader che invia subito in stampa il documento (come succede cliccando col tasto destro su un fle pdf nell'esplora risorse e selezionando la voce Print).
Spero questo codice possa esserti di aiuto.
Fabrizio


Imports System.Diagnostics

Sub PrintAdobePDF(ByVal Filename As String)
Dim myProcess As New Process
Try
'Point the process to our Acrobat file
myProcess.StartInfo.FileName = Filename
myProcess.StartInfo.WorkingDirectory = New IO.FileInfo(Filename).DirectoryName

'Since we don't want to see Acrobat's UI (we just want the report printed)
'then suppress the UI and pass the "Print" verb to force Acrobat to only print
myProcess.StartInfo.CreateNoWindow = True
'myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myProcess.StartInfo.Verb = "Print"
myProcess.Start()

'Wait until Acrobat has finished heavy processing,
'or 10 seconds whichever comes first
If Not myProcess.HasExited Then
myProcess.WaitForInputIdle(10000)

'Attempt to close Acrobat so it's UI
'doesn't sit on the user's task bar
'loop until we suceed or until we timeout.
'If we timeout, kill Adobe
Dim i As Integer = 1
Dim lbRunning As Boolean = True
While lbRunning And i <= 20
'sleep for 1 second
System.Threading.Thread.Sleep(1000)

'See if the Adobe will exit gracefully Select
Select Case myProcess.HasExited
Case True : lbRunning = False
Case False : lbRunning = Not myProcess.CloseMainWindow
End Select

i += 1
End While

'If it never closed gracefuly, force it out of memory
If lbRunning AndAlso Not myProcess.HasExited Then myProcess.Kill()
myProcess.Dispose()
End If

Catch ex As System.ComponentModel.Win32Exception
'A process error throws a somewhat generic
'Win32Exception object when the source file is not found.
'Capture the Win32Exception object and examine it for a
'File Not Found state. If found, throw a more specific exception
Const ERROR_FILE_NOT_FOUND As Integer = 2
Select Case ex.NativeErrorCode
Case ERROR_FILE_NOT_FOUND : Throw New System.IO.FileNotFoundException
Case Else : Throw New Exception(ex.Message & " (PrintAdobePDF)")
End Select
Catch ex As Exception
'Bubble up the exception and concatinate the name of this procedure
'as a poor man's route trace
Throw New Exception(ex.Message & " (PrintAdobeFile)")
End Try
End Sub


AndreaKpr Profilo | Senior Member


Grazie Fabrizio.
Avevo già pensato a questa metodologia, ma cosi facendo non sono sicuro che la stampante
abbia termiato correttamente la stampa.
O se la stampante sia andata in errore...

babbubba Profilo | Senior Member

Hai trovato una soluzione? Anche io sto cercando un sistema per convertire uno stream da file pdf in un'immagine o qaulcosa da usare con graphics.draw... dentro l'evento print di un printdocument...

Babbubba
www.babbubba.net

Chiedere è curiosità ... rispondere è educazione.
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5