/* Antenna House PDF Tool API V8.0 .Net Interface sample program 概要:各種注釈の作成 Copyright 2025 Antenna House, Inc. */ using PdfTkNet; namespace AppendAnnotShapes { class AppendAnnotShapes { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("usage: AppendAnnotShapes.exe in-pdf-file out-pdf-file 規格"); Console.WriteLine("規格"); Console.WriteLine("1 : FreeText注釈(引き出し線あり) 2 : FreeText(引き出し線なし)注釈 3 : 円注釈"); Console.WriteLine("4 : 矩形注釈 5 : ライン注釈 6 : 折れ線注釈 7 : 多角形注釈"); return; } try { using (PtlParamInput input = new PtlParamInput(args[0])) using (PtlParamOutput output = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) { // PDFファイルをロードします。 doc.load(input); // ページコンテナの取得 using (PtlPages pages = doc.getPages()) { // ページコンテナが空かどうか if (pages.isEmpty()) { Console.WriteLine("ページコンテナが空\n"); return; } // 1ページ目の取得 using (PtlPage page = pages.get(0)) { int kind = int.Parse(args[2]); switch (kind) { case 1: // FreeText注釈(引き出し線あり)の追加 // 注釈の追加 addAnnotFreeText(page, true); break; case 2: // FreeText注釈(引き出し線なし)の追加 addAnnotFreeText(page, false); break; case 3: // 円注釈の追加 addAnnotCircle(page); break; case 4: // 矩形注釈の追加 addAnnotSquare(page); break; case 5: // ライン注釈の追加 addAnnotLine(page); break; case 6: // 折れ線注釈の追加 addAnnotPolyLine(page); break; case 7: // 多角形注釈の追加 addAnnotPolygone(page); break; default: Console.WriteLine("usage: AppendAnnotShapes.exe in-pdf-file out-pdf-file 規格"); Console.WriteLine("規格"); Console.WriteLine("1 : FreeText注釈(引き出し線あり) 2 : FreeText(引き出し線なし)注釈 3 : 円注釈"); Console.WriteLine("4 : 矩形注釈 5 : ライン注釈 6 : 折れ線注釈 7 : 多角形注釈"); return; } } //doc.setSaveOption(PtlPDFDocument.SAVE_OPTION.SAVE_INCREMENTAL_UPDATE); // ファイルに保存します。 doc.save(output); } } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("-- 完了 --"); } } static void addAnnotFreeText(PtlPage page, bool on) { // 注釈コンテナの取得 using (PtlAnnots annots = page.getAnnots()) // フリーテキスト注釈 using (PtlAnnotFreeText annotFreeText = new PtlAnnotFreeText()) using (PtlRect rect = new PtlRect(50, 100, 95, 150)) using (PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) using (PtlColorDeviceRGB colorBlue = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f)) { // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下 annotFreeText.setTextBoxRect(rect); // 色を設定 annotFreeText.setColor(colorRed); // 内部色を設定 annotFreeText.setInteriorColor(colorBlue); // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotFreeText.setTextContents("フリーテキスト注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotFreeText.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // ポップアップウィンドウのタイトル文字列設定 annotFreeText.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotFreeText.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotFreeText.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotFreeText.setMarkUpCA(0.8f); using (PtlTextBoxAnnot textBox = new PtlTextBoxAnnot()) using (PtlParamTextBoxAnnot paramTextBox1 = new PtlParamTextBoxAnnot()) using (PtlParamFontAnnot font = new PtlParamFontAnnot("MS ゴシック", 12, false, false)) using (PtlParamTextBoxAnnot paramTextBox2 = new PtlParamTextBoxAnnot()) using (PtlParamFontAnnot font2 = new PtlParamFontAnnot("MS 明朝", 20, false, false)) using (PtlParamTextBoxAnnot paramTextBox3 = new PtlParamTextBoxAnnot()) using (PtlParamFontAnnot font3 = new PtlParamFontAnnot("MS P明朝", 20, true, true)) using (PtlParamTextBoxAnnot paramTextBox4 = new PtlParamTextBoxAnnot()) using (PtlParamFontAnnot font4 = new PtlParamFontAnnot("MS 明朝", 10, false, true)) { paramTextBox1.setFont(font); paramTextBox1.setTextColor(colorRed); paramTextBox2.setFont(font2); paramTextBox2.setTextColor(new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f)); paramTextBox2.setUnderline(true); paramTextBox3.setFont(font3); paramTextBox3.setTextColor(new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f)); paramTextBox3.setStrikeOut(true); paramTextBox4.setFont(font4); paramTextBox4.setTextColor(new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f)); textBox.writeStringNL("あいうえお", paramTextBox1); textBox.writeStringNL("かきくけこ", paramTextBox2); textBox.writeString("さしすせそ", paramTextBox1); textBox.writeString("たちつてと", paramTextBox3); textBox.writeStringNL("なにぬねの", paramTextBox4); annotFreeText.setTextBox(textBox); if (on) { // 引き出し線の設定 using (PtlPoints points = annotFreeText.getCalloutPoints()) { // 右から右下へ points.append(150, 70); points.append(130, 125); points.append(95, 125); } // 引き出し線の線端 annotFreeText.setLineEndingStyle(PtlAnnotFreeText.LINE_ENDING_STYLE.STYLE_OPEN_ARROW); } // 注釈の追加 annots.append(annotFreeText); } } } static void addAnnotCircle(PtlPage page) { using (PtlAnnots annots = page.getAnnots()) using (PtlAnnotCircle annotCircle = new PtlAnnotCircle()) using (PtlRect rect = new PtlRect(50.0f, 100.0f, 100.0f, 150.0f)) { // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下 annotCircle.setRect(rect); // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotCircle.setTextContents("円形注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotCircle.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 注釈フラグを設定(論理和) FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */ annotCircle.setAnnotFlags(PtlAnnotText.ANNOT_FLAGS.FLAG_NOROTATE); using (PtlColorDeviceRGB colorLine = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) using (PtlColorDeviceRGB colorInterior = new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f)) { annotCircle.setColor(colorLine); annotCircle.setInteriorColor(colorInterior); } // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */ annotCircle.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID); // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */ annotCircle.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN); //------ // ポップアップウィンドウのタイトル文字列設定 annotCircle.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotCircle.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotCircle.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotCircle.setMarkUpCA(0.8f); //------ annots.append(annotCircle); } } static void addAnnotSquare(PtlPage page) { using (PtlAnnots annots = page.getAnnots()) using (PtlAnnotSquare annotSquare = new PtlAnnotSquare()) using (PtlRect rect = new PtlRect(50.0f, 100.0f, 95.0f, 150.0f)) using (PtlColorDeviceRGB colorLine = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) using (PtlColorDeviceRGB colorInterior = new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f)) { // 矩形座標を設定 座標の単位はmmで原点(0,0)は左下 annotSquare.setRect(rect); // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotSquare.setTextContents("矩形注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotSquare.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 注釈フラグを設定(論理和) FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */ annotSquare.setAnnotFlags(PtlAnnotText.ANNOT_FLAGS.FLAG_NOROTATE); // 色を設定 annotSquare.setColor(colorLine); // 内部色を設定 annotSquare.setInteriorColor(colorInterior); // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */ annotSquare.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID); // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */ annotSquare.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN); //------ // ポップアップウィンドウのタイトル文字列設定 annotSquare.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotSquare.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotSquare.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotSquare.setMarkUpCA(0.8f); //------ // 注釈の追加 annots.append(annotSquare); } } static void addAnnotLine(PtlPage page) { using (PtlAnnots annots = page.getAnnots()) using (PtlAnnotLine annotLine = new PtlAnnotLine()) { // 開始座標を設定 座標の単位はmmで原点(0,0)は左下 annotLine.setStartPoint(new PtlPoint(0.0f, 100.0f)); // 終了座標を設定 座標の単位はmmで原点(0,0)は左下 annotLine.setEndPoint(new PtlPoint(100.0f, 200.0f)); // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotLine.setTextContents("ライン注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotLine.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 注釈フラグを設定(論理和) FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */ annotLine.setAnnotFlags(PtlAnnotText.ANNOT_FLAGS.FLAG_NOROTATE); // 色を設定 annotLine.setColor(new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)); // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */ annotLine.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID); // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */ annotLine.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN); // キャプション設定 annotLine.setCaptionPosition(PtlAnnotLine.CAPTION_POSITION.POSITION_TOP); annotLine.setViewCaption(true); annotLine.setHorizontalOffset(30.0f); annotLine.setVerticalOffset(10.0f); // 線端スタイル設定 annotLine.setLineStartPointStyle(PtlAnnotLine.LINE_ENDING_STYLE.STYLE_R_OPEN_ARROW); annotLine.setLineEndPointStyle(PtlAnnotLine.LINE_ENDING_STYLE.STYLE_DIAMOND); // 引き出し線 annotLine.setLeaderLinesLength(-20.0f); annotLine.setExtensionLeaderLines(50.0f); // 引き出し線オフセット annotLine.setLeaderLinesOffset(20.0f); //------ // ポップアップウィンドウのタイトル文字列設定 annotLine.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotLine.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotLine.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotLine.setMarkUpCA(0.8f); //------ // 注釈の追加 annots.append(annotLine); } } static void addAnnotPolyLine(PtlPage page) { using (PtlAnnots annots = page.getAnnots()) //注釈コンテナの取得 using (PtlAnnotPolyLine annotPolyline = new PtlAnnotPolyLine()) { // 座標を設定 座標の単位はmmで原点(0,0)は左下 using (PtlPoints points = annotPolyline.getVertices()) { points.append(new PtlPoint(92, 123)); points.append(new PtlPoint(96, 117)); points.append(new PtlPoint(103, 121)); points.append(new PtlPoint(115, 119)); points.append(new PtlPoint(121, 112)); points.append(new PtlPoint(130, 113)); points.append(new PtlPoint(121, 122)); points.append(new PtlPoint(90, 126)); points.append(new PtlPoint(86, 124)); points.append(new PtlPoint(89, 116)); } // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotPolyline.setTextContents("折れ線注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotPolyline.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 注釈フラグを設定(論理和) FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */ annotPolyline.setAnnotFlags(PtlAnnotText.ANNOT_FLAGS.FLAG_NOROTATE); // 色を設定 annotPolyline.setColor(new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)); // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */ annotPolyline.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID); // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */ annotPolyline.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN); //------ // ポップアップウィンドウのタイトル文字列設定 annotPolyline.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotPolyline.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotPolyline.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotPolyline.setMarkUpCA(0.8f); //------ //注釈の追加 annots.append(annotPolyline); } } static void addAnnotPolygone(PtlPage page) { using (PtlAnnots annots = page.getAnnots()) //注釈コンテナの取得 using (PtlAnnotPolygon annotPolygone = new PtlAnnotPolygon()) { using (PtlPoints points = annotPolygone.getVertices()) { points.append(new PtlPoint(92, 123)); points.append(new PtlPoint(96, 117)); points.append(new PtlPoint(103, 121)); points.append(new PtlPoint(115, 119)); points.append(new PtlPoint(121, 112)); points.append(new PtlPoint(130, 113)); points.append(new PtlPoint(121, 122)); points.append(new PtlPoint(90, 126)); points.append(new PtlPoint(86, 124)); points.append(new PtlPoint(89, 116)); } // 内容を設定(注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明) annotPolygone.setTextContents("多角形注釈サンプル"); // 日時の設定(2025/01/01 00:00:00) annotPolygone.setDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 注釈フラグを設定(論理和) FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */ annotPolygone.setAnnotFlags(PtlAnnotText.ANNOT_FLAGS.FLAG_NOROTATE); // 色を設定 annotPolygone.setColor(new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)); // 内部色を設定 annotPolygone.setInteriorColor(new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f)); // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */ annotPolygone.setBorderStyle(PtlAnnotText.BORDER_STYLE.BORDER_SOLID); // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */ annotPolygone.setBorderWidth(PtlAnnotText.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN); //------ // ポップアップウィンドウのタイトル文字列設定 annotPolygone.setMarkUpTitle("Antenna House"); // サブジェクトの短い説明設定 annotPolygone.setMarkUpSubj("サブジェクトの短い説明"); // 注釈生成日時の設定(2025/01/01 00:00:00) annotPolygone.setMarkUpDate(new PtlDate(2025, 1, 1, 0, 0, 0)); // 不透明度を設定 0.0 ~ 1.0。0.0が透明、1.0が不透明 annotPolygone.setMarkUpCA(0.8f); //------ //注釈の追加 annots.append(annotPolygone); } } } }