/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:リンク注釈の作成 Copyright 2013-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace AppendAnnotLink { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("usage: AppendAnnotLink.exe in-pdf-file out-pdf-file アクションの種類\n"); Console.WriteLine("アクションの種類\n0 : GOTOアクションの設定\n1 : GOTORアクションの設定\n2 : Launchアクションの設定\n3 : URIアクションの設定\n"); return; } String actionKind = args[2]; switch (actionKind) { case "0": case "1": case "2": case "3": break; default: Console.WriteLine("usage: AppendAnnotLink.exe in-pdf-file out-pdf-file アクションの種類\n"); Console.WriteLine("アクションの種類\n0 : GOTOアクションの設定\n1 : GOTORアクションの設定\n2 : Launchアクションの設定\n3 : URIアクションの設定\n"); 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; } // 1ページ目の取得 using (PtlPage page = pages.get(0)) { // 注釈の追加 addAnnotLink(pages, page, actionKind); } } // ファイルに保存します。 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 addAnnotLink(PtlPages pages, PtlPage page, String actionKind) { // 注釈コンテナの取得 using (PtlAnnots annots = page.getAnnots()) { // PDFのリンク注釈 using (PtlAnnotLink annotlink = new PtlAnnotLink()) { // 矩形座標を設定 using (PtlRect rectAnnot = new PtlRect(30.0f, 30.0f, 50.0f, 50.0f)) { annotlink.setRect(rectAnnot); } // 内容を設定 annotlink.setTextContents("PDFのリンク注釈"); // アクションの設定 setAction(pages, annotlink, actionKind); // 注釈の追加 annots.append(annotlink); } } } static void setAction(PtlPages pages, PtlAnnotLink annotlink, String actionKind) { switch (actionKind) { case "0": { // GOTOアクションの設定 using (PtlActionGoTo acttiongoto = new PtlActionGoTo()) using (PtlDestFit destfit = new PtlDestFit()) { // 宛先ページの設定(最終ページに) destfit.setPageNumber(pages.getCount() - 1); acttiongoto.setDest(destfit); // アクションの設定 annotlink.setAction(acttiongoto); } break; } case "1": { // GOTORアクションの設定 using (PtlActionGoToR actiongotor = new PtlActionGoToR()) using (PtlDestFit destfit = new PtlDestFit()) { // 宛先ページの設定(最終ページに) destfit.setPageNumber(pages.getCount() - 1); actiongotor.setDest(destfit); // ファイル間移動用PDFファイルを設定 actiongotor.setFileName("test.pdf"); // 新ウィンドウフラグを設定 actiongotor.setNewWindowFlag(true); // アクションの設定 annotlink.setAction(actiongotor); } break; } case "2": { // Launchアクションの設定 using (PtlActionLaunch actionlaunch = new PtlActionLaunch()) { // 起動ファイル名を設定 actionlaunch.setFileName("test.txt"); // 新ウィンドウフラグを設定 actionlaunch.setNewWindowFlag(true); // アクションの設定 annotlink.setAction(actionlaunch); } break; } case "3": { // URIアクションの設定 using (PtlActionURI actionurl = new PtlActionURI()) { // URIを設定 actionurl.setURI("http://www.antenna.co.jp/"); // アクションの設定 annotlink.setAction(actionurl); } break; } } } } }