翻譯|行業(yè)資訊|編輯:胡濤|2024-11-19 13:33:28.810|閱讀 86 次
概述:在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中更改 PDF 文件的頁面大小。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在某些情況下,您可能需要更改 PDF 文件的頁面大小。例如,您需要打印 PDF 文件,但其頁面大小與打印機(jī)使用的紙張大小不同。在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中更改 PDF 文件的頁面大小。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發(fā)人員可以新建一個 PDF 文檔或者對現(xiàn)有的 PDF 文檔進(jìn)行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
首先,您需要將 Spire.PDF for.NET 包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。可以從此鏈接下載 DLL 文件,也可以通過NuGet安裝。
PM> Install-Package Spire.PDF
更改 PDF 文件的頁面大小的方法是創(chuàng)建一個新的 PDF 文件并向其中添加所需大小的頁面,然后根據(jù)原始 PDF 文件中的頁面創(chuàng)建模板,并將模板繪制到新 PDF 文件中的頁面上。此過程將保留原始 PDF 文件中的文本、圖像和其他元素。
Spire.PDF for .NET 支持多種標(biāo)準(zhǔn)紙張尺寸,如 letter、legal、A0、A1、A2、A3、A4、B0、B1、B2、B3、B4 等。以下步驟向您展示如何將 PDF 文件的頁面大小更改為標(biāo)準(zhǔn)紙張大小:
【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
Spire.PDF for .NET 使用點(1/72 英寸)作為測量單位。如果您需要將 PDF 的頁面大小更改為其他測量單位(如英寸或毫米)的自定義紙張大小,則可以使用PdfUnitConvertor類將它們轉(zhuǎn)換為點。
以下步驟向您展示如何將 PDF 文件的頁面大小更改為以英寸為單位的自定義紙張大小:
【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
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢慧都在線客服 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn