跳至页脚内容
使用IRONPDF

VB .NET 在 PictureBox 中显示 PDF:在 Windows 窗体中将 PDF 页面渲染为图像

在 PictureBox 控件中显示 PDF 文件

由于 PictureBox 仅支持 BMP、PNG、TIF 和 JPG 等图像格式,因此无法在 PictureBox 控件中直接显示 PDF 文件。 解决方案是将 PDF 页面渲染为位图图像,然后在 Windows 窗体应用程序中显示。

在本教程中,我们将向您展示如何使用 IronPDF 强大的光栅化功能转换和显示 PDF 文档。 如果您需要更多参考资料或对未涉及的问题有答案,请允许我说明一下,您通常可以在 IronPDF 官方参考资料 文档中找到更多信息。

立即开始使用 IronPDF。
green arrow pointer

为什么 PictureBox 不能直接显示 PDF 文件?

Microsoft .NET Framework Windows Forms 应用程序中的 PictureBox 控件仅用于显示图像文件。 当开发人员尝试将 PDF 文档加载到 PictureBox 中时,他们会遇到错误,因为 PDF 格式需要专门的渲染。 虽然有些解决方案涉及通过 COM 组件使用 Adobe Reader 或 Adobe PDF Reader,但这种方法会产生对外部软件(如 Adobe Acrobat)的依赖性。 通常还有另一种方法可以解决程序的局限性。

一种更简洁的方法是使用 IronPDF 将每个 PDF 页面渲染为位图图像。 这种方法适用于任何 .NET 应用程序,用户无需在桌面系统上下载或安装 Adobe Acrobat。 该流程将 PDF 页面转换为 PictureBox 可以本地显示的图像格式,支持输出为 PNG、TIF 和其他标准格式。

如何为 PDF 查看器设置 Windows 表单项目?

在 Visual Studio 中创建一个新的 Windows 窗体应用程序项目。 在表单中添加工具箱中的 PictureBox 控件以及用于浏览页面的导航按钮。 通过 NuGet 软件包管理器搜索 "IronPDF "并点击安装,即可安装 IronPDF。

!a href="/static-assets/pdf/blog/vb-net-display-pdf-in-picture-box/vb-net-display-pdf-in-picture-box-1.webp">VB .NET Display PDF in PictureBox:在 Windows 窗体中将 PDF 页面渲染为图像:图片 1 - 安装 IronPDF

该 SDK 提供了将 PDF 文件加载、渲染和转换为图像所需的所有方法。

下面的代码示例显示了基本表单设置,其中包含用于 VB NET 在 PictureBox 功能中显示 PDF 的控件:

Imports IronPDF
Imports System.Drawing

Public Class PdfViewerForm
    Private currentPdf As PdfDocument
    Private currentPageIndex As Integer = 0
    Private pageImages() As IronSoftware.Drawing.AnyBitmap

    Private Sub PdfViewerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Form initialization code
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub
End Class
Imports IronPDF
Imports System.Drawing

Public Class PdfViewerForm
    Private currentPdf As PdfDocument
    Private currentPageIndex As Integer = 0
    Private pageImages() As IronSoftware.Drawing.AnyBitmap

    Private Sub PdfViewerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Form initialization code
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub
End Class
VB .NET

空白表单 UI 示例

!a href="/static-assets/pdf/blog/vb-net-display-pdf-in-picture-box/vb-net-display-pdf-in-picture-box-2.webp">VB .NET Display PDF in PictureBox:在 Windows 窗体中将 PDF 页面渲染为图像:图片 2 - 准备使用的空白 Windows 表单示例。

该代码为 PDF 查看器控件奠定了基础。 PdfDocument 对象保存加载的 PDF 文档,而 pageImages 保存每个 PDF 页面的渲染位图图像。 SizeMode 属性可在显示页面时自动缩放比例。 这是整个项目的重要基础。

如何在 PictureBox 中加载和显示 PDF 页面?

加载和显示 PDF 文档需要将每个 PDF 页面转换为 PictureBox 可以呈现的位图。 IronPDF 的 ToBitmap 方法可高效处理这一转换过程,支持各种图像格式和质量设置。

Private Sub LoadPdfDocument(filePath As String)
    ' Load the PDF document from file path
    currentPdf = PdfDocument.FromFile(filePath)
    ' Convert all PDF pages to bitmap images
    pageImages = currentPdf.ToBitmap()
    ' Display the first page
    If pageImages IsNot Nothing AndAlso pageImages.Length > 0 Then
        DisplayPage(0)
    End If
End Sub

Private Sub DisplayPage(pageIndex As Integer)
    If pageImages IsNot Nothing AndAlso pageIndex < pageImages.Length Then
        ' Create new Bitmap from the rendered page
        Dim newBitmap As New Bitmap(pageImages(pageIndex).GetStream())
        PictureBox1.Image = newBitmap
        currentPageIndex = pageIndex
        lblPageNumber.Text = $"Page {pageIndex + 1} of {pageImages.Length}"
    End If
End Sub

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Using openDialog As New OpenFileDialog()
        openDialog.Filter = "PDF Files|*.pdf"
        If openDialog.ShowDialog() = DialogResult.OK Then
            LoadPdfDocument(openDialog.FileName)
        End If
    End Using
End Sub
Private Sub LoadPdfDocument(filePath As String)
    ' Load the PDF document from file path
    currentPdf = PdfDocument.FromFile(filePath)
    ' Convert all PDF pages to bitmap images
    pageImages = currentPdf.ToBitmap()
    ' Display the first page
    If pageImages IsNot Nothing AndAlso pageImages.Length > 0 Then
        DisplayPage(0)
    End If
End Sub

Private Sub DisplayPage(pageIndex As Integer)
    If pageImages IsNot Nothing AndAlso pageIndex < pageImages.Length Then
        ' Create new Bitmap from the rendered page
        Dim newBitmap As New Bitmap(pageImages(pageIndex).GetStream())
        PictureBox1.Image = newBitmap
        currentPageIndex = pageIndex
        lblPageNumber.Text = $"Page {pageIndex + 1} of {pageImages.Length}"
    End If
End Sub

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Using openDialog As New OpenFileDialog()
        openDialog.Filter = "PDF Files|*.pdf"
        If openDialog.ShowDialog() = DialogResult.OK Then
            LoadPdfDocument(openDialog.FileName)
        End If
    End Using
End Sub
VB .NET

PDF 格式显示

!a href="/static-assets/pdf/blog/vb-net-display-pdf-in-picture-box/vb-net-display-pdf-in-picture-box-3.webp">VB .NET Display PDF in PictureBox:在 Windows 窗体中将 PDF 页面渲染为图像:图片 3 - 在我们的查看器中显示 PDF 的输入示例。

LoadPdfDocument 方法使用 PdfDocument.FromFile 从指定路径加载 PDF 文件,并使用用户选择的文件名。 ToBitmap 方法会在一次操作中将所有页面转换为位图图像。 每个位图都可以直接分配给 PictureBox 的 Image 属性进行显示。 这种方法通过将所有呈现的页面存储在一个数组中来处理多页 PDF 文档。 您还可以通过点击 Visual Studio 设计器中的相应选项卡来编辑表格的外观。

如何在 PDF 页面之间导航?

实现页面导航可让用户浏览 PDF 文档的所有页面。 以下代码示例演示了上一个和下一个按钮的功能:

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    If currentPageIndex > 0 Then
        DisplayPage(currentPageIndex - 1)
    End If
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    If pageImages IsNot Nothing AndAlso currentPageIndex < pageImages.Length - 1 Then
        DisplayPage(currentPageIndex + 1)
    End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    If currentPageIndex > 0 Then
        DisplayPage(currentPageIndex - 1)
    End If
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    If pageImages IsNot Nothing AndAlso currentPageIndex < pageImages.Length - 1 Then
        DisplayPage(currentPageIndex + 1)
    End If
End Sub
VB .NET

输出

!a href="/static-assets/pdf/blog/vb-net-display-pdf-in-picture-box/vb-net-display-pdf-in-picture-box-4.webp">VB .NET Display PDF in PictureBox:在 Windows 窗体中将 PDF 页面渲染为图像:图像 4 - 使用我们的按钮导航到第 2 页。

这些事件处理程序会在导航前检查边界,以防止出错。 IsNot Nothing 检查可确保在尝试导航之前已加载 PDF 文档。 这种模式适用于构建一个完整的 PDF 查看器,支持显示多个页面。

如何为保存和打印 PDF 文档添加功能?

许多 Windows 窗体应用程序需要打印 PDF 文档或将渲染的页面保存为图像文件。 IronPdf 通过其全面的 API 支持这两种操作。 使用该功能不会影响系统安全性,也不需要复杂的数学计算来计算打印页边距。

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    If currentPdf IsNot Nothing Then
        Dim printDialog As New PrintDialog()
        If printDialog.ShowDialog() = DialogResult.OK Then
            currentPdf.Print()
        End If
    End If
End Sub

Private Sub btnSaveImage_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    If PictureBox1.Image IsNot Nothing Then
        Using saveDialog As New SaveFileDialog()
            saveDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|TIF Image|*.tif"
            If saveDialog.ShowDialog() = DialogResult.OK Then
                PictureBox1.Image.Save(saveDialog.FileName)
            End If
        End Using
    End If
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    If currentPdf IsNot Nothing Then
        Dim printDialog As New PrintDialog()
        If printDialog.ShowDialog() = DialogResult.OK Then
            currentPdf.Print()
        End If
    End If
End Sub

Private Sub btnSaveImage_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    If PictureBox1.Image IsNot Nothing Then
        Using saveDialog As New SaveFileDialog()
            saveDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|TIF Image|*.tif"
            If saveDialog.ShowDialog() = DialogResult.OK Then
                PictureBox1.Image.Save(saveDialog.FileName)
            End If
        End Using
    End If
End Sub
VB .NET

打印对话框

!a href="/static-assets/pdf/blog/vb-net-display-pdf-in-picture-box/vb-net-display-pdf-in-picture-box-5.webp">VB .NET Display PDF in PictureBox:在 Windows 窗体中将 PDF 页面渲染为图像:图像 5 - 打印按钮工作示例

打印功能使用 IronPDF 内置的 打印方法,该方法与 Microsoft Windows 打印基础架构集成。 保存功能可将当前页面导出为图像文件,格式由用户选择。 这些功能使 PDF 查看器适用于文档管理应用程序。

结论

借助 IronPDF for .NET 的图像转换功能,在 VB.NET Windows Forms 应用程序中构建 PDF 查看器变得简单易行。 通过将 PDF 页面渲染为位图,开发人员可以在标准 PictureBox 控件中显示 PDF 文档,而无需外部依赖性。 该方法支持页面导航、缩放控制和打印功能,可提供完整的文档查看解决方案。

开始免费试用,在您的 Windows 窗体项目中实施 PDF 显示,或 购买许可证进行生产部署。

常见问题解答

如何使用 VB.NET 在 Windows 窗体 PictureBox 中显示 PDF?

您可以使用 IronPDF 的 ToBitmap 方法在 Windows 窗体 PictureBox 中显示 PDF,该方法允许您将 PDF 页面渲染为图像。

IronPDF 为在 VB.NET 中渲染 PDF 页面提供了哪些方法?

IronPDF 提供 ToBitmap 方法,用于将 PDF 页面渲染为图像,然后可以在 VB.NET 中的 PictureBox 控件中显示。

是否可以在 PictureBox 中浏览 PDF 页面?

是的,您可以通过使用 IronPDF 的页面导航功能来导航 PDF 页面,允许您在 PictureBox 控件中显示不同的页面。

使用 IronPDF 在 Windows 窗体中显示 PDF 有哪些优势?

IronPdf 通过将 PDF 页面转换为图像,简化了在 Windows 窗体中显示 PDF 的过程,这些图像可以轻松集成到 PictureBox 等控件中。它还支持页面导航等功能。

IronPDF 能否处理在 PictureBox 中显示的多页 PDF?

是的,IronPDF 可以处理多页 PDF。您可以使用它的方法选择特定页面并将其渲染为图片,以便在 PictureBox 中显示。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。