跳至页脚内容
使用IRONPDF

在Windows窗体PictureBox中渲染PDF页面 | C#指南

在 Windows Forms 应用程序中显示 PDF 页面是文档管理工具、报表查看器和文件预览实用程序的常见需求。 PictureBox 控件是一个理想的选择,因为它已经处理表单中的图像渲染。 问题在于 PDF 文件不是图像——它们需要经过专门的渲染才能被任何标准图像控件显示。 本指南将逐步介绍如何使用 IronPDF 将 PDF 页面转换为位图图像,并在 PictureBox 控件中显示它们,以及页面导航、打印和保存支持。

立即开始使用 IronPDF。
green arrow pointer

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

Windows Forms 中的PictureBox 控件专门为图像格式而设计:BMP、PNG、JPG、GIF、TIFF 以及其他一些格式。 当您尝试将 PDF 文件路径分配给 PictureBox.ImageLocation 或将 PDF 流加载到 PictureBox.Image 时,控件会抛出异常,因为 PDF 二进制格式不是可识别的图像格式。

有些方法试图通过 COM 互操作嵌入 Adobe Acrobat 来规避这个问题。但这会造成对运行应用程序的每台机器都必须安装 Adobe Reader 的硬性依赖——这种部署限制在企业环境中难以管理。 它还会将渲染引擎锁定为用户所安装的 Acrobat 版本,这可能会导致不同机器之间的兼容性问题。

更简洁的方法是使用 .NET PDF 库,该库在运行时将PDF页面转换为位图图像。IronPDF 通过其 ToBitmap 方法提供了这种功能,该方法将 PDF 文档的每一页渲染成一个 AnyBitmap 对象。 然后您可以将该对象转换为标准的 System.Drawing.Bitmap 并将其直接分配给 PictureBox.Image。 无需安装 Adobe 产品,无需进行 COM 注册,也无需在应用程序窗口中嵌入外部查看器。

该技术适用于 .NET Framework 4.6.2 及更高版本,以及 Windows 上的 .NET 6、8 和 10。 IronPDF 内部的渲染引擎基于 Chrome,这意味着即使对于具有复杂布局、嵌入式字体和混合图像/文本内容的 PDF,它也能生成高保真输出。

如何在 Windows Forms 项目中安装 IronPDF?

在编写任何代码之前,您需要将 IronPDF 添加到您的项目中。 最快的方法是通过 NuGet 包管理器 CLI:

dotnet add package IronPdf
dotnet add package IronPdf
SHELL

或者,使用 Visual Studio 包管理器控制台:

Install-Package IronPdf
Install-Package IronPdf
SHELL

安装完成后,请确认包引用出现在您的 .csproj 文件中。IronPDF 的目标平台是 Windows,运行在 .NET 6 及更高版本上。因此,如果您在项目文件中使用跨平台目标,请为 Windows 添加 <RuntimeIdentifiers><Platforms> 限制。 对于 Windows Forms 项目,在 Visual Studio 中创建项目模板时,通常已经配置好了。

对于生产环境中的许可,请将您的许可证密钥放在应用程序启动代码中,且位于所有 IronPDF 调用之前:

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

在开发和评估过程中,IronPDF 以试用模式运行,这会在渲染输出中添加水印。 免费试用许可证会移除水印,用于测试目的。

如何设置 Windows Forms 项目结构?

打开 Visual Studio 并创建一个面向 .NET 10 的新 Windows 窗体应用程序项目。项目模板生成一个 Form1.cs 和一个 Program.cs 入口点。

在设计器界面中,将以下控件添加到表单中:

  • 一个名为 PictureBoxpictureBoxPdf 覆盖了表单的大部分区域,其中 SizeMode 设置为 Zoom
  • 一个名为 ButtonbtnOpen,文本为"打开 PDF"
  • 一个名为 ButtonbtnPrevious,文本为"上一页"
  • 一个名为 ButtonbtnNext,文本为"下一个"
  • 一个名为 ButtonbtnSave,文本为"保存页面"
  • 一个名为 ButtonbtnPrint,文本为"打印"
  • 一个名为 LabellblPageInfo,用于显示当前页码

PictureBox 上的 SizeMode = Zoom 设置可确保 PDF 页面按比例缩放以适应控件,而不会扭曲纵横比。 这一点很重要,因为 PDF 页面的大小各不相同——信纸、A4 和法律格式的尺寸都不同,您希望查看器能够优雅地处理所有这些尺寸。

以下是PDF查看器应用程序的完整表单代码。 这使用了 Program.cs 中的顶级语句,并将完整的查看器逻辑放在 Form1.cs 中:

using IronPdf;
using IronSoftware.Drawing;
using System.Drawing;

public partial class Form1 : Form
{
    private PdfDocument? currentPdf;
    private AnyBitmap[]? pageImages;
    private int currentPageIndex = 0;

    public Form1()
    {
        InitializeComponent();
        pictureBoxPdf.SizeMode = PictureBoxSizeMode.Zoom;
        UpdateNavigationState();
    }

    private void UpdateNavigationState()
    {
        bool hasPdf = pageImages != null && pageImages.Length > 0;
        btnPrevious.Enabled = hasPdf && currentPageIndex > 0;
        btnNext.Enabled = hasPdf && currentPageIndex < (pageImages?.Length - 1 ?? 0);
        btnSave.Enabled = hasPdf;
        btnPrint.Enabled = hasPdf;
        lblPageInfo.Text = hasPdf
            ? $"Page {currentPageIndex + 1} of {pageImages!.Length}"
            : "No document loaded";
    }
}
using IronPdf;
using IronSoftware.Drawing;
using System.Drawing;

public partial class Form1 : Form
{
    private PdfDocument? currentPdf;
    private AnyBitmap[]? pageImages;
    private int currentPageIndex = 0;

    public Form1()
    {
        InitializeComponent();
        pictureBoxPdf.SizeMode = PictureBoxSizeMode.Zoom;
        UpdateNavigationState();
    }

    private void UpdateNavigationState()
    {
        bool hasPdf = pageImages != null && pageImages.Length > 0;
        btnPrevious.Enabled = hasPdf && currentPageIndex > 0;
        btnNext.Enabled = hasPdf && currentPageIndex < (pageImages?.Length - 1 ?? 0);
        btnSave.Enabled = hasPdf;
        btnPrint.Enabled = hasPdf;
        lblPageInfo.Text = hasPdf
            ? $"Page {currentPageIndex + 1} of {pageImages!.Length}"
            : "No document loaded";
    }
}
Imports IronPdf
Imports IronSoftware.Drawing
Imports System.Drawing

Public Partial Class Form1
    Inherits Form

    Private currentPdf As PdfDocument
    Private pageImages As AnyBitmap()
    Private currentPageIndex As Integer = 0

    Public Sub New()
        InitializeComponent()
        pictureBoxPdf.SizeMode = PictureBoxSizeMode.Zoom
        UpdateNavigationState()
    End Sub

    Private Sub UpdateNavigationState()
        Dim hasPdf As Boolean = pageImages IsNot Nothing AndAlso pageImages.Length > 0
        btnPrevious.Enabled = hasPdf AndAlso currentPageIndex > 0
        btnNext.Enabled = hasPdf AndAlso currentPageIndex < If(pageImages?.Length - 1, 0)
        btnSave.Enabled = hasPdf
        btnPrint.Enabled = hasPdf
        lblPageInfo.Text = If(hasPdf, $"Page {currentPageIndex + 1} of {pageImages.Length}", "No document loaded")
    End Sub
End Class
$vbLabelText   $csharpLabel

UpdateNavigationState 辅助方法会根据 PDF 是否已加载以及查看器当前显示的页面来禁用或启用导航和操作按钮。 这样可以防止用户在加载文档之前点击按钮时出现空引用错误。

!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

如何将 PDF 页面加载并渲染到 PictureBox 中?

将 PDF 加载到 PictureBox 中并渲染到 PdfDocument.FromFile 中涉及两个步骤:使用 PdfDocument.FromFile 加载文档,然后调用 ToBitmap 将所有页面转换为位图对象数组。

PDF 转图像转换方法 ToBitmap 以请求的 DPI 渲染 PDF 中的每一页。 更高的DPI可以产生更清晰的图像,但会占用更多内存。 对于文档查看器来说,150 DPI 通常是一个不错的平衡点——既能在屏幕上清晰阅读,又不会因为显示大型文档而占用大量内存。

private void btnOpen_Click(object sender, EventArgs e)
{
    using OpenFileDialog openDialog = new OpenFileDialog();
    openDialog.Filter = "PDF Files|*.pdf";
    openDialog.Title = "Select a PDF Document";

    if (openDialog.ShowDialog() == DialogResult.OK)
    {
        LoadPdfDocument(openDialog.FileName);
    }
}

private void LoadPdfDocument(string filePath)
{
    currentPdf?.Dispose();
    currentPdf = PdfDocument.FromFile(filePath);

    // Render all pages as bitmaps at 150 DPI
    pageImages = currentPdf.ToBitmap(150);
    currentPageIndex = 0;

    DisplayCurrentPage();
    UpdateNavigationState();
}

private void DisplayCurrentPage()
{
    if (pageImages == null || pageImages.Length == 0) return;

    // Convert AnyBitmap to System.Drawing.Bitmap for PictureBox
    System.Drawing.Bitmap bitmap = pageImages[currentPageIndex].ToImage<System.Drawing.Bitmap>();
    pictureBoxPdf.Image?.Dispose();
    pictureBoxPdf.Image = bitmap;

    UpdateNavigationState();
}
private void btnOpen_Click(object sender, EventArgs e)
{
    using OpenFileDialog openDialog = new OpenFileDialog();
    openDialog.Filter = "PDF Files|*.pdf";
    openDialog.Title = "Select a PDF Document";

    if (openDialog.ShowDialog() == DialogResult.OK)
    {
        LoadPdfDocument(openDialog.FileName);
    }
}

private void LoadPdfDocument(string filePath)
{
    currentPdf?.Dispose();
    currentPdf = PdfDocument.FromFile(filePath);

    // Render all pages as bitmaps at 150 DPI
    pageImages = currentPdf.ToBitmap(150);
    currentPageIndex = 0;

    DisplayCurrentPage();
    UpdateNavigationState();
}

private void DisplayCurrentPage()
{
    if (pageImages == null || pageImages.Length == 0) return;

    // Convert AnyBitmap to System.Drawing.Bitmap for PictureBox
    System.Drawing.Bitmap bitmap = pageImages[currentPageIndex].ToImage<System.Drawing.Bitmap>();
    pictureBoxPdf.Image?.Dispose();
    pictureBoxPdf.Image = bitmap;

    UpdateNavigationState();
}
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Using openDialog As New OpenFileDialog()
        openDialog.Filter = "PDF Files|*.pdf"
        openDialog.Title = "Select a PDF Document"

        If openDialog.ShowDialog() = DialogResult.OK Then
            LoadPdfDocument(openDialog.FileName)
        End If
    End Using
End Sub

Private Sub LoadPdfDocument(filePath As String)
    currentPdf?.Dispose()
    currentPdf = PdfDocument.FromFile(filePath)

    ' Render all pages as bitmaps at 150 DPI
    pageImages = currentPdf.ToBitmap(150)
    currentPageIndex = 0

    DisplayCurrentPage()
    UpdateNavigationState()
End Sub

Private Sub DisplayCurrentPage()
    If pageImages Is Nothing OrElse pageImages.Length = 0 Then Return

    ' Convert AnyBitmap to System.Drawing.Bitmap for PictureBox
    Dim bitmap As System.Drawing.Bitmap = pageImages(currentPageIndex).ToImage(Of System.Drawing.Bitmap)()
    pictureBoxPdf.Image?.Dispose()
    pictureBoxPdf.Image = bitmap

    UpdateNavigationState()
End Sub
$vbLabelText   $csharpLabel

PdfDocument.FromFile 方法将 PDF 从磁盘加载到内存中。 调用 ToBitmap 并带有 DPI 参数,返回一个 AnyBitmap 对象数组,每个页面一个对象。 AnyBitmap 类型是 IronPDF 的与格式无关的位图类,而 ToImage<System.Drawing.Bitmap>() 将其转换为 PictureBox.Image 所期望的 System.Drawing.Bitmap 类型。

请注意,在分配新对象之前,之前的 PictureBox.Image 已被释放。 这很重要——每个 Bitmap 对象都包含未托管的 GDI+ 资源,未能释放旧位图会导致长时间运行的应用程序出现内存泄漏。

!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 表单示例。

加载完成后,查看器立即显示第一页。 lblPageInfo 标签更新,显示当前页码和总页数。

!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 的输入示例。

如何在PDF页面之间添加页面导航?

只能显示第一页的 PDF 查看器对于多页文档来说并不实用。 导航按钮允许用户在已渲染的页面数组中向前和向后移动:

private void btnPrevious_Click(object sender, EventArgs e)
{
    if (currentPageIndex > 0)
    {
        currentPageIndex--;
        DisplayCurrentPage();
    }
}

private void btnNext_Click(object sender, EventArgs e)
{
    if (pageImages != null && currentPageIndex < pageImages.Length - 1)
    {
        currentPageIndex++;
        DisplayCurrentPage();
    }
}
private void btnPrevious_Click(object sender, EventArgs e)
{
    if (currentPageIndex > 0)
    {
        currentPageIndex--;
        DisplayCurrentPage();
    }
}

private void btnNext_Click(object sender, EventArgs e)
{
    if (pageImages != null && currentPageIndex < pageImages.Length - 1)
    {
        currentPageIndex++;
        DisplayCurrentPage();
    }
}
Private Sub btnPrevious_Click(sender As Object, e As EventArgs)
    If currentPageIndex > 0 Then
        currentPageIndex -= 1
        DisplayCurrentPage()
    End If
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs)
    If pageImages IsNot Nothing AndAlso currentPageIndex < pageImages.Length - 1 Then
        currentPageIndex += 1
        DisplayCurrentPage()
    End If
End Sub
$vbLabelText   $csharpLabel

每次点击事件处理程序都会将 currentPageIndex 的值加一,并调用 DisplayCurrentPage,后者会将 PictureBox.Image 的值替换为对应的位图。边界检查(currentPageIndex > 0currentPageIndex < pageImages.Length - 1)可以防止索引越界异常,但 UpdateNavigationState 也会在按钮超出范围时禁用它们。

对于较大的文档,您还可以考虑添加页码输入字段,用户可以在其中输入特定的页码以直接跳转到该页,或者在侧边添加可滚动的缩略图条。pageImages 数组已经包含所有已渲染的页面,因此跳转到任意页面只需将 currentPageIndex 设置为目标索引并调用 DisplayCurrentPage 即可。

!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 页。

如何使用查看器保存和打印 PDF 页面?

文档查看器通常需要支持将当前页面保存为图像文件以及将文档发送到打印机这两个功能。 IronPDF 让这两件事都变得简单易行。

将当前页面另存为图像文件

当前页面已作为 System.Drawing.BitmapPictureBox.Image 中可用。 使用 Image.Save 保存,格式根据用户选择的文件扩展名推断:

private void btnSave_Click(object sender, EventArgs e)
{
    if (pictureBoxPdf.Image == null) return;

    using SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|TIFF Image|*.tif";
    saveDialog.Title = "Save Page As Image";
    saveDialog.FileName = $"page_{currentPageIndex + 1}";

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        pictureBoxPdf.Image.Save(saveDialog.FileName);
    }
}
private void btnSave_Click(object sender, EventArgs e)
{
    if (pictureBoxPdf.Image == null) return;

    using SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|TIFF Image|*.tif";
    saveDialog.Title = "Save Page As Image";
    saveDialog.FileName = $"page_{currentPageIndex + 1}";

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        pictureBoxPdf.Image.Save(saveDialog.FileName);
    }
}
Private Sub btnSave_Click(sender As Object, e As EventArgs)
    If pictureBoxPdf.Image Is Nothing Then Return

    Using saveDialog As New SaveFileDialog()
        saveDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|TIFF Image|*.tif"
        saveDialog.Title = "Save Page As Image"
        saveDialog.FileName = $"page_{currentPageIndex + 1}"

        If saveDialog.ShowDialog() = DialogResult.OK Then
            pictureBoxPdf.Image.Save(saveDialog.FileName)
        End If
    End Using
End Sub
$vbLabelText   $csharpLabel

打印PDF文档

对于打印,请使用 IronPDF 的Print 方法,该方法会将 PDF 直接发送到 Windows 打印基础架构——而不是渲染后的图像,而是实际的 PDF 内容。 与打印位图相比,这种方式可以产生更清晰的输出,因为打印机驱动程序接收的是矢量 PDF 指令,而不是栅格化图像:

private void btnPrint_Click(object sender, EventArgs e)
{
    if (currentPdf == null) return;

    using PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        currentPdf.Print();
    }
}
private void btnPrint_Click(object sender, EventArgs e)
{
    if (currentPdf == null) return;

    using PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        currentPdf.Print();
    }
}
Private Sub btnPrint_Click(sender As Object, e As EventArgs)
    If currentPdf Is Nothing Then Return

    Using printDialog As New PrintDialog()
        If printDialog.ShowDialog() = DialogResult.OK Then
            currentPdf.Print()
        End If
    End Using
End Sub
$vbLabelText   $csharpLabel

PrintDialog 允许用户在打印开始前选择目标打印机并配置设置。 实际的打印作业由 IronPDF 的内部渲染引擎处理,该引擎与 Windows 打印后台处理程序集成。

!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 - 打印按钮工作示例

如何正确处理和处置内存?

PDF 文档和渲染的位图会占用大量内存,尤其是在 DPI 设置较高或文档较大时。 如果没有妥善处置,长时间运行的查看器应用程序会逐渐耗尽可用内存。

处置清单

此模式下正确处置的关键点如下:

  • 在加载新文档之前,请调用 currentPdf?.Dispose()PdfDocument 实现了 IDisposable 并持有非托管资源。
  • 在将新位图分配给 PictureBox.Image 之前,请调用 pictureBoxPdf.Image?.Dispose()。 每个渲染后的页面都是一个独立的非托管 GDI+ 对象。
  • pageImages 数组包含 AnyBitmap 对象。 当您离开页面并且不再需要旧位图时,对 DisposePictureBox.Image 调用会处理 System.Drawing.Bitmap 副本,但您可能还希望在加载新文档时释放数组中的 AnyBitmap 对象。

重写 Form1.Dispose 以在表单关闭时进行清理:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        currentPdf?.Dispose();
        pictureBoxPdf.Image?.Dispose();

        if (pageImages != null)
        {
            foreach (var bitmap in pageImages)
                bitmap.Dispose();
        }

        components?.Dispose();
    }
    base.Dispose(disposing);
}
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        currentPdf?.Dispose();
        pictureBoxPdf.Image?.Dispose();

        if (pageImages != null)
        {
            foreach (var bitmap in pageImages)
                bitmap.Dispose();
        }

        components?.Dispose();
    }
    base.Dispose(disposing);
}
Protected Overrides Sub Dispose(disposing As Boolean)
    If disposing Then
        currentPdf?.Dispose()
        pictureBoxPdf.Image?.Dispose()

        If pageImages IsNot Nothing Then
            For Each bitmap In pageImages
                bitmap.Dispose()
            Next
        End If

        components?.Dispose()
    End If
    MyBase.Dispose(disposing)
End Sub
$vbLabelText   $csharpLabel

这种模式确保在表单关闭时释放所有 IronPDF 和 GDI+ 资源,从而防止生产部署中出现内存泄漏。

如何只渲染可见页面而不是所有页面?

对于包含几十页甚至几百页的大型 PDF 文件,使用 ToBitmap 预先渲染所有页面可能速度很慢且占用大量内存。更高效的做法是按需渲染用户想要查看的页面。

IronPDF 的RasterizeToImageFiles方法支持逐页渲染,但对于内存使用,您也可以使用 PageCount 并一次渲染一个页面:

private void DisplayPageLazy(int pageIndex)
{
    if (currentPdf == null) return;

    // Render only the requested page
    AnyBitmap[] singlePage = currentPdf.ToBitmap(150, pageIndex, pageIndex);

    System.Drawing.Bitmap bitmap = singlePage[0].ToImage<System.Drawing.Bitmap>();
    pictureBoxPdf.Image?.Dispose();
    pictureBoxPdf.Image = bitmap;

    singlePage[0].Dispose();

    lblPageInfo.Text = $"Page {pageIndex + 1} of {currentPdf.PageCount}";
    UpdateNavigationState();
}
private void DisplayPageLazy(int pageIndex)
{
    if (currentPdf == null) return;

    // Render only the requested page
    AnyBitmap[] singlePage = currentPdf.ToBitmap(150, pageIndex, pageIndex);

    System.Drawing.Bitmap bitmap = singlePage[0].ToImage<System.Drawing.Bitmap>();
    pictureBoxPdf.Image?.Dispose();
    pictureBoxPdf.Image = bitmap;

    singlePage[0].Dispose();

    lblPageInfo.Text = $"Page {pageIndex + 1} of {currentPdf.PageCount}";
    UpdateNavigationState();
}
Private Sub DisplayPageLazy(pageIndex As Integer)
    If currentPdf Is Nothing Then Return

    ' Render only the requested page
    Dim singlePage As AnyBitmap() = currentPdf.ToBitmap(150, pageIndex, pageIndex)

    Dim bitmap As System.Drawing.Bitmap = singlePage(0).ToImage(Of System.Drawing.Bitmap)()
    pictureBoxPdf.Image?.Dispose()
    pictureBoxPdf.Image = bitmap

    singlePage(0).Dispose()

    lblPageInfo.Text = $"Page {pageIndex + 1} of {currentPdf.PageCount}"
    UpdateNavigationState()
End Sub
$vbLabelText   $csharpLabel

ToBitmap(dpi, startPage, endPage) 重载渲染特定范围的页面。 通过为 startPageendPage 传递相同的索引,每次只渲染一个页面。这虽然会牺牲每次导航的渲染时间,但可以降低峰值内存使用量,对于非常大的文档来说,这种做法是值得的。

有关 IronPDF 图像输出功能的更多信息,请参阅PDF 转图像文档PDF 光栅化指南

一个实用的PDF查看器还应该允许用户放大内容和旋转页面。 对于缩放,动态调整传递给 ToBitmap 的 DPI -- 存储一个 currentDpi 字段(从 150 开始),在缩放按钮点击时将其增加或减少 50,然后使用更新后的 DPI 调用延迟页面渲染器。 更高的 DPI 会产生更大的图像,PictureBoxZoom 模式下会缩放图像以适应控件。 使用 Math.MinMath.Max 将 DPI 范围限制在 72 到 400 DPI 之间,以避免低端输出模糊或高端内存消耗过大。

有关更高级的渲染选项,包括从 PDF 中提取文本以及图像渲染,请参阅PDF 文本提取指南PDF 图像提取文档

您还可以探索 IronPDF 对合并 PDF 文档添加水印旋转 PDF 页面添加注释等功能的支持,这些功能在更高级的查看器中也能实现。

如何为 Windows Forms 选择合适的 PDF 渲染库?

在为 Windows Forms 选择 PDF 渲染库时,有多种选择。以下是一个简单的比较,可帮助您评估各种选项:

Windows窗体应用程序的PDF渲染选项
选项 外部依赖 .NET支持 商业许可 图像输出
IronPDF 无(独立式) .NET 4.6.2 -- .NET 10 是的(提供试用) 可配置DPI下的高保真度
Adobe Acrobat COM 已安装 Adobe Reader/Acrobat 仅限 Windows .NET Framework Adobe 许可 取决于已安装的版本
PDFium.NET PDFium 原生二进制文件 .NET 5+ BSD(开源) 质量好,可手动调节DPI
GhostScript 封装器 已安装 GhostScript 任何方式(通过 CLI 或 COM) AGPL 或商业用途 画质好,但渲染速度较慢

IronPDF 在 Windows Forms 用例中的主要优势在于它是完全独立的——无需在最终用户的计算机上安装任何外部软件。 IronPDF 的许可页面详细介绍了定价和免费试用选项。 您可以在NuGet.org上找到该软件包,以及下载统计信息和版本历史记录。 要深入了解 PDF 规范如何定义页面渲染,ISO 发布的ISO 32000-2 PDF 标准描述了 IronPDF 和其他渲染器必须实现的格式。

要了解 IronPDF 除了图像渲染之外的其他功能,请参阅IronPDF 文档主页,其中涵盖了 PDF 创建、HTML 到 PDF 的转换、表单处理、数字签名等等。

下一步计划是什么?

本教程中所示的模式——使用 ToBitmap 渲染,使用 PictureBox 显示,使用基于索引的控件导航——为您提供了一个功能齐全、无依赖项的 Windows Forms PDF 查看器。 从这里出发,根据您的应用程序需求,有几个方向值得探索。

对于生产级查看器,可以考虑使用延迟页面渲染来高效处理大型文档,在主查看器旁边添加可滚动缩略图面板,并显示与 DPI 参数关联的缩放控件。 对于文档管理应用程序,将查看器与 IronPDF 的文本提取表单字段读取功能相结合,以构建可搜索的文档索引。

如果您的工作流程涉及在显示 PDF 之前以编程方式生成 PDF,IronPDF 还支持HTML 到 PDF 的转换以及从头开始创建 PDF ,因此同一个库可以处理生成和查看。

开始免费试用,获取用于开发的无水印许可证密钥,或查看完整的 API 参考,了解 PdfDocumentAnyBitmap 和支持的渲染类中可用的所有方法。 IronPDF 示例库还提供了可直接运行的代码示例,涵盖了 C# 中的常见 PDF 任务。

常见问题解答

如何在Windows应用程序的PictureBox中使用C#显示PDF?

使用IronPDF的ToBitmap方法将PDF页面渲染为AnyBitmap对象,将每个对象转换为System.Drawing.Bitmap,并分配给PictureBox.Image。

IronPDF提供了什么方法在C#中渲染PDF页面?

IronPDF提供了ToBitmap方法,可在可配置DPI下将PDF页面渲染为AnyBitmap对象。调用ToImage()以转换为PictureBox用途。

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

是的。将渲染的页面位图存储在数组中,增加或减少一个页面索引变量。在每次导航点击时,将相应的位图分配给PictureBox.Image。

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

IronPDF是完全独立的,无需安装额外软件。它支持可配置的DPI渲染、页面导航、打印和文本提取,不依赖于Adobe Acrobat。

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

是的。ToBitmap为每页返回一个AnyBitmap。存储数组并按索引导航,或使用ToBitmap(dpi, startPage, endPage)重载按需进行逐页渲染。

Curtis Chau
技术作家

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

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

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我