OEM販売のご相談
ご相談ください!

PDF Tool APIサンプル集:ページの拡大・縮小
(全ページをA4縦サイズに変更する例)

PDFファイルの全ページを指定のサイズに拡大あるいは縮小するコンソールアプリケーションです。

概要

入力PDFファイルの全ページをA4縦のサイズに変更し出力します。

コマンドラインでの実行例

sample13.exe c:\in\test13.pdf c:\sav\output13.pdf

ダウンロード

サンプルコード

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
    Antenna House PDF Tool API 7.0
    C# Interface sample program
  
    概要:ページの拡大・縮小
  
    Copyright 2023 Antenna House,Inc.
 */
 using System;
 using PdfTkNet;
 
namespace sample13
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: sample13.exe in-pdf-file out-pdf-file");
                return;
            }
 
            try
            {
                using (PtlParamInput aInput = new PtlParamInput(args[0]))
                using (PtlParamOutput aOutput = new PtlParamOutput(args[1]))
                using (PtlPDFDocument aDoc = new PtlPDFDocument())
                using(PtlRect A4V = new PtlRect(0, 0, 210, 297))
                {
                    aDoc.load(aInput);
                    PtlRect rectNew = A4V;
 
                    using (PtlPages pages = aDoc.getPages())
                    {
                        int numPages = aDoc.getPageCount();  //ページ数を取得する
                        for(int i = 0;i < numPages;i++)
                        {
                            using(PtlPage page = pages.get(i))
                            using(PtlSize pageSizeOld = page.getSize())  //ページサイズを取得する
                            {
                                float rateWidth = rectNew.getRight() / pageSizeOld.getWidth();  //拡大縮小率を算出する
                                float rateHeight = rectNew.getTop() / pageSizeOld.getHeight();  //拡大縮小率を算出する
                                float rate = System.Math.Min(rateWidth,rateHeight);
                                page.zoom(rate);  //ページを拡大縮小する
                                page.setViewBox(rectNew);   //ページの表示域を設定する
                            }
                        }
                    }
                    aDoc.save(aOutput);
 
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("-- 完了 --");
            }
        }
    }
}