PDF Tool APIサンプル集: 各ページの注釈の日付とコメントを取得し一覧にしCSV形式で出力する
PDFの各ページの注釈の日付とコメントを取得し一覧にしてCSVの形式で出力するコンソールアプリケーションです。
概要
サンプルコードの概要
1)注釈を取得したいPDFを開きます
2)ページごとに注釈コンテナを取得します
3)注釈の詳細を取得します
4)取得した詳細から文字列を取得します
5)注釈コンテナにある注釈の数だけ3)4)を繰り返します
6)ページの数だけ2)から5)を繰り返します
7)取得した文字列をCSVとして出力して終了します
コマンドラインでの実行例
sample.exe c:\in\test14.pdf c:\sav\output14.pdf
ダウンロード
サンプルコード
/*
antenna house pdf tool api v7.0
c# interface sample program
概要:各ページの注釈の日付とコメントを取得し一覧にしCSV形式で出力する
copyright 2024 antenna house,inc.
・ソースコードは、複製・改変・再頒布できます。
・ソースコードについて、pdf tool apiに関してのお問い合わせは受付ます。
その他の部分につきましては、お客様にて動作確認等行ってください。
弊社にて動作を保証するものではありません。
*/
using PdfTkNet;
using System;
using System.IO;
using System.Text;
namespace sampleapi14_cs
{
internal class sampleapi14_cs
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("usage: sampleapi14_cs.exe in-pdf-file out-csv-file");
return;
}
String outpathAttach = "";
try
{
using (PtlParamInput inputFile = new PtlParamInput(args[0]))
using (StreamWriter sw = new StreamWriter(args[1], false, Encoding.UTF8))
using (PtlPDFDocument doc = new PtlPDFDocument())
{
//PDFファイルをロードします。
doc.load(inputFile);
// タイトル行
sw.WriteLine("ページ,ANNOT_TYPE,Date,TextContents,MarkUpTitle");
//ページコンテナの取得
using (PtlPages pages = doc.getPages())
{
//ページコンテナが空かどうか
if (pages.isEmpty())
{
Console.WriteLine("ページコンテナが空");
return;
}
int numPages = pages.getCount();
for (int i = 0; i < numPages; i++)
{
Console.WriteLine("ページ : " + (i + 1));
using (PtlPage page = pages.get(i)) //ページの取得
using (PtlAnnots annots = page.getAnnots()) //注釈コンテナの取得
{
//注釈コンテナが空かどうか
if (annots.isEmpty())
{
Console.WriteLine("注釈コンテナが空");
}
else
{
//注釈数の取得
int numAnnots = annots.getCount();
Console.WriteLine("注釈数 : " + numAnnots);
for (int j = 0; j < numAnnots; j++)
{
// 注釈タイプ取得
using (PtlAnnot annot = annots.get(j))
{
StringBuilder sb = new StringBuilder();
sb.Append(i + 1);
if (showAnnot(annot, outpathAttach, sb))
{
sw.WriteLine(sb.ToString());
}
sb.Clear();
}
}
}
}
}
}
sw.Close();
}
}
catch (PtlException pex)
{
Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
pex.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("-- 完了 --");
}
}
static bool showAnnot(PtlAnnot annot, String outpathAttach, StringBuilder sb)
{
bool ret = false;
PtlAnnot.ANNOT_TYPE annotType = annot.getType();
sb.Append(",\"" + annotType + "\"");
switch (annotType)
{
case PtlAnnot.ANNOT_TYPE.TYPE_TEXT:
PtlAnnotText annotText = (PtlAnnotText)annot;
showAnnotCommon(annotText, sb);
showAnnotMarkup(annotText, sb);
ret = true;
break;
case PtlAnnot.ANNOT_TYPE.TYPE_LINK:
// 今回は対象外
//PtlAnnotLink annotLink = (PtlAnnotLink)annot;
//showAnnotCommon(annotLink, sb);
//ret = true;
break;
case PtlAnnot.ANNOT_TYPE.TYPE_STAMP:
PtlAnnotStamp annotStamp = (PtlAnnotStamp)annot;
showAnnotCommon(annotStamp, sb);
showAnnotMarkup(annotStamp, sb);
ret = true;
break;
case PtlAnnot.ANNOT_TYPE.TYPE_FILE_ATTACHMENT:
PtlAnnotFileAttachment annotFileAttachment = (PtlAnnotFileAttachment)annot;
showAnnotCommon(annotFileAttachment, sb);
showAnnotMarkup(annotFileAttachment, sb);
ret = true;
break;
default:
showAnnotCommon(annot, sb);
if (annot.isMarkup())
{
showAnnotMarkup(annot, sb);
}
ret = true;
break;
}
return ret;
}
static void showAnnotCommon(PtlAnnot annot, StringBuilder sb)
{
using (PtlDate date = annot.getDate())
{
int year = date.getYear();
int month = date.getMonth();
int day = date.getDay();
int hour = date.getHour();
int min = date.getMin();
int sec = date.getSec();
sb.Append("," + year + "/" + month + "/" + day + " " + hour + ":" + min + ":" + sec);
sb.Append(",\"" + annot.getTextContents() + "\"");
}
}
static void showAnnotMarkup(PtlAnnot annot, StringBuilder sb)
{
PtlAnnotMarkup annotMarkup = (PtlAnnotMarkup)annot;
sb.Append(",\"" + annotMarkup.getMarkUpTitle() + "\"");
}
}
}

