PDF Tool APIサンプル集:ページの拡大・縮小
(全ページをA4縦サイズに変更する例)
PDFファイルの全ページを指定のサイズに拡大あるいは縮小するコンソールアプリケーションです。
概要
入力PDFファイルの全ページをA4縦のサイズに変更し出力します。
コマンドラインでの実行例
sample13.exe c:\in\test13.pdf c:\sav\output13.pdf
ダウンロード
サンプルコード
/*
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("-- 完了 --");
}
}
}
}

