IRONSOFTWAREHOME
在 C# 中缩放 PDF 对象
using IronPdf;
using System.Linq;

// Create a PDF from a URL using a PNG image
string html = @"<img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTi8LuOR6_A98euPLs-JRwoLU7Nc31nVP15rw&s'>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Access the first image object on the first page
var image = pdf.Pages.First().ObjectModel.ImageObjects.First();

// To scale the image to 70% of its original size uniformly
image.Scale = new System.Drawing.PointF(0.7f, 0.7f);

// Save the PDF with the scaled image
pdf.SaveAs("scaled_image.pdf");
Imports IronPdf
Imports System.Linq

' Create a PDF from a URL using a PNG image
Dim html As String = "<img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTi8LuOR6_A98euPLs-JRwoLU7Nc31nVP15rw&s'>"
Dim renderer As New ChromePdfRenderer()
' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Access the first image object on the first page
Dim image = pdf.Pages.First().ObjectModel.ImageObjects.First()

' To scale the image to 70% of its original size uniformly
image.Scale = New System.Drawing.PointF(0.7F, 0.7F)

' Save the PDF with the scaled image
pdf.SaveAs("scaled_image.pdf")
NuGet Download
PM > Install-Package IronPdf
在 C# 中缩放 PDF 对象

在 C# 中缩放 PDF 对象

IronPDF使开发人员能够以编程方式缩放PDF对象,允许对PDF中的文本和图像等元素进行精确控制,而无需修改原始文件或创建全新的文件。

使用IronPDF库,您可以轻松缩放PDF对象。 下面的示例演示了如何仅用几行代码完成此任务。

代码解释

在这个例子中,我们首先创建一个包含&lt;img&gt;标签的HTML字符串,以嵌入图像。 之后,使用PdfDocument对象。

要访问我们刚刚添加的ImageObject,我们需要浏览文档的结构。 我们首先使用Pages.First访问PDF的第一页。 从那里,我们深入其ObjectModel,它包含页面的结构化内容。 然后我们访问ImageObjects集合,检索第一个元素,并将其分配给一个变量。

为了缩放图像,我们为PointF。 在本例中,我们将图像以其原始大小的70%在x轴和y轴上进行缩放。 注意,缩放因数大于1将增加大小,而小于1(但大于0)的因数将有效地缩小图像。 您还可以通过为x和y提供不同的值来进行非均匀缩放。

修改SaveAs方法以保存应用更改的PDF。

了解如何轻松扩展 PDF DOM - 立即访问我们的指南!
在 GitHub 上查看

准备开始了吗?

Nuget Downloads 20,878,335版本:2026.9刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户