翻譯|行業資訊|編輯:胡濤|2024-12-03 10:38:05.860|閱讀 89 次
概述:在本文中,您將學習如何通過壓縮內容來擴大 PDF 頁邊距。歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
PDF 頁邊距是正文內容和頁面邊緣之間的空白。與 Word 不同,PDF 文檔中的頁邊距不易修改,因為 Adobe 不提供任何功能供用戶自由操作頁邊距。但是,您可以更改頁面縮放比例(放大/壓縮內容)或裁剪頁面以獲得合適的頁邊距。在本文中,您將學習如何通過壓縮內容來擴大 PDF 頁邊距。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
步驟 1:創建一個 PdfDocument 對象來加載原始 PDF 文檔。
PdfDocument origDoc = new PdfDocument(); origDoc.LoadFromFile("sample.pdf");
第 2 步:創建另一個 PdfDocument 對象。
PdfDocument destDoc = new PdfDocument();
步驟 3:設置您想要添加到現有 PDF 文檔邊距的增量。
float top = 50; float bottom = 50; float left = 50; float right = 50;
步驟 4:將原始文檔中的壓縮內容傳輸到新的 PDF 文檔。
foreach (PdfPageBase page in origDoc.Pages) { PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0)); newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height); newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top)); }
步驟 5:保存到文件。
destDoc.SaveToFile("result.pdf", FileFormat.PDF);
原始PDF:
結果:
完整代碼:
[C#]
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace ChangeMargins { class Program { static void Main(string[] args) { PdfDocument origDoc = new PdfDocument(); origDoc.LoadFromFile("sample.pdf"); PdfDocument destDoc = new PdfDocument(); float top = 50; float bottom = 50; float left = 50; float right = 50; foreach (PdfPageBase page in origDoc.Pages) { PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0)); newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height); newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top)); } destDoc.SaveToFile("result.pdf", FileFormat.PDF); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace ChangeMargins Class Program Private Shared Sub Main(args As String()) Dim origDoc As New PdfDocument() origDoc.LoadFromFile("sample.pdf") Dim destDoc As New PdfDocument() Dim top As Single = 50 Dim bottom As Single = 50 Dim left As Single = 50 Dim right As Single = 50 For Each page As PdfPageBase In origDoc.Pages Dim newPage As PdfPageBase = destDoc.Pages.Add(page.Size, New PdfMargins(0)) newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height) newPage.Canvas.DrawTemplate(page.CreateTemplate(), New PointF(left, top)) Next destDoc.SaveToFile("result.pdf", FileFormat.PDF) End Sub End Class End Namespace
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢慧都在線客服 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn