在文檔處理過程中,調(diào)整 PDF 頁面尺寸是一個常見需求,例如將 A4 文檔適配到 A3 紙張打印、為移動端優(yōu)化顯示尺寸,或統(tǒng)一不同來源 PDF 的頁面規(guī)格。本文將介紹如何使用免費的 Free Spire.PDF for .NET 庫,通過 C# 代碼靈活設(shè)置或修改 PDF 頁面尺寸。
一、安裝免費 .NET 庫
推薦使用 NuGet 進行安裝(最便捷的方式):
Visual Studio 圖形界面操作:
右鍵單擊項目 → 選擇“管理 NuGet 程序包” → 搜索“Free Spire.PDF” → 點擊安裝。或使用 NuGet 命令行:
Install-Package FreeSpire.PDF
注意:Free Spire.PDF for .NET 版本對文檔有 10 頁的限制,適用于頁數(shù)較少的小型文檔處理。
二、設(shè)置/修改 PDF 頁面尺寸
本文采用 “新建文檔 + 模板繪制” 的方式調(diào)整頁面尺寸。該方法通過 PdfTextLayout 的 OnePage 布局模式,自動將原頁面內(nèi)容縮放并完整適配到新尺寸頁面,避免直接修改尺寸造成內(nèi)容裁剪或布局錯亂。
PDF 頁面尺寸的基本單位是 點(Point),常用換算關(guān)系如下:
- 1 英寸 = 72 點
- A4(210mm × 297mm)≈ 595.28 點 × 841.89 點
- A3(297mm × 420mm)≈ 841.89 點 × 1190.55 點
Free Spire.PDF 提供了 PdfUnitConvertor 工具類,可便捷地完成英寸、毫米與點之間的單位換算。
場景1:調(diào)整為標準預設(shè)尺寸(如 A1)
適用于將 PDF 批量轉(zhuǎn)換為標準紙張尺寸(如 A1、A3、Letter 等)。直接使用庫內(nèi)置的 PdfPageSize 枚舉,無需手動計算尺寸。
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace ChangePageSizeToStandardPaperSize
{
class Program
{
static void Main(string[] args)
{
// 1. 加載原始 PDF
PdfDocument originPdf = new PdfDocument();
originPdf.LoadFromFile("Sample.pdf");
// 2. 創(chuàng)建新 PDF 文檔
PdfDocument newPdf = new PdfDocument();
// 3. 遍歷每一頁,重新繪制到 A1 尺寸頁面
foreach(PdfPageBase page in originPdf.Pages)
{
// 添加一個 A1 尺寸的新頁面(無邊距)
PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));
// 設(shè)置布局為單頁適配,確保內(nèi)容完整縮放至新頁面
PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.OnePage;
// 將原頁面創(chuàng)建為模板并繪制到新頁面
PdfTemplate template = page.CreateTemplate();
template.Draw(newPage, new PointF(0, 0), layout);
}
// 4. 保存結(jié)果
newPdf.SaveToFile("PresetSize.pdf");
// 5. 釋放資源
originPdf.Close();
newPdf.Close();
}
}
}
關(guān)鍵點說明:
-
PdfPageSize.A1:直接調(diào)用預設(shè)尺寸,支持 A0–A4、Letter、Legal 等常見規(guī)格。 -
PdfLayoutType.OnePage:確保原頁面內(nèi)容整體縮放并完整放入新頁面。 -
PdfTemplate:將原頁面作為可重用的繪圖模板,實現(xiàn)內(nèi)容遷移。
場景2:調(diào)整為自定義尺寸
適用于需要非標準尺寸的場景(例如 6.5×8.5 英寸)。利用 PdfUnitConvertor 自動完成單位換算,避免手動計算誤差。
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace ChangePageSizeToCustomPaperSize
{
class Program
{
static void Main(string[] args)
{
PdfDocument originPdf = new PdfDocument();
originPdf.LoadFromFile("Sample.pdf");
PdfDocument newPdf = new PdfDocument();
// 單位換算:英寸 → 點
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float width = unitCvtr.ConvertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
float height = unitCvtr.ConvertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
SizeF customSize = new SizeF(width, height);
foreach (PdfPageBase page in originPdf.Pages)
{
// 添加自定義尺寸的頁面
PdfPageBase newPage = newPdf.Pages.Add(customSize, new PdfMargins(0));
PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.OnePage;
PdfTemplate template = page.CreateTemplate();
template.Draw(newPage, new PointF(0, 0), layout);
}
newPdf.SaveToFile("CustomSize.pdf");
originPdf.Close();
newPdf.Close();
}
}
}
關(guān)鍵點說明:
-
PdfUnitConvertor:支持英寸、毫米、點、厘米等單位之間的靈活換算。 -
ConvertUnits方法:傳入數(shù)值、原單位與目標單位,自動返回換算結(jié)果。 -
SizeF:用于定義任意寬度與高度,滿足非標準尺寸需求。
三、注意事項
- 單位統(tǒng)一:確保尺寸的寬高使用相同單位(通常為點),避免因單位不一致導致頁面偏差。
-
內(nèi)容縮放:
PdfLayoutType.OnePage會自動將原內(nèi)容縮放到新頁面內(nèi)。如需保持原始比例或自定義縮放,可結(jié)合ScaleTransform方法進一步控制。 - 格式兼容性:庫生成的 PDF 兼容主流閱讀器(Adobe Acrobat、Chrome、Edge 等)。若原 PDF 包含表單、注釋等復雜元素,調(diào)整尺寸后建議驗證其完整性。
-
資源釋放:操作完成后調(diào)用
Close()方法釋放PdfDocument對象,避免文件被占用導致后續(xù)操作失敗。
該方案完全基于代碼實現(xiàn),無需依賴外部工具,適合集成到 .NET 文檔處理系統(tǒng)中,在中小規(guī)模 PDF 尺寸調(diào)整場景中具有較好的實用性與穩(wěn)定性。