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
相关推荐
郝YH是人间理想2 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core2 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情2 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287562 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔3 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好3 小时前
HTML 表格与div深度解析区别及常见误区
前端·html
运维@小兵3 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js
koiy.cc4 小时前
记录:echarts实现tooltip的某个数据常显和恢复
前端·echarts
一只专注api接口开发的技术猿4 小时前
企业级电商数据对接:1688 商品详情 API 接口开发与优化实践
大数据·前端·爬虫
GISer_Jing4 小时前
[前端高频]数组转树、数组扁平化、深拷贝、JSON.stringify&JSON.parse等手撕
前端·javascript·json