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

如何在VB.NET中预览并打印SharpPDFLabel?代码报错求助

VB.NET中SharpPDFLabel打印预览与打印的问题修正及实现

你的代码存在的核心问题

  • ASP.NET API误用:Response.AddHeader和FileStreamResult是ASP.NET(Web应用)专属API,你的代码是WinForms桌面程序,该环境下不存在Response对象,这是报错的直接原因。
  • 命名冲突:变量名personCollection和自定义类名完全重复,会触发编译错误,必须修改其中一方的名称。
  • 集合未初始化:personCollection仅声明但未实例化,循环遍历会抛出空引用异常。

正确实现方案(WinForms环境)

第一步:修正基础代码(命名与集合初始化)

先解决命名冲突和集合初始化问题:

' 修正类名,避免与变量重名
Public Class Person
    Public fullname As String
    Public address1 As String
    Public address2 As String
    Public city As String
    Public stateCode As String
    Public zipcode As String
End Class

Public Class Form1
    ' 修正变量名并初始化集合
    Private personList As New List(Of Person)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 添加测试数据到集合
        personList.Add(New Person With {
            .fullname = "张三",
            .address1 = "测试街道1号",
            .address2 = "XX小区3栋",
            .city = "北京",
            .stateCode = "BJ",
            .zipcode = "100000"
        })

        ' 创建标签模板
        Dim labelDefinition = New SharpPDFLabel.Labels.A4Labels.Avery.L5160()
        Dim customLabelCreator = New SharpPDFLabel.CustomLabelCreator(labelDefinition)

        ' 批量添加标签内容
        For Each person In personList
            Dim label As New SharpPDFLabel.Label(SharpPDFLabel.Enums.Alignment.LEFT)
            label.AddText(person.fullname, "Verdana", 12, embedFont:=True)
            label.AddText(person.address1, "Verdana", 12, embedFont:=True)
            label.AddText(person.address2, "Verdana", 12, embedFont:=True)
            label.AddText($"{person.city}, {person.stateCode} {person.zipcode}", "Verdana", 12, embedFont:=True)
            customLabelCreator.AddLabel(label)
        Next

        ' 生成PDF流
        Dim pdfStream = customLabelCreator.CreatePDF()
        ' 调用预览/打印方法
        PreviewAndPrintPdf(pdfStream)
    End Sub

第二步:实现打印预览与打印

方法1:使用默认PDF阅读器(简单快捷)

将PDF流保存为临时文件,调用系统默认PDF阅读器打开(自带预览和打印功能):

Private Sub PreviewAndPrintPdf(pdfStream As Stream)
    ' 创建临时文件路径
    Dim tempPath = Path.Combine(Path.GetTempPath(), "address_labels.pdf")

    ' 将流写入临时文件
    Using fileStream As New FileStream(tempPath, FileMode.Create)
        pdfStream.Position = 0 ' 重置流指针到起始位置
        pdfStream.CopyTo(fileStream)
    End Using

    ' 调用默认程序打开PDF(支持预览和打印)
    Process.Start(New ProcessStartInfo(tempPath) With {
        .UseShellExecute = True
    })
End Sub

方法2:嵌入WinForms控件预览(集成化)

使用开源的PdfiumViewer控件(需通过NuGet安装),直接在窗体中嵌入PDF预览并支持打印:

  1. 通过NuGet安装PdfiumViewer包;
  2. 在窗体上添加PdfViewer控件(可从工具箱拖入);
  3. 实现预览代码:
Private Sub PreviewAndPrintPdf(pdfStream As Stream)
    pdfStream.Position = 0 ' 重置流指针
    ' 将流加载到PdfViewer控件
    PdfViewer1.LoadDocument(pdfStream)
    ' 直接调用打印对话框
    ' PdfViewer1.Print()
End Sub

注意事项

  • 确保Verdana字体在系统中存在,或替换为系统已安装的字体;
  • 临时文件可在程序退出时删除,避免占用磁盘空间;
  • 使用PdfiumViewer时需注意平台兼容性(32/64位)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 19:06:01