/* Antenna House PDF Tool API V8.0 .NET Interface sample program 概要:テキストオブジェクトの挿入 Copyright 2013-2025 Antenna House, Inc. */ using PdfTkNet; using System; using System.Diagnostics; using System.Xml; namespace WriteTextPageNo { class WriteTextPageNo { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("usage: WriteTextPageNo.exe in-pdf-file out-pdf-file"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) { // PDFファイルをロードします。 doc.load(inputFile); using (PtlPages pages = doc.getPages()) { // ページコンテナが空かどうか if (pages.isEmpty()) { Console.WriteLine("ページコンテナが空"); return; } int pageno = pages.getCount(); // 現在日時取得 DateTime today = DateTime.Today; int year = today.Year; int month = today.Month; int day = today.Day; for (int i = 0; i < pageno; i++) { // ページの取得 using (PtlPage page = pages.get(i)) { string str = "実行日:" + year + "/" + month + "/" + day + " ページ番号:" + (i + 1) + "/ " + pageno; // テキスト追加 writeString(page, str); } } } // ファイルに保存します。 doc.save(outputFile); } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("-- 完了 --"); } } static void writeString(PtlPage page, string str) { using (PtlContent content = page.getContent()) // ページコンテントの取得 { PtlSize pageSize = page.getSize(); // 文字列出力 using (PtlRect rect = new PtlRect(0.0f, 0.0f, pageSize.getWidth(), pageSize.getHeight())) using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス { // フォント指定に使うパラメータクラス using (PtlParamFont font = new PtlParamFont()) { // フォント名の設定 font.setName("游ゴシック"); // サイズの設定 font.setSize(12.0f); // フォントの設定 writestring.setFont(font); } // 文字色設定 writestring.setTextColor(new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)); writestring.setOutlineColor(new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f)); // 文字列出力 content.writeString(rect, PtlContent.ALIGN.ALIGN_TOP_RIGHT, str, writestring); } } } } }