/* Antenna House PDF Tool API V8.0 .NET Interface sample program 概要:テキストオブジェクトの挿入 Copyright 2013-2025 Antenna House, Inc. */ using System; using PdfTkNet; namespace WriteString { class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("usage: WriteString.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; } using (PtlPage page = pages.get(0)) // 先頭ページ { // テキスト追加 writeString(page); } } // ファイルに保存します。 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) { using (PtlContent content = page.getContent()) // ページコンテントの取得 { // 絵文字出力 using (PtlRect rect = new PtlRect(0.0f, 250.0f, 300.0f, 500.0f)) using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス { // フォント指定に使うパラメータクラス using (PtlParamFont font = new PtlParamFont()) { // フォント名の設定 font.setName("Segoe UI Emoji"); // サイズの設定 font.setSize(80.0f); // フォントの設定 writestring.setFont(font); } // 文字色設定 using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) { writestring.setTextColor(colorText); } // 文字列出力 content.writeString(rect, PtlContent.ALIGN.ALIGN_CENTER, "🌝🌞🐄🐈🐉", writestring); } // 文字列出力 using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f)) using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス { // フォント指定に使うパラメータクラス using (PtlParamFont font = new PtlParamFont()) { // フォント名の設定 font.setName("MS明朝"); // サイズの設定 font.setSize(80.0f); // フォントの設定 writestring.setFont(font); } // 文字色設定 using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) { writestring.setTextColor(colorText); } // 文字列出力 content.writeString(rect, PtlContent.ALIGN.ALIGN_CENTER, "writeString Test 1", writestring); } // 文字列回転出力 using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f)) using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス { // フォント指定に使うパラメータクラス using (PtlParamFont font = new PtlParamFont()) { // フォント名の設定 font.setName("MS-Gothic"); // サイズの設定 font.setSize(40.0f); // フォントの設定 writestring.setFont(font); } // 文字色設定 using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f)) { writestring.setTextColor(colorText); } // 文字列出力 content.writeString(rect, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 45.0f, "writeString Test 2", writestring); } // 縦書きの文字列出力 using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 200.0f)) using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス { // フォント指定に使うパラメータクラス using (PtlParamFont font = new PtlParamFont()) { // フォント名の設定 font.setName("MSP-Mincho"); // サイズの設定 font.setSize(60.0f); // ウエイトの設定 font.setWeight(PtlParamFont.WEIGHT.WEIGHT_HEAVY); // フォントの設定 writestring.setFont(font); } // 文字色設定 using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f)) { writestring.setTextColor(colorText); } // 文字列出力 content.writeStringV(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, "writeString Test 3", writestring); } } } } }