技術記事

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% に縮小します。