技术文章

使用 Delphi、C# 和 VB.Net 中的 losLab PDF 库将 PDF 页面缩放 70%

· PDF 编程
通过 losLab PDF 库,将 PDF 页面缩小 70%。 losLab PDF 库

在处理 PDF 文件时,通常需要调整内容大小以满足各种需求。 在这种情况下,我们的目标是将 PDF 文件中所有页面的大小缩小 70%。 本指南将介绍必要的步骤,解答相关问题并提供解决方案。

问题描述

我们需要将 PDF 文档中的每一页缩小 70%,同时保持原始页面的顺序。 这需要:

  1. 加载原始 PDF 文件。
  2. 捕获并调整每页的大小。
  3. 将调整大小后的页面保存到新的 PDF 文件中。

实现目标的步骤。

  1. 初始化环境:
    • 加载原始 PDF 文件。
    • 删除任何已存在的输出文件,以避免冲突。
  2. 设置缩放参数:
    • 定义缩放因子(70%)。
    • 计算用于使缩放内容居中的边界。
  3. 循环处理每页:
    • 选择第一页。
    • 获取页面的内容。
    • 创建一个具有原始尺寸的新页面。
    • 将捕获并缩放的内容绘制到新页面上。
    • 对所有页面重复此操作。
  4. 保存新的 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% 的比例缩小,同时保持原始顺序。