0

全部,

我有以下代码用于打印写入磁盘的 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 之间。出于某种原因,某些文档会打印,而其他文档则不会。文件大小和打印成功之间似乎没有相关性。由于正在处理,因此不会引发任何错误。

有没有人遇到过这样的问题?请让我知道,如果你有任何问题。

4

1 回答 1

0

我有一个类似的问题,我发现如果我增加睡眠时间,那么线程就有时间完成它所做的事情,然后打印 PDF。试试看希望有帮助

于 2012-07-06T15:13:30.860 回答