You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET生成PDF后无法直接打印,求解决方法(已尝试Chrome Process方式)

VB.NET 直接打印PDF的解决思路

一、排查Chrome调用的问题

  • 确认Chrome路径正确性:Chrome在64位系统的默认路径通常是C:\Program Files (x86)\Google\Chrome\Application\chrome.exe,可以先硬编码路径测试,避免路径变量指向错误位置。
  • 修正命令参数:仅--kiosk-printing参数可能无法直接触发打印,需补充指定打印机(可选)并确保参数格式正确,示例:
processo.StartInfo.Arguments = String.Format("--kiosk-printing --print-to-printer=""你的打印机名称"" ""{0}""", caminhoArquivoPDF)
  • 临时关闭CreateNoWindow:将CreateNoWindow = False,查看Chrome是否弹出错误提示(如PDF路径无效、权限不足等),方便定位问题。
  • 捕获进程异常:添加异常捕获代码,确认进程启动是否失败:
Try
    processo.Start()
    processo.WaitForExit()
Catch ex As Exception
    Console.WriteLine(ex.Message) ' 输出异常信息用于排查
End Try

二、使用PDF处理库直接打印(更稳定)

依赖外部浏览器容易受环境影响,推荐用专业PDF库直接操作:

  • PDFSharp 示例:
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing.Printing

Dim pdfDoc As PdfDocument = PdfReader.Open(caminhoArquivoPDF, PdfDocumentOpenMode.Import)
Dim printDoc As New PdfPrintDocument(pdfDoc)
' 可选:指定打印机,不设置则用系统默认
printDoc.PrinterSettings.PrinterName = "目标打印机名称"
printDoc.Print()
  • iText 7 示例:
Imports iText.Kernel.Pdf
Imports System.Drawing.Printing

Using reader As New PdfReader(caminhoArquivoPDF)
    Using document As New PdfDocument(reader)
        Dim printDoc As New PrintDocument()
        printDoc.PrinterSettings.PrinterName = "目标打印机名称"
        AddHandler printDoc.PrintPage, Sub(sender, e)
            Dim page = document.GetPage(printDoc.PrinterSettings.FromPage)
            Dim pageSize = page.GetPageSize()
            ' 将PDF页面绘制到打印画布
            e.Graphics.DrawImage(YourPdfToImageConverter(page), e.PageBounds)
        End Sub
        printDoc.Print()
    End Using
End Using

三、使用Windows Shell命令打印

调用系统默认PDF阅读器完成打印,无需指定具体程序:

Imports System.Runtime.InteropServices

<DllImport("shell32.dll")>
Private Shared Function ShellExecute(hwnd As IntPtr, lpOperation As String, lpFile As String, lpParameters As String, lpDirectory As String, nShowCmd As Integer) As IntPtr
End Function

' 触发打印
ShellExecute(IntPtr.Zero, "print", caminhoArquivoPDF, Nothing, Nothing, 0)

内容的提问来源于stack exchange,提问作者Heitor Badotti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:47:13