/* Antenna House PDF Tool API V8.0 .NET Interface sample program 概要:矩形内で折り返す文字列 Copyright 2021-2025 Antenna House,Inc. */ using System; using PdfTkNet; namespace DrawTextBox { class DrawTextBox { static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("usage: DrawTextBox_3.exe in-pdf-file out-pdf-file\n"); return; } try { using (PtlParamInput input = new PtlParamInput(args[0])) using (PtlParamOutput output = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) using (PtlOption option = new PtlOption()) { option.setUnit(PtlOption.UNIT.UNIT_PT); doc.load(input); string fontName = "メイリオ"; float fontSize = 24.0f; using (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true)) using (PtlParamFont fontBoldItalic = new PtlParamFont(fontName, fontSize, true, true, true)) using (PtlColorDeviceRGB colorBlack = new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f)) using (PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) { using (PtlPages pages = doc.getPages()) using (PtlPage page = pages.get(0)) //1ページ目 using (PtlSize pageSize = page.getSize()) using (PtlContent content = page.getContent()) using (PtlRect rect = new PtlRect(20, 20, pageSize.getWidth() - 20, pageSize.getHeight() - 20)) using (PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, 400, 400)) { // TextBoxの縁取りを付ける textBox.setOutlineColor(colorRed); // 縁取りがテキストを囲むサイズに変わるよう指定 textBox.fitToBBox(true); using (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox()) { paramWriteString.setFont(fontNormal); paramWriteString.setTextColor(colorBlack); string text1 = "長いテキストは折り返されます。"; textBox.writeString(text1, paramWriteString); // 文字列を出力 string text2 = "「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)"; textBox.writeStringNL(text2, paramWriteString); // 文字列を出力して改行 textBox.writeNL(); // 改行 string text3 = ("英字は途中で折り返されません。TextBoxはPtlParamWriteStringTextBoxを使います。"); textBox.writeStringNL(text3, paramWriteString); // 文字列を出力して改行 } textBox.terminate(); doc.save(output); Console.WriteLine("-- 完了 --"); } } } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }