您的位置:首页 > 教育 > 培训 > 重庆开发app的公司_龙口市最新公告_网络营销组织的概念_win7系统优化

重庆开发app的公司_龙口市最新公告_网络营销组织的概念_win7系统优化

2025/8/9 7:51:18 来源:https://blog.csdn.net/m0_67129275/article/details/143885453  浏览:    关键词:重庆开发app的公司_龙口市最新公告_网络营销组织的概念_win7系统优化
重庆开发app的公司_龙口市最新公告_网络营销组织的概念_win7系统优化

在某些情况下,您可能需要更改 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.DrawingNamespace 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 页面大小

在 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.DrawingNamespace 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

C#/VB.NET:更改 PDF 页面大小

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com