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天試用金鑰
無需信用卡或帳戶建立