2012-07-05 78 views
0

所有,不工作100%打印在vb.net一個PDF時

我有下面的代碼,我使用打印寫入到磁盤的PDF文件:

Public Function PrintPDF(ByVal PDFFile As String, ByVal Printer As String, ByVal Timeout As Integer) As Integer 

    log_error.sendEventLog("PrintPDF", "DEBUGGING - Printing PDF. File: " & PDFFile & ". Printer: " & Printer, EventLogEntryType.Information) 

    If Printer.Trim.Length = 0 Then 
     Printer = (New System.Drawing.Printing.PrinterSettings).PrinterName 
    End If 

    Dim Proc As New System.Diagnostics.Process 

    Try 
     Proc.EnableRaisingEvents = True 
     Proc.StartInfo.FileName = PDFFile 
     Proc.StartInfo.Arguments = Chr(34) + Printer + Chr(34) 
     Proc.StartInfo.Verb = "PrintTo" 
     Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized 
     Proc.StartInfo.CreateNoWindow = True 

     Proc.Start() 
    Catch ex As Exception 
     log_error.sendEventLog("PrintPDF Error", "An issue has occured when attemtping to print a PDF", EventLogEntryType.Error) 
    End Try 

    Do While Timeout > 0 AndAlso Not Proc.HasExited 
     System.Threading.Thread.Sleep(1000) 
     Timeout -= 1 
    Loop 

    If Not Proc.HasExited Then 
     log_error.sendEventLog("PrintPDF", "Process Killed", EventLogEntryType.Information) 
     Proc.Kill() 
    End If 

    log_error.sendEventLog("PrintPDF", "Process Closed", EventLogEntryType.Information) 
    Proc.Close() 
    Proc.Dispose() 

    Return 0 
End Function 

當利用這種方法,我循環了大約10個PDF文檔。這些文件的範圍在2mb到5mb之間。由於某種原因,某些文件可能會打印,而其他文件則不會。文件大小和打印成功之間似乎沒有關聯。在處理過程中不會出現任何錯誤。

有沒有人遇到過像這樣的問題?請讓我知道,如果你有任何問題。

回答

0

我有類似的問題,我發現如果我增加睡眠時間,那麼線程有時間完成它的操作,然後打印PDF。試試希望它有幫助