通過 losLab PDF 庫,將 PDF 頁面縮小 70%。 losLab PDF 庫
在處理 PDF 檔案時,通常需要調整內容大小以滿足各種需求。 在這種情況下,我們的目標是將 PDF 檔案中所有頁面的大小縮小 70%。 本指南將介紹必要的步驟,解答相關問題並提供解決方案。
問題描述
我們需要將 PDF 文件中的每一頁縮小 70%,同時保持原始頁面的順序。 這需要:
- 載入原始 PDF 檔案。
- 捕獲並調整每頁的大小。
- 將調整大小後的頁面儲存到新的 PDF 檔案中。
實現目標的步驟。
- 初始化環境:
- 載入原始 PDF 檔案。
- 刪除任何已存在的輸出檔案,以避免衝突。
- 設定縮放參數:
- 定義縮放因子(70%)。
- 計算用於使縮放內容居中的邊界。
- 迴圈處理每頁:
- 選擇第一頁。
- 獲取頁面的內容。
- 建立一個具有原始尺寸的新頁面。
- 將捕獲並縮放的內容繪製到新頁面上。
- 對所有頁面重複此操作。
- 儲存新的 PDF 檔案並開啟它:
- 將修改後的頁面儲存到新的 PDF 檔案中。
- 自動開啟新的 PDF 檔案以檢視結果。
程式碼實現
以下是執行上述步驟的 C# 程式碼:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
private void button_Click(object sender, EventArgs e) { // Delete the old file if it exists to avoid any conflicts. File.Delete("newpages.pdf"); // Define variables for page dimensions and scaling factors. double pageWidth, pageHeight, horizBorder, vertBorder; double scaleFactor = 0.70; // 70% scaling reduction. int capturedPageId; int ret; // Load the original PDF document. PDFL.LoadFromFile("Pages.pdf"); PDFL.SetOrigin(1); // Get the total number of pages in the document. int numPages = PDFL.PageCount(); // Loop through all pages to process each one. for (int i = 1; i <= numPages; i++) { // Always select the first page as the pages get deleted after capture. PDFL.SelectPage(1); // Retrieve the dimensions of the current page. pageWidth = PDFL.PageWidth(); pageHeight = PDFL.PageHeight(); // Calculate the borders to center the scaled page content. horizBorder = pageWidth * (1.0 - scaleFactor) / 2; vertBorder = pageHeight * (1.0 - scaleFactor) / 2; // Capture the content of the first page. This action deletes the page from the document. capturedPageId = PDFL.CapturePage(1); // Create a new page with the original dimensions. int pageId = PDFL.NewPage(); PDFL.SetPageDimensions(pageWidth, pageHeight); // Draw the captured page content onto the new page with the specified scaling. ret = PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder); } // Save the modified document as a new PDF file. PDFL.SaveToFile("newpages.pdf"); // Open the newly created PDF file for review. System.Diagnostics.Process.Start(@"newpages.pdf"); |
說明和理由
- 檔案刪除: 確保清除任何之前的輸出,以防止錯誤或過時的內容。
- 縮放因子: 設定為 0.70,這會將內容的大小縮減到原始大小的 70%。
- 邊框計算: 將縮放後的內容居中於原始頁面尺寸內。
- 頁面處理迴圈: 迴圈遍歷所有頁面,依次捕獲、縮放和繪製每個頁面。
- 檔案儲存和開啟: 完成新文件,並將其開啟供使用者驗證更改。
通過這種結構化的方法,我們確保 PDF 中的每一頁都以一致的比例縮放,並保持其原始順序,從而生成一份專業處理的文件。
Delphi 版本:
使用 Delphi 將 PDF 頁面的比例縮放至 70%:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
procedure TForm1.ButtonClick(Sender: TObject); var pageWidth, pageHeight, horizBorder, vertBorder: Double; scaleFactor: Double; capturedPageId, ret: Integer; numPages, pageId, i: Integer; begin // Delete the old file if it exists to avoid any conflicts. if FileExists('newpages.pdf') then DeleteFile('newpages.pdf'); // Define the scaling factor (70%). scaleFactor := 0.70; // 70% scaling reduction. // Load the original PDF document. PDFL.LoadFromFile('Pages.pdf'); PDFL.SetOrigin(1); // Get the total number of pages in the document. numPages := PDFL.PageCount(); // Loop through all pages to process each one. for i := 1 to numPages do begin // Always select the first page as the pages get deleted after capture. PDFL.SelectPage(1); // Retrieve the dimensions of the current page. pageWidth := PDFL.PageWidth(); pageHeight := PDFL.PageHeight(); // Calculate the borders to center the scaled page content. horizBorder := pageWidth * (1.0 - scaleFactor) / 2; vertBorder := pageHeight * (1.0 - scaleFactor) / 2; // Capture the content of the first page. This action deletes the page from the document. capturedPageId := PDFL.CapturePage(1); // Create a new page with the original dimensions. pageId := PDFL.NewPage(); PDFL.SetPageDimensions(pageWidth, pageHeight); // Draw the captured page content onto the new page with the specified scaling. ret := PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder); end; // Save the modified document as a new PDF file. PDFL.SaveToFile('newpages.pdf'); // Open the newly created PDF file for review. ShellExecute(0, 'open', 'newpages.pdf', nil, nil, SW_SHOWNORMAL); end; |
VB.Net 版本
以下是用於執行此任務的 VB.Net 程式碼:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
Private Sub button_Click(sender As Object, e As EventArgs) Handles button.Click ' Delete the old file if it exists to avoid any conflicts. If File.Exists("newpages.pdf") Then File.Delete("newpages.pdf") End If ' Define variables for page dimensions and scaling factors. Dim pageWidth, pageHeight, horizBorder, vertBorder As Double Dim scaleFactor As Double = 0.70 ' 70% scaling reduction. Dim capturedPageId, ret As Integer ' Load the original PDF document. PDFL.LoadFromFile("Pages.pdf") PDFL.SetOrigin(1) ' Get the total number of pages in the document. Dim numPages As Integer = PDFL.PageCount() ' Loop through all pages to process each one. For i As Integer = 1 To numPages ' Always select the first page as the pages get deleted after capture. PDFL.SelectPage(1) ' Retrieve the dimensions of the current page. pageWidth = PDFL.PageWidth() pageHeight = PDFL.PageHeight() ' Calculate the borders to center the scaled page content. horizBorder = pageWidth * (1.0 - scaleFactor) / 2 vertBorder = pageHeight * (1.0 - scaleFactor) / 2 ' Capture the content of the first page. This action deletes the page from the document. capturedPageId = PDFL.CapturePage(1) ' Create a new page with the original dimensions. Dim pageId As Integer = PDFL.NewPage() PDFL.SetPageDimensions(pageWidth, pageHeight) ' Draw the captured page content onto the new page with the specified scaling. ret = PDFL.DrawCapturedPage(capturedPageId, horizBorder, vertBorder, pageWidth - 2 * horizBorder, pageHeight - 2 * vertBorder) Next ' Save the modified document as a new PDF file. PDFL.SaveToFile("newpages.pdf") ' Open the newly created PDF file for review. Process.Start("newpages.pdf") End Sub |
VB.Net 和 Delphi 版本的程式碼都實現了與原始 C# 程式碼相同的效果,確保 PDF 中的每一頁都按 70% 的比例縮小,同時保持原始順序。