Spire.PDF for .NET【页面设置】演示:更改 PDF 页面大小

在某些情况下,您可能需要更改 PDF 文件的页面大小。例如,您需要打印 PDF 文件,但其页面大小与打印机使用的纸张大小不同。在本文中,我们将演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中更改 PDF 文件的页面大小。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。可以从此链接下载 DLL 文件,也可以通过NuGet安装。

复制代码
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中将 PDF 页面大小更改为标准纸张大小

更改 PDF 文件的页面大小的方法是创建一个新的 PDF 文件并向其中添加所需大小的页面,然后根据原始 PDF 文件中的页面创建模板,并将模板绘制到新 PDF 文件中的页面上。此过程将保留原始 PDF 文件中的文本、图像和其他元素。

Spire.PDF for .NET 支持多种标准纸张尺寸,如 letter、legal、A0、A1、A2、A3、A4、B0、B1、B2、B3、B4 等。以下步骤向您展示如何将 PDF 文件的页面大小更改为标准纸张大小:

  • 初始化PdfDocument实例并使用 **PdfDocument.LoadFromFile()**方法加载原始 PDF 文件。
  • 初始化另一个PdfDocument实例来创建一个新的 PDF 文件。
  • 循环浏览原始 PDF 中的页面。
  • **使用PdfDocument.Pages.Add()**方法将所需大小的页面添加到新的 PDF 文件。
  • 初始化一个PdfTextLayout实例,并通过 PdfTextLayout.Layout属性将文本布局设置为一页。
  • **使用PdfPageBase.CreateTemplate()**方法根据原始 PDF 中的页面创建模板。
  • **使用PdfTemplate.Draw()**方法将模板以指定的文本布局绘制到新 PDF 文件的页面上。
  • **使用PdfDocument.SaveToFile()**方法保存结果文件。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ChangePageSizeToStandardPaperSize
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument originPdf = new PdfDocument();
//Load the original PDF document
originPdf.LoadFromFile("Sample.pdf");

//Create a new PDF document
PdfDocument newPdf = new PdfDocument();

//Loop through the pages in the original PDF
foreach(PdfPageBase page in originPdf.Pages)
{
//Add pages of size A1 to the new PDF
PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));
//Create a PdfTextLayout instance
PdfTextLayout layout = new PdfTextLayout();
//Set text layout as one page (if not set the content will not scale to fit page size)
layout.Layout = PdfLayoutType.OnePage;
//Create templates based on the pages in the original PDF
PdfTemplate template = page.CreateTemplate();
//Draw the templates onto the pages in the new PDF
template.Draw(newPage, new PointF(0, 0), layout);
}

//Save the result document
newPdf.SaveToFile("ChangePageSizeToA1.pdf");
}
}
}

VB.NET

复制代码
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace ChangePageSizeToStandardPaperSize
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim originPdf As PdfDocument = New PdfDocument()
'Load the original PDF document
originPdf.LoadFromFile("Sample.pdf")

'Create a new PDF document
Dim newPdf As PdfDocument = New PdfDocument()

'Loop through the pages in the original PDF
For Each page As PdfPageBase In originPdf.Pages
'Add pages of size A1 to the new PDF
Dim newPage As PdfPageBase = newPdf.Pages.Add(PdfPageSize.A1, New PdfMargins(0))
'Create a PdfTextLayout instance
Dim layout As PdfTextLayout = New PdfTextLayout()
'Set text layout as one page (if not set the content will not scale to fit page size)
layout.Layout = PdfLayoutType.OnePage
'Create templates based on the pages in the original PDF
Dim template As PdfTemplate = page.CreateTemplate()
'Draw the templates onto the pages in the new PDF
template.Draw(newPage, New PointF(0, 0), layout)
Next

'Save the result document
newPdf.SaveToFile("ChangePageSizeToA1.pdf")
End Sub
End Class
End Namespace
在 C# 和 VB.NET 中将 PDF 页面大小更改为自定义纸张大小

Spire.PDF for .NET 使用点(1/72 英寸)作为测量单位。如果您需要将 PDF 的页面大小更改为其他测量单位(如英寸或毫米)的自定义纸张大小,则可以使用PdfUnitConvertor类将它们转换为点。

以下步骤向您展示如何将 PDF 文件的页面大小更改为以英寸为单位的自定义纸张大小:

  • 初始化PdfDocument实例并使用 **PdfDocument.LoadFromFile()**方法加载原始 PDF 文件。
  • 初始化另一个PdfDocument实例来创建一个新的 PDF 文件。
  • 初始化PdfUnitConvertor实例,然后使用 **PdfUnitConvertor.ConvertUnits()**方法将自定义尺寸(以英寸为单位)转换为点。
  • 从自定义大小初始化SizeF实例。
  • 循环浏览原始 PDF 中的页面。
  • **使用PdfDocument.Pages.Add()**方法将自定义大小的页面添加到新的 PDF 文件。
  • 初始化一个PdfTextLayout实例,并通过 PdfTextLayout.Layout属性将文本布局设置为一页。
  • **使用PdfPageBase.CreateTemplate()**方法从原始 PDF 中的页面创建模板。
  • **使用PdfTemplate.Draw()**方法将模板以指定的文本布局绘制到新 PDF 文件的页面上。
  • **使用PdfDocument.SaveToFile()**方法保存结果文件。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ChangePageSizeToCustomPaperSize
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument originPdf = new PdfDocument();
//Load the original PDF document
originPdf.LoadFromFile("Sample.pdf");

//Create a new PDF document
PdfDocument newPdf = new PdfDocument();

//Create a PdfUnitConvertor instance
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
//Convert the custom size in inches to points
float width = unitCvtr.ConvertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
float height = unitCvtr.ConvertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
//Create a new SizeF instance from the custom size, then it will be used as the page size of the new PDF
SizeF size = new SizeF(width, height);

//Loop through the pages in the original PDF
foreach (PdfPageBase page in originPdf.Pages)
{
//Add pages of the custom size (6.5*8.5 inches) to the new PDF
PdfPageBase newPage = newPdf.Pages.Add(size, new PdfMargins(0));
//Create a PdfTextLayout instance
PdfTextLayout layout = new PdfTextLayout();
//Set text layout as one page (if not set the content will not scale to fit page size)
layout.Layout = PdfLayoutType.OnePage;
//Create templates based on the pages in the original PDF
PdfTemplate template = page.CreateTemplate();
//Draw the templates onto the pages in the new PDF
template.Draw(newPage, new PointF(0, 0), layout);
}

//Save the result document
newPdf.SaveToFile("ChangePageSizeToCustomSize.pdf");
}
}
}

VB.NET

复制代码
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace ChangePageSizeToCustomPaperSize
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim originPdf As PdfDocument = New PdfDocument()
'Load the original PDF document
originPdf.LoadFromFile("Sample.pdf")

'Create a new PDF document
Dim newPdf As PdfDocument = New PdfDocument()

'Create a PdfUnitConvertor instance
Dim unitCvtr As PdfUnitConvertor = New PdfUnitConvertor()
'Convert the custom size in inches to points
Dim width As Single = unitCvtr.ConvertUnits(6.5F, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
Dim height As Single = unitCvtr.ConvertUnits(8.5F, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
'Create a new SizeF instance from the custom size, then it will be used as the page size of the new PDF
Dim size As SizeF = New SizeF(width, height)

'Loop through the pages in the original PDF
For Each page As PdfPageBase In originPdf.Pages
'Add pages of the custom size (6.5*8.5 inches) to the new PDF
Dim newPage As PdfPageBase = newPdf.Pages.Add(size, New PdfMargins(0))
'Create a PdfTextLayout instance
Dim layout As PdfTextLayout = New PdfTextLayout()
'Set text layout as one page (if not set the content will not scale to fit page size)
layout.Layout = PdfLayoutType.OnePage
'Create templates based on the pages in the original PDF
Dim template As PdfTemplate = page.CreateTemplate()
'Draw the templates onto the pages in the new PDF
template.Draw(newPage, New PointF(0, 0), layout)
Next

'Save the result document
newPdf.SaveToFile("ChangePageSizeToCustomSize.pdf")
End Sub
End Class
End Namespace
相关推荐
且白15 分钟前
vsCode使用本地低版本node启动配置文件
前端·vue.js·vscode·编辑器
程序研15 分钟前
一、ES6-let声明变量【解刨分析最详细】
前端·javascript·es6
AAA_自动化工程师25 分钟前
TIA博途中的程序导出为PDF格式的具体方法示例
pdf·tia博途·程序导出·pdf格式·具体方法
行云流水剑40 分钟前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
siwangqishiq240 分钟前
Vulkan Tutorial 教程翻译(四) 绘制三角形 2.2 呈现
前端
李三岁_foucsli42 分钟前
js中消息队列和事件循环到底是怎么个事,宏任务和微任务还存在吗?
前端·chrome
尽欢i42 分钟前
HTML5 拖放 API
前端·html
IDRSolutions_CN1 小时前
PDF 转 HTML5 —— HTML5 填充图形不支持 Even-Odd 奇偶规则?(第二部分)
java·经验分享·pdf·软件工程·团队开发
PasserbyX1 小时前
一句话解释JS链式调用
前端·javascript
1024小神1 小时前
tauri项目,如何在rust端读取电脑环境变量
前端·javascript