/*
Antenna House PDF Tool API V7.0
C++ Interface sample program
概要:スタンプ注釈の作成
Copyright 2013-2021 Antenna House, Inc.
*/
#include < PdfTk.h >
#include < math.h >
#include < stdio.h >
using namespace PdfTk;
void addPreDefinedStampAnnot(PtlAnnots& annots);
void addPreDefinedStampAnnot2(PtlAnnots& annots);
void addCustomStampAnnotFromPdf(PtlAnnots& annots, const char* pathPdf);
void addCustomStampAnnotFromImage(PtlAnnots& annots, const char* pathImage);
void addCustomStampAnnotFromDrawContent(PtlAnnots& annots);
void drawDateStamp(PtlContent& content, float sizeStamp, const char* szDate, const char* szName);
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("usage: AppendAnnotStamp.exe in-pdf-file out-pdf-file スタンプ種類 [in-xxx-file]\n\n");
printf("スタンプ種類\n0 : 標準 1 : 標準2 2 : カスタム(PDF) 3 : カスタム(Image) 4 : カスタム(日付印)\n");
return 1;
}
try
{
PtlParamInput input(argv[1]);
PtlParamOutput output(argv[2]);
const char* stampKind = argv[3];
switch (stampKind[0]) {
case '0':
case '1':
case '2':
case '3':
case '4':
break;
default:
printf("usage: AppendAnnotStamp.exe in-pdf-file out-pdf-file スタンプ種類 [in-xxx-file]\n\n");
printf("スタンプ種類\n0 : 標準 1 : 標準2 2 : カスタム(PDF) 3 : カスタム(Image) 4 : カスタム(日付印)\n");
return 1;
}
PtlPDFDocument doc;
// PDFファイルをロードします。
doc.load(input);
// ページコンテナの取得
PtlPages& pages = doc.getPages();
// ページコンテナが空かどうか
if (pages.isEmpty()){
printf("ページコンテナが空\n");
return 1;
}
// 1ページ目の取得
PtlPage page = pages.get(0);
// 注釈コンテナの取得
PtlAnnots& annots = page.getAnnots();
switch (stampKind[0]) {
case '0':
// 標準
addPreDefinedStampAnnot(annots);
break;
case '1':
// 標準2
addPreDefinedStampAnnot2(annots);
break;
case '2':
// 読み込んだPDFを外観とするスタンプ
if (argc < 5) {
printf("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-pdf-file");
return 1;
}
addCustomStampAnnotFromPdf(annots, argv[4]);
break;
case '3':
// 読み込んだ画像を外観とするスタンプ
if (argc < 5) {
printf("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-image-file");
return 1;
}
addCustomStampAnnotFromImage(annots, argv[4]);
break;
case '4':
// 日付印を外観として描画
addCustomStampAnnotFromDrawContent(annots);
break;
}
// ファイルに保存します。
doc.save(output);
printf("完了!\n");
}
catch (const PtlException &e)
{
fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
return 1;
}
return 0;
}
void addPreDefinedStampAnnot(PtlAnnots& annots)
{
PtlAnnotStamp stampAnnot1;
stampAnnot1.setIconType(PtlAnnotStamp::ICON_APPROVED);
PtlRect rectSize1 = stampAnnot1.getRect();
stampAnnot1.setRect(PtlRect(10.0f, 270.0f, 10.0f+rectSize1.getRight(), 270.0f+rectSize1.getTop()));
stampAnnot1.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot1);
PtlAnnotStamp stampAnnot2;
stampAnnot2.setIconType(PtlAnnotStamp::ICON_AS_IS);
PtlRect rectSize2 = stampAnnot2.getRect();
stampAnnot2.setRect(PtlRect(10.0f, 250.0f, 10.0f+rectSize2.getRight(), 250.0f+rectSize2.getTop()));
stampAnnot2.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot2);
PtlAnnotStamp stampAnnot3;
stampAnnot3.setIconType(PtlAnnotStamp::ICON_CONFIDENTIAL);
PtlRect rectSize3 = stampAnnot3.getRect();
stampAnnot3.setRect(PtlRect(10.0f, 230.0f, 10.0f+rectSize3.getRight(), 230.0f+rectSize3.getTop()));
stampAnnot3.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot3);
PtlAnnotStamp stampAnnot4;
stampAnnot4.setIconType(PtlAnnotStamp::ICON_DEPARTMENTAL);
PtlRect rectSize4 = stampAnnot4.getRect();
stampAnnot4.setRect(PtlRect(10.0f, 210.0f, 10.0f+rectSize4.getRight(), 210.0f+rectSize4.getTop()));
stampAnnot4.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot4);
PtlAnnotStamp stampAnnot5;
stampAnnot5.setIconType(PtlAnnotStamp::ICON_DRAFT);
PtlRect rectSize5 = stampAnnot5.getRect();
stampAnnot5.setRect(PtlRect(10.0f, 190.0f, 10.0f+rectSize5.getRight(), 190.0f+rectSize5.getTop()));
stampAnnot5.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot5);
PtlAnnotStamp stampAnnot6;
stampAnnot6.setIconType(PtlAnnotStamp::ICON_EXPERIMENTAL);
PtlRect rectSize6 = stampAnnot6.getRect();
stampAnnot6.setRect(PtlRect(10.0f, 170.0f, 10.0f+rectSize6.getRight(), 170.0f+rectSize6.getTop()));
stampAnnot6.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot6);
PtlAnnotStamp stampAnnot7;
stampAnnot7.setIconType(PtlAnnotStamp::ICON_EXPIRED);
PtlRect rectSize7 = stampAnnot7.getRect();
stampAnnot7.setRect(PtlRect(10.0f, 150.0f, 10.0f+rectSize7.getRight(), 150.0f+rectSize7.getTop()));
stampAnnot7.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot7);
PtlAnnotStamp stampAnnot8;
stampAnnot8.setIconType(PtlAnnotStamp::ICON_FINAL);
PtlRect rectSize8 = stampAnnot8.getRect();
stampAnnot8.setRect(PtlRect(10.0f, 130.0f, 10.0f+rectSize8.getRight(), 130.0f+rectSize8.getTop()));
stampAnnot8.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot8);
PtlAnnotStamp stampAnnot9;
stampAnnot9.setIconType(PtlAnnotStamp::ICON_FOR_COMMENT);
PtlRect rectSize9 = stampAnnot9.getRect();
stampAnnot9.setRect(PtlRect(10.0f, 110.0f, 10.0f+rectSize9.getRight(), 110.0f+rectSize9.getTop()));
stampAnnot9.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot9);
PtlAnnotStamp stampAnnot10;
stampAnnot10.setIconType(PtlAnnotStamp::ICON_FOR_PUBLIC_RELEASE);
PtlRect rectSize10 = stampAnnot10.getRect();
stampAnnot10.setRect(PtlRect(10.0f, 90.0f, 10.0f+rectSize10.getRight(), 90.0f+rectSize10.getTop()));
stampAnnot10.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot10);
PtlAnnotStamp stampAnnot11;
stampAnnot11.setIconType(PtlAnnotStamp::ICON_NOT_APPROVED);
PtlRect rectSize11 = stampAnnot11.getRect();
stampAnnot11.setRect(PtlRect(10.0f, 70.0f, 10.0f+rectSize11.getRight(), 70.0f+rectSize11.getTop()));
stampAnnot11.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot11);
PtlAnnotStamp stampAnnot12;
stampAnnot12.setIconType(PtlAnnotStamp::ICON_NOT_FOR_PUBLIC_RELEASE);
PtlRect rectSize12 = stampAnnot12.getRect();
stampAnnot12.setRect(PtlRect(10.0f, 50.0f, 10.0f+rectSize12.getRight(), 50.0f+rectSize12.getTop()));
stampAnnot12.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot12);
PtlAnnotStamp stampAnnot13;
stampAnnot13.setIconType(PtlAnnotStamp::ICON_SOLD);
PtlRect rectSize13 = stampAnnot13.getRect();
stampAnnot13.setRect(PtlRect(10.0f, 30.0f, 10.0f+rectSize13.getRight(), 30.0f+rectSize13.getTop()));
stampAnnot13.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot13);
PtlAnnotStamp stampAnnot14;
stampAnnot14.setIconType(PtlAnnotStamp::ICON_TOP_SECRET);
PtlRect rectSize14 = stampAnnot14.getRect();
stampAnnot14.setRect(PtlRect(10.0f, 10.0f, 10.0f+rectSize14.getRight(), 10.0f+rectSize14.getTop()));
stampAnnot14.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot14);
}
void addPreDefinedStampAnnot2(PtlAnnots& annots)
{
PtlAnnotStamp stampAnnot1;
stampAnnot1.setIconType(PtlAnnotStamp::ICON_SB_APPROVED);
PtlRect rectSize1 = stampAnnot1.getRect();
stampAnnot1.setRect(PtlRect(10.0f, 270.0f, 10.0f+rectSize1.getRight(), 270.0f+rectSize1.getTop()));
stampAnnot1.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot1);
PtlAnnotStamp stampAnnot2;
stampAnnot2.setIconType(PtlAnnotStamp::ICON_SB_COMPLETED);
PtlRect rectSize2 = stampAnnot2.getRect();
stampAnnot2.setRect(PtlRect(10.0f, 250.0f, 10.0f+rectSize2.getRight(), 250.0f+rectSize2.getTop()));
stampAnnot2.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot2);
PtlAnnotStamp stampAnnot3;
stampAnnot3.setIconType(PtlAnnotStamp::ICON_SB_CONFIDENTIAL);
PtlRect rectSize3 = stampAnnot3.getRect();
stampAnnot3.setRect(PtlRect(10.0f, 230.0f, 10.0f+rectSize3.getRight(), 230.0f+rectSize3.getTop()));
stampAnnot3.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot3);
PtlAnnotStamp stampAnnot4;
stampAnnot4.setIconType(PtlAnnotStamp::ICON_SB_DRAFT);
PtlRect rectSize4 = stampAnnot4.getRect();
stampAnnot4.setRect(PtlRect(10.0f, 210.0f, 10.0f+rectSize4.getRight(), 210.0f+rectSize4.getTop()));
stampAnnot4.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot4);
PtlAnnotStamp stampAnnot5;
stampAnnot5.setIconType(PtlAnnotStamp::ICON_SB_FINAL);
PtlRect rectSize5 = stampAnnot5.getRect();
stampAnnot5.setRect(PtlRect(10.0f, 190.0f, 10.0f+rectSize5.getRight(), 190.0f+rectSize5.getTop()));
stampAnnot5.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot5);
PtlAnnotStamp stampAnnot6;
stampAnnot6.setIconType(PtlAnnotStamp::ICON_SB_FOR_COMMENT);
PtlRect rectSize6 = stampAnnot6.getRect();
stampAnnot6.setRect(PtlRect(10.0f, 170.0f, 10.0f+rectSize6.getRight(), 170.0f+rectSize6.getTop()));
stampAnnot6.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot6);
PtlAnnotStamp stampAnnot7;
stampAnnot7.setIconType(PtlAnnotStamp::ICON_SB_FOR_PUBLIC_RELEASE);
PtlRect rectSize7 = stampAnnot7.getRect();
stampAnnot7.setRect(PtlRect(10.0f, 150.0f, 10.0f+rectSize7.getRight(), 150.0f+rectSize7.getTop()));
stampAnnot7.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot7);
PtlAnnotStamp stampAnnot8;
stampAnnot8.setIconType(PtlAnnotStamp::ICON_SB_INFORMATIONONLY);
PtlRect rectSize8 = stampAnnot8.getRect();
stampAnnot8.setRect(PtlRect(10.0f, 130.0f, 10.0f+rectSize8.getRight(), 130.0f+rectSize8.getTop()));
stampAnnot8.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot8);
PtlAnnotStamp stampAnnot9;
stampAnnot9.setIconType(PtlAnnotStamp::ICON_SB_NOT_APPROVED);
PtlRect rectSize9 = stampAnnot9.getRect();
stampAnnot9.setRect(PtlRect(10.0f, 110.0f, 10.0f+rectSize9.getRight(), 110.0f+rectSize9.getTop()));
stampAnnot9.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot9);
PtlAnnotStamp stampAnnot10;
stampAnnot10.setIconType(PtlAnnotStamp::ICON_SB_NOT_FOR_PUBLIC_RELEASE);
PtlRect rectSize10 = stampAnnot10.getRect();
stampAnnot10.setRect(PtlRect(10.0f, 90.0f, 10.0f+rectSize10.getRight(), 90.0f+rectSize10.getTop()));
stampAnnot10.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot10);
PtlAnnotStamp stampAnnot11;
stampAnnot11.setIconType(PtlAnnotStamp::ICON_SB_PRELIMINARYRESULTS);
PtlRect rectSize11 = stampAnnot11.getRect();
stampAnnot11.setRect(PtlRect(10.0f, 70.0f, 10.0f+rectSize11.getRight(), 70.0f+rectSize11.getTop()));
stampAnnot11.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot11);
PtlAnnotStamp stampAnnot12;
stampAnnot12.setIconType(PtlAnnotStamp::ICON_SB_VOID);
PtlRect rectSize12 = stampAnnot12.getRect();
stampAnnot12.setRect(PtlRect(10.0f, 50.0f, 10.0f+rectSize12.getRight(), 50.0f+rectSize12.getTop()));
stampAnnot12.setAnnotFlags(PtlAnnot::FLAG_PRINT);
annots.append(stampAnnot12);
}
void addCustomStampAnnotFromPdf(PtlAnnots& annots, const char* pathPdf)
{
PtlAnnotStamp stampAnnot;
PtlPDFDocument doc_custom;
// PDFファイル
PtlParamInput inputCustom(pathPdf);
// スタンプにするPDFファイルをロードします。
doc_custom.load(inputCustom);
PtlPages& pagesCustomStamp = doc_custom.getPages();
// ページコンテナが空かどうか
if (pagesCustomStamp.isEmpty())
{
printf("ページコンテナが空\n");
return;
}
// 追加された画像ページを取得する
PtlPage pageCustomStamp = pagesCustomStamp.get(0);
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
PtlSize size = pageCustomStamp.getSize();
stampAnnot.setRect(PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()));
stampAnnot.setIconType(PtlAnnotStamp::ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot::FLAG_PRINT);
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
void addCustomStampAnnotFromImage(PtlAnnots& annots, const char* pathImage)
{
PtlAnnotStamp stampAnnot;
// 画像描画パラメータ
PtlParamDrawImage paramDrawImage;
// 画像ファイル
PtlParamInput inputCustom(pathImage);
// 画像描画パラメータに画像ファイルを設定
paramDrawImage.setImageStream(inputCustom);
// 画像ページパラメータ
PtlParamImagePage paramImagePage;
// 画像ページパラメータに画像描画パラメータを設定
paramImagePage.setImage(paramDrawImage);
// 画像ページのサイズを画像サイズにあわせる
paramImagePage.setPaperType(PtlParamImagePage::PAPER_IMAGE_SIZE);
// 画像ページパラメータから作成したページ
PtlPage pageCustomStamp(paramImagePage);
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
PtlSize size = pageCustomStamp.getSize();
stampAnnot.setRect(PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()));
stampAnnot.setIconType(PtlAnnotStamp::ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot::FLAG_PRINT);
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
void addCustomStampAnnotFromDrawContent(PtlAnnots& annots)
{
// スタンプサイズ
float sizeStamp = 30.0f;
PtlAnnotStamp stampAnnot;
stampAnnot.setRect(PtlRect(10.0f, 100.0f, 10.0f + sizeStamp, 100.0f + sizeStamp));
stampAnnot.setIconType(PtlAnnotStamp::ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot::FLAG_PRINT);
// 日付印を描画する仮のページ
PtlPage pageCustomStamp;
pageCustomStamp.setMediaBox(PtlRect(0, 0, sizeStamp, sizeStamp));
// 日付印を描画するコンテント
PtlContent& content = pageCustomStamp.getContent();
// コンテントに日付印を描画
//drawDateStamp(content, sizeStamp, "2014/09/8", "鈴木");
drawDateStamp(content, sizeStamp, "2014/09/8", "Suzuki");
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
void drawDateStamp(PtlContent& content, float sizeStamp, const char* szDate, const char* szName)
{
// スタンプに外接する矩形
PtlRect rectStamp(0, 0, sizeStamp, sizeStamp);
// スタンプの色
PtlColorDeviceRGB color = PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
// スタンプの円/線を描画するパラメーター
PtlParamDrawShape paramDrawShape;
paramDrawShape.setLineStyle(PtlParamDrawShape::LINE_STYLE_SOLID);
paramDrawShape.setLineWidth(PtlParamDrawShape::LINE_WIDTH_THIN);
paramDrawShape.setLineColor(color);
paramDrawShape.setFillColor(PtlColorNone());
// スタンプの円を描画
content.drawCircle(rectStamp, paramDrawShape);
#define PI 3.141592
// 円の中心から15度で線と円が交わるとする
double rad = 15.0f * PI / 180.0;
// 円の半径
float r = sizeStamp / 2;
// 円の中心から交点へのx方向の長さ
float xx = r * (float)cos(rad);
// 円の中心から交点へのy方向の長さ
float yy = r * (float)sin(rad);
// 上線左端のx座標
float from_x = r - xx;
// 上線左端のy座標
float to_x = r + xx;
// 下線左端のx座標
float upper_y = r + yy;
// 下線左端のy座標
float lower_y = r - yy;
// 円の中に上線を描画
content.drawLine(PtlPoint(from_x,upper_y), PtlPoint(to_x,upper_y), paramDrawShape);
// 円の中に下線を描画
content.drawLine(PtlPoint(from_x,lower_y), PtlPoint(to_x,lower_y), paramDrawShape);
// 日付のフォント
float fontSizeDate = sizeStamp / 2.15f; // yyyy/mm/dd を想定
//PtlParamFont fontDate(PtlParamString("MS ゴシック"), fontSizeDate, PtlParamFont::WEIGHT_NORMAL, false, true);
PtlParamFont fontDate(PtlParamString("Helvetica"), fontSizeDate, PtlParamFont::WEIGHT_NORMAL, false, true);
// 日付
PtlParamString textDate(szDate);
// 日付を描画するパラメーター
PtlParamWriteString paramWriteStringDate;
paramWriteStringDate.setFont(fontDate);
paramWriteStringDate.setTextColor(color);
paramWriteStringDate.setOutlineColor(color);
// 日付を描画する矩形
PtlRect rectDate(from_x, lower_y, to_x, upper_y);
// 日付を描画
content.writeString(rectDate, PtlContent::ALIGN_CENTER, textDate, paramWriteStringDate);
// 名前のフォント
float fontSizeName = sizeStamp / 1.5f;
// 下線から名前を描画する矩形までのマージン
float margin = sizeStamp / 40.0f;
// 名前を書く矩形の高さ
float heightNameBase = upper_y-lower_y;
float heightName = fontSizeName * 25.4f / 72.0f;
while (heightName > heightNameBase) {
fontSizeName -= 0.5f;
heightName = fontSizeName * 25.4f / 72.0f;
}
// 名前のフォント
//PtlParamFont fontName(PtlParamString("MS 明朝"), fontSizeName, PtlParamFont::WEIGHT_NORMAL, false, true);
PtlParamFont fontName(PtlParamString("Helvetica"), fontSizeName, PtlParamFont::WEIGHT_NORMAL, false, true);
// 線の幅
float widthLine = 0.5f;
// 線幅を減じた円の半径
float rr = r - widthLine;
// 名前の高さのラジアン
float d = asin((heightName + yy + margin)/rr);
// 名前をおさめる矩形の幅
float widthName = rr * (float)cos(d) * 2;
// 名前
PtlParamString textName(szName);
float textWidthName = fontName.getStringWidth(textName);
// 名前の長さによるフォントサイズの調整
while (textWidthName > widthName) {
fontSizeName -= 0.5f;
fontName.setSize(fontSizeName);
textWidthName = fontName.getStringWidth(textName);
heightName = fontSizeName * 25.4f / 72.0f;
d = asin((heightName + yy + margin)/rr);
widthName = rr * (float)cos(d) * 2;
}
// 名前を描画するパラメーター
PtlParamWriteString paramWriteStringName;
paramWriteStringName.setFont(fontName);
paramWriteStringName.setTextColor(color);
paramWriteStringName.setOutlineColor(color);
// 名前を描画する矩形
PtlRect rectName(from_x, lower_y-heightName-margin, to_x, lower_y-margin);
// 名前を描画
content.writeString(rectName, PtlContent::ALIGN_CENTER, textName, paramWriteStringName);
}
PDF Tool APIサンプルコード:スタンプ注釈の作成
指定した種類のスタンプ注釈を挿入します。
概要
サンプルコードの概要
指定した種類のスタンプ注釈を挿入します。
3番目の引数でスタンプの種類を指定します。
0 : 標準スタンプ
1 : 標準スタンプ2
2 : カスタムスタンプ(PDF)
3 : カスタムスタンプ(Image)
4 : カスタムスタンプ(日付印)
2 : カスタムスタンプ(PDF) と 3 : カスタムスタンプ(Image) は
4番目の引数で スタンプPDFやスタンプ画像を指定します。
- PtlAnnotStamp: PDFのスタンプ注釈を表現したクラス。
- PtlAnnotStamp.setIconName(java.lang.String name): アイコン名を設定します。
- PtlAnnotStamp.setIconType(PtlAnnotStamp.ICON_TYPE type): アイコンタイプを設定します。
- PtlAnnotStamp.ICON_TYPE: アイコンのタイプを表した列挙型定数。
サンプルコード
/*
Antenna House PDF Tool API V7.0
Java Interface sample program
概要:スタンプ注釈の作成
Copyright 2015-2021 Antenna House, Inc.
*/
package Sample;
import java.io.*;
import jp.co.antenna.ptl.*;
public class AppendAnnotStamp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 (custom-icon-pdf-file)\n");
System.out.println("スタンプ種類\n0 : 標準 1 : 標準2 2 : カスタム(PDF) 3 : カスタム(Image) 4 : カスタム(日付印)");
return;
}
String stampKind = args[2];
switch (stampKind) {
case "0":
case "1":
case "2":
case "3":
case "4":
break;
default:
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロードします。
doc.load(inputFile);
try (PtlPages pages = doc.getPages()) //ページコンテナの取得
{
// ページコンテナが空かどうか
if (pages.isEmpty())
{
System.out.println("ページコンテナが空\n");
return;
}
try (PtlPage page = pages.get(0);
PtlAnnots annots = page.getAnnots())
{
switch (stampKind)
{
case "0":
// 標準
addPreDefinedStampAnnot(annots);
break;
case "1":
// 標準2
addPreDefinedStampAnnot2(annots);
break;
case "2":
// 読み込んだPDFを外観とするスタンプ
if (args.length < 4)
{
System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-pdf-file");
return;
}
addCustomStampAnnotFromPdf(annots, args[3]);
break;
case "3":
// 読み込んだ画像を外観とするスタンプ
if (args.length < 4)
{
System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-image-file");
return;
}
addCustomStampAnnotFromImage(annots, args[3]);
break;
case "4":
// 日付印を外観として描画
addCustomStampAnnotFromDrawContent(annots);
break;
}
}
}
// ファイルに保存します。
doc.save(outputFile);
}
catch (PtlException pex) {
System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n " + pex.getErrorMessage());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (Error ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally {
System.out.println("-- 完了 --");
}
}
public static void addPreDefinedStampAnnot(PtlAnnots annots) throws PtlException, Exception, Error
{
try (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp()) {
stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_APPROVED);
try(PtlRect rectSize = stampAnnot1.getRect();
PtlRect rect = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop())){
stampAnnot1.setRect(rect);
}
stampAnnot1.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot1);
}
try (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp()) {
stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_AS_IS);
try(PtlRect rectSize = stampAnnot2.getRect();
PtlRect rect = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop())){
stampAnnot2.setRect(rect);
}
stampAnnot2.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot2);
}
try (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp()) {
stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CONFIDENTIAL);
try(PtlRect rectSize = stampAnnot3.getRect();
PtlRect rect = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop())){
stampAnnot3.setRect(rect);
}
stampAnnot3.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot3);
}
try (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp()) {
stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DEPARTMENTAL);
try(PtlRect rectSize = stampAnnot4.getRect();
PtlRect rect = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop())){
stampAnnot4.setRect(rect);
}
stampAnnot4.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot4);
}
try (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp()) {
stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DRAFT);
try(PtlRect rectSize = stampAnnot5.getRect();
PtlRect rect = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop())){
stampAnnot5.setRect(rect);
}
stampAnnot5.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot5);
}
try (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp()) {
stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPERIMENTAL);
try(PtlRect rectSize = stampAnnot6.getRect();
PtlRect rect = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop())){
stampAnnot6.setRect(rect);
}
stampAnnot6.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot6);
}
try (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp()) {
stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPIRED);
try(PtlRect rectSize = stampAnnot7.getRect();
PtlRect rect = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop())){
stampAnnot7.setRect(rect);
}
stampAnnot7.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot7);
}
try (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp()) {
stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FINAL);
try(PtlRect rectSize = stampAnnot8.getRect();
PtlRect rect = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop())){
stampAnnot8.setRect(rect);
}
stampAnnot8.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot8);
}
try (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp()) {
stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_COMMENT);
try(PtlRect rectSize = stampAnnot9.getRect();
PtlRect rect = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop())){
stampAnnot9.setRect(rect);
}
stampAnnot9.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot9);
}
try (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp()) {
stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_PUBLIC_RELEASE);
try(PtlRect rectSize = stampAnnot10.getRect();
PtlRect rect = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop())){
stampAnnot10.setRect(rect);
}
stampAnnot10.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot10);
}
try (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp()) {
stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_APPROVED);
try(PtlRect rectSize = stampAnnot11.getRect();
PtlRect rect = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop())){
stampAnnot11.setRect(rect);
}
stampAnnot11.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot11);
}
try (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp()) {
stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_FOR_PUBLIC_RELEASE);
try(PtlRect rectSize = stampAnnot12.getRect();
PtlRect rect = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop())){
stampAnnot12.setRect(rect);
}
stampAnnot12.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot12);
}
try (PtlAnnotStamp stampAnnot13 = new PtlAnnotStamp()) {
stampAnnot13.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SOLD);
try(PtlRect rectSize = stampAnnot13.getRect();
PtlRect rect = new PtlRect(10.0f, 30.0f, 10.0f + rectSize.getRight(), 30.0f + rectSize.getTop())){
stampAnnot13.setRect(rect);
}
stampAnnot13.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot13);
}
try (PtlAnnotStamp stampAnnot14 = new PtlAnnotStamp()) {
stampAnnot14.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_TOP_SECRET);
try(PtlRect rectSize = stampAnnot14.getRect();
PtlRect rect = new PtlRect(10.0f, 10.0f, 10.0f + rectSize.getRight(), 10.0f + rectSize.getTop())){
stampAnnot14.setRect(rect);
}
stampAnnot14.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot14);
}
}
public static void addPreDefinedStampAnnot2(PtlAnnots annots) throws PtlException, Exception, Error
{
try (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp()) {
stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_APPROVED);
try(PtlRect rectSize = stampAnnot1.getRect();
PtlRect rect = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop())){
stampAnnot1.setRect(rect);
}
stampAnnot1.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot1);
}
try (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp()) {
stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_COMPLETED);
try(PtlRect rectSize = stampAnnot2.getRect();
PtlRect rect = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop())){
stampAnnot2.setRect(rect);
}
stampAnnot2.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot2);
}
try (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp();) {
stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_CONFIDENTIAL);
try(PtlRect rectSize = stampAnnot3.getRect();
PtlRect rect = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop())){
stampAnnot3.setRect(rect);
}
stampAnnot3.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot3);
}
try (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp()) {
stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_DRAFT);
try(PtlRect rectSize = stampAnnot4.getRect();
PtlRect rect = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop())){
stampAnnot4.setRect(rect);
}
stampAnnot4.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot4);
}
try (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp()) {
stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FINAL);
try(PtlRect rectSize = stampAnnot5.getRect();
PtlRect rect = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop())){
stampAnnot5.setRect(rect);
}
stampAnnot5.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot5);
}
try (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp()) {
stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_COMMENT);
try(PtlRect rectSize = stampAnnot6.getRect();
PtlRect rect = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop())){
stampAnnot6.setRect(rect);
}
stampAnnot6.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot6);
}
try (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp()) {
stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_PUBLIC_RELEASE);
try(PtlRect rectSize = stampAnnot7.getRect();
PtlRect rect = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop())){
stampAnnot7.setRect(rect);
}
stampAnnot7.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot7);
}
try (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp()) {
stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_INFORMATIONONLY);
try(PtlRect rectSize = stampAnnot8.getRect();
PtlRect rect = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop())){
stampAnnot8.setRect(rect);
}
stampAnnot8.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot8);
}
try (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp()) {
stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_APPROVED);
try(PtlRect rectSize = stampAnnot9.getRect();
PtlRect rect = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop())){
stampAnnot9.setRect(rect);
}
stampAnnot9.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot9);
}
try (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp()) {
stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_FOR_PUBLIC_RELEASE);
try(PtlRect rectSize = stampAnnot10.getRect();
PtlRect rect = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop())){
stampAnnot10.setRect(rect);
}
stampAnnot10.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot10);
}
try (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp()) {
stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_PRELIMINARYRESULTS);
try(PtlRect rectSize = stampAnnot11.getRect();
PtlRect rect = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop())){
stampAnnot11.setRect(rect);
}
stampAnnot11.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot11);
}
try (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp()) {
stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_VOID);
try(PtlRect rectSize = stampAnnot12.getRect();
PtlRect rect = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop())){
stampAnnot12.setRect(rect);
}
stampAnnot12.setAnnotFlags(PtlAnnot.FLAG_PRINT);
annots.append(stampAnnot12);
}
}
public static void addCustomStampAnnotFromPdf(PtlAnnots annots, String pathPdf) throws PtlException, Exception, Error
{
try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp();
PtlPDFDocument doc_custom = new PtlPDFDocument();
PtlParamInput inputCustom = new PtlParamInput(pathPdf)) // PDFファイル
{
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);
// スタンプにするPDFファイルをロードします。
doc_custom.load(inputCustom);
try (PtlPages pagesCustomStamp = doc_custom.getPages())
{
// ページコンテナが空かどうか
if (pagesCustomStamp.isEmpty())
{
System.out.println("ページコンテナが空\n");
return;
}
// 追加された画像ページを取得する
try (PtlPage pageCustomStamp = pagesCustomStamp.get(0)) {
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
try (PtlSize size = pageCustomStamp.getSize();
PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()))
{
stampAnnot.setRect(rect);
}
}
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
public static void addCustomStampAnnotFromImage(PtlAnnots annots, String pathImage) throws PtlException, Exception, Error
{
try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp()) {
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);
try (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage(); // 画像描画パラメータ
PtlParamInput inputCustom = new PtlParamInput(pathImage); // 画像ファイル
PtlParamImagePage paramImagePage = new PtlParamImagePage()) // 画像ページパラメータ
{
// 画像描画パラメータに画像ファイルを設定
paramDrawImage.setImageStream(inputCustom);
// 画像ページパラメータに画像描画パラメータを設定
paramImagePage.setImage(paramDrawImage);
// 画像ページのサイズを画像サイズにあわせる
paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);
// 画像ページパラメータから作成したページ
try (PtlPage pageCustomStamp = new PtlPage(paramImagePage))
{
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
try (PtlSize size = pageCustomStamp.getSize();
PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()))
{
stampAnnot.setRect(rect);
}
}
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
public static void addCustomStampAnnotFromDrawContent(PtlAnnots annots) throws PtlException, Exception, Error
{
float sizeStamp = 30.0f;
try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp();
PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f + sizeStamp, 100.0f + sizeStamp))
{
stampAnnot.setRect(rect);
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);
try (PtlPage pageCustomStamp = new PtlPage();
PtlRect rectMedia = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp);
PtlContent content = pageCustomStamp.getContent())
{
// ページサイズ
pageCustomStamp.setMediaBox(rectMedia);
// コンテントに日付印を描画
drawDateStamp(content, sizeStamp, "2014/09/8", "鈴木");
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
public static void drawDateStamp(PtlContent content, float sizeStamp, String textDate, String textName) throws PtlException, Exception, Error
{
try (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape();
PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f); // スタンプの色
PtlColorNone colorNone = new PtlColorNone();
PtlRect rectStamp = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp))
{
// スタンプの円/線を描画するパラメーター
paramDrawShape.setLineStyle(PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID);
paramDrawShape.setLineWidth(PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN);
paramDrawShape.setLineColor(color);
paramDrawShape.setFillColor(colorNone);
// スタンプの円を描画
content.drawCircle(rectStamp, paramDrawShape);
// 円の中心から15度で線と円が交わるとする
double rad = 15.0f * Math.PI / 180.0f;
// 円の半径
float r = sizeStamp / 2;
// 円の中心から交点へのx方向の長さ
float xx = r * (float)Math.cos(rad);
// 円の中心から交点へのy方向の長さ
float yy = r * (float)Math.sin(rad);
// 上線左端のx座標
float from_x = r - xx;
// 上線左端のy座標
float to_x = r + xx;
// 下線左端のx座標
float upper_y = r + yy;
// 下線左端のy座標
float lower_y = r - yy;
try (PtlPoint posFrom1 = new PtlPoint(from_x, upper_y);
PtlPoint posTo1 = new PtlPoint(to_x, upper_y);
PtlPoint posFrom2 = new PtlPoint(from_x, lower_y);
PtlPoint posTo2 = new PtlPoint(to_x, lower_y))
{
// 円の中に上線を描画
content.drawLine(posFrom1, posTo1, paramDrawShape);
// 円の中に下線を描画
content.drawLine(posFrom2, posTo2, paramDrawShape);
}
// 日付のフォントサイズ
float fontSizeDate = sizeStamp / 2.15f; // yyyy/mm/dd を想定
try (PtlParamWriteString paramWriteStringDate = new PtlParamWriteString();
PtlRect rectDate = new PtlRect(from_x, lower_y, to_x, upper_y);
PtlParamFont font = new PtlParamFont("MS-Gothic", fontSizeDate, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
paramWriteStringDate.setFont(font);
paramWriteStringDate.setTextColor(color);
paramWriteStringDate.setOutlineColor(color);
// 日付を描画
content.writeString(rectDate, PtlContent.ALIGN.ALIGN_CENTER, textDate, paramWriteStringDate);
}
// 下線から名前を描画する矩形までのマージン
float margin =sizeStamp / 40.0f;
// 名前のフォントサイズ
float fontSizeName = sizeStamp / 1.5f;
try (PtlParamWriteString paramWriteStringName = new PtlParamWriteString();
PtlRect rectName = new PtlRect(from_x, lower_y - (upper_y - lower_y) - margin, to_x, lower_y - margin);
PtlParamFont font = new PtlParamFont("MS-Mincho", fontSizeDate, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
// 名前を書く矩形の高さ
float heightNameBase = upper_y - lower_y;
float heightName = fontSizeName * 25.4f / 72.0f;
while (heightName > heightNameBase)
{
fontSizeName -= 0.5f;
heightName = fontSizeName * 25.4f / 72.0f;
}
// 線の幅
float widthLine = 0.5f;
// 線幅を減じた円の半径
float rr = r - widthLine;
// 名前の高さのラジアン
float d = (float)Math.asin(((heightName + yy + margin) / rr));
// 名前をおさめる矩形の幅
float widthName = rr * (float)Math.cos(d) * 2;
// テキストの長さ
float textWidthName = font.getStringWidth(textName);
// 名前の長さによるフォントサイズの調整
while (textWidthName > widthName)
{
fontSizeName -= 0.5f;
font.setSize(fontSizeName);
textWidthName = font.getStringWidth(textName);
heightName = fontSizeName * 25.4f / 72.0f;
d = (float)Math.asin((heightName + yy + margin) / rr);
widthName = rr * (float)Math.cos(d) * 2;
}
paramWriteStringName.setFont(font);
paramWriteStringName.setTextColor(color);
paramWriteStringName.setOutlineColor(color);
// 名前を描画
content.writeString(rectName, PtlContent.ALIGN.ALIGN_CENTER, textName, paramWriteStringName);
}
}
}
}
/*
Antenna House PDF Tool API V7.0
.NET Interface sample program
概要:スタンプ注釈の作成
Copyright 2013-2021 Antenna House, Inc.
*/
using System;
using PdfTkNet;
namespace AppendAnnotStamp
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("usage: AppendAnnotStamp.exe in-pdf-file out-pdf-file スタンプ種類 [in-xxx-file]\n");
Console.WriteLine("スタンプ種類\n0 : 標準 1 : 標準2 2 : カスタム(PDF) 3 : カスタム(Image) 4 : カスタム(日付印)\n");
return;
}
String stampKind = args[2];
switch (stampKind)
{
case "0":
case "1":
case "2":
case "3":
case "4":
break;
default:
Console.WriteLine("usage: AppendAnnotStamp.exe in-pdf-file out-pdf-file スタンプ種類 [in-xxx-file]\n");
Console.WriteLine("スタンプ種類\n0 : 標準 1 : 標準2 2 : カスタム(PDF) 3 : カスタム(Image) 4 : カスタム(日付印)\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))
{
// 注釈コンテナの取得
using (PtlAnnots annots = page.getAnnots())
{
switch (stampKind)
{
case "0":
// 標準
addPreDefinedStampAnnot(annots);
break;
case "1":
// 標準2
addPreDefinedStampAnnot2(annots);
break;
case "2":
// 読み込んだPDFを外観とするスタンプ
if (args.Length < 4)
{
Console.WriteLine("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-pdf-file");
return;
}
addCustomStampAnnotFromPdf(annots, args[3]);
break;
case "3":
// 読み込んだ画像を外観とするスタンプ
if (args.Length < 4)
{
Console.WriteLine("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-image-file");
return;
}
addCustomStampAnnotFromImage(annots, args[3]);
break;
case "4":
// 日付印を外観として描画
addCustomStampAnnotFromDrawContent(annots);
break;
}
}
}
}
// 別のファイルに保存します。
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 addPreDefinedStampAnnot(PtlAnnots annots)
{
using (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp())
{
stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_APPROVED);
using (PtlRect rectSize = stampAnnot1.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop()))
{
stampAnnot1.setRect(rectAnnot);
}
stampAnnot1.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot1);
}
using (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp())
{
stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_AS_IS);
using (PtlRect rectSize = stampAnnot2.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop()))
{
stampAnnot2.setRect(rectAnnot);
}
stampAnnot2.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot2);
}
using (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp())
{
stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CONFIDENTIAL);
using (PtlRect rectSize = stampAnnot3.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop()))
{
stampAnnot3.setRect(rectAnnot);
}
stampAnnot3.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot3);
}
using (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp())
{
stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DEPARTMENTAL);
using (PtlRect rectSize = stampAnnot4.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop()))
{
stampAnnot4.setRect(rectAnnot);
}
stampAnnot4.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot4);
}
using (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp())
{
stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DRAFT);
using (PtlRect rectSize = stampAnnot5.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop()))
{
stampAnnot5.setRect(rectAnnot);
}
stampAnnot5.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot5);
}
using (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp())
{
stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPERIMENTAL);
using (PtlRect rectSize = stampAnnot6.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop()))
{
stampAnnot6.setRect(rectAnnot);
}
stampAnnot6.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot6);
}
using (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp())
{
stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPIRED);
using (PtlRect rectSize = stampAnnot7.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop()))
{
stampAnnot7.setRect(rectAnnot);
}
stampAnnot7.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot7);
}
using (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp())
{
stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FINAL);
using (PtlRect rectSize = stampAnnot8.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop()))
{
stampAnnot8.setRect(rectAnnot);
}
stampAnnot8.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot8);
}
using (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp())
{
stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_COMMENT);
using (PtlRect rectSize = stampAnnot9.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop()))
{
stampAnnot9.setRect(rectAnnot);
}
stampAnnot9.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot9);
}
using (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp())
{
stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_PUBLIC_RELEASE);
using (PtlRect rectSize = stampAnnot10.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop()))
{
stampAnnot10.setRect(rectAnnot);
}
stampAnnot10.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot10);
}
using (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp())
{
stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_APPROVED);
using (PtlRect rectSize = stampAnnot11.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop()))
{
stampAnnot11.setRect(rectAnnot);
}
stampAnnot11.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot11);
}
using (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp())
{
stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_FOR_PUBLIC_RELEASE);
using (PtlRect rectSize = stampAnnot12.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop()))
{
stampAnnot12.setRect(rectAnnot);
}
stampAnnot12.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot12);
}
using (PtlAnnotStamp stampAnnot13 = new PtlAnnotStamp())
{
stampAnnot13.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SOLD);
using (PtlRect rectSize = stampAnnot13.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 30.0f, 10.0f + rectSize.getRight(), 30.0f + rectSize.getTop()))
{
stampAnnot13.setRect(rectAnnot);
}
stampAnnot13.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot13);
}
using (PtlAnnotStamp stampAnnot14 = new PtlAnnotStamp())
{
stampAnnot14.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_TOP_SECRET);
using (PtlRect rectSize = stampAnnot14.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 10.0f, 10.0f + rectSize.getRight(), 10.0f + rectSize.getTop()))
{
stampAnnot14.setRect(rectAnnot);
}
stampAnnot14.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot14);
}
}
static void addPreDefinedStampAnnot2(PtlAnnots annots)
{
using (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp())
{
stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_APPROVED);
using (PtlRect rectSize = stampAnnot1.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop()))
{
stampAnnot1.setRect(rectAnnot);
}
stampAnnot1.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot1);
}
using (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp())
{
stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_COMPLETED);
using (PtlRect rectSize = stampAnnot2.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop()))
{
stampAnnot2.setRect(rectAnnot);
}
stampAnnot2.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot2);
}
using (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp())
{
stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_CONFIDENTIAL);
using (PtlRect rectSize = stampAnnot3.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop()))
{
stampAnnot3.setRect(rectAnnot);
}
stampAnnot3.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot3);
}
using (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp())
{
stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_DRAFT);
using (PtlRect rectSize = stampAnnot4.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop()))
{
stampAnnot4.setRect(rectAnnot);
}
stampAnnot4.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot4);
}
using (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp())
{
stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FINAL);
using (PtlRect rectSize = stampAnnot5.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop()))
{
stampAnnot5.setRect(rectAnnot);
}
stampAnnot5.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot5);
}
using (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp())
{
stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_COMMENT);
using (PtlRect rectSize = stampAnnot6.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop()))
{
stampAnnot6.setRect(rectAnnot);
}
stampAnnot6.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot6);
}
using (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp())
{
stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_PUBLIC_RELEASE);
using (PtlRect rectSize = stampAnnot7.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop()))
{
stampAnnot7.setRect(rectAnnot);
}
stampAnnot7.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot7);
}
using (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp())
{
stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_INFORMATIONONLY);
using (PtlRect rectSize = stampAnnot8.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop()))
{
stampAnnot8.setRect(rectAnnot);
}
stampAnnot8.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot8);
}
using (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp())
{
stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_APPROVED);
using (PtlRect rectSize = stampAnnot9.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop()))
{
stampAnnot9.setRect(rectAnnot);
}
stampAnnot9.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot9);
}
using (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp())
{
stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_FOR_PUBLIC_RELEASE);
using (PtlRect rectSize = stampAnnot10.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop()))
{
stampAnnot10.setRect(rectAnnot);
}
stampAnnot10.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot10);
}
using (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp())
{
stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_PRELIMINARYRESULTS);
using (PtlRect rectSize = stampAnnot11.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop()))
{
stampAnnot11.setRect(rectAnnot);
}
stampAnnot11.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot11);
}
using (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp())
{
stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_VOID);
using (PtlRect rectSize = stampAnnot12.getRect())
using (PtlRect rectAnnot = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop()))
{
stampAnnot12.setRect(rectAnnot);
}
stampAnnot12.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
annots.append(stampAnnot12);
}
}
static void addCustomStampAnnotFromPdf(PtlAnnots annots, String pathPdf)
{
using (PtlAnnotStamp stampAnnot = new PtlAnnotStamp())
using (PtlPDFDocument doc_custom = new PtlPDFDocument())
using (PtlParamInput inputCustom = new PtlParamInput(pathPdf)) // PDFファイル
{
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
// スタンプにするPDFファイルをロードします。
doc_custom.load(inputCustom);
using (PtlPages pagesCustomStamp = doc_custom.getPages())
{
// ページコンテナが空かどうか
if (pagesCustomStamp.isEmpty())
{
Console.WriteLine("ページコンテナが空\n");
return;
}
// 追加された画像ページを取得する
using (PtlPage pageCustomStamp = pagesCustomStamp.get(0)) {
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
using (PtlSize size = pageCustomStamp.getSize())
using (PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f + size.getWidth(), 100.0f + size.getHeight()))
{
stampAnnot.setRect(rect);
}
}
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
static void addCustomStampAnnotFromImage(PtlAnnots annots, String pathImage)
{
using (PtlAnnotStamp stampAnnot = new PtlAnnotStamp())
{
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage()) // 画像描画パラメータ
using (PtlParamInput inputCustom = new PtlParamInput(pathImage)) // 画像ファイル
using (PtlParamImagePage paramImagePage = new PtlParamImagePage()) // 画像ページパラメータ
{
// 画像描画パラメータに画像ファイルを設定
paramDrawImage.setImageStream(inputCustom);
// 画像ページパラメータに画像描画パラメータを設定
paramImagePage.setImage(paramDrawImage);
// 画像ページのサイズを画像サイズにあわせる
paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);
// 画像ページパラメータから作成したページ
using (PtlPage pageCustomStamp = new PtlPage(paramImagePage))
{
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
using (PtlSize size = pageCustomStamp.getSize())
using (PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f + size.getWidth(), 100.0f + size.getHeight()))
{
stampAnnot.setRect(rect);
}
}
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
static void addCustomStampAnnotFromDrawContent(PtlAnnots annots)
{
float sizeStamp = 30.0f;
using (PtlAnnotStamp stampAnnot = new PtlAnnotStamp())
using (PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f + sizeStamp, 100.0f + sizeStamp))
using (PtlPDFDocument doc_custom = new PtlPDFDocument())
{
stampAnnot.setRect(rect);
stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
stampAnnot.setAnnotFlags(PtlAnnot.ANNOT_FLAGS.FLAG_PRINT);
using (PtlPage pageCustomStamp = new PtlPage()) // 日付印を描画する仮のページ
using (PtlRect rectMedia = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp))
using (PtlContent content = pageCustomStamp.getContent()) // 日付印を描画するコンテント
{
// ページサイズ
pageCustomStamp.setMediaBox(rectMedia);
// コンテントに日付印を描画
drawDateStamp(content, sizeStamp, "2014/09/8", "鈴木");
// 画像ページを注釈に追加する
stampAnnot.setPage(pageCustomStamp);
}
stampAnnot.setIconName("MyIcon");
annots.append(stampAnnot);
}
}
static void drawDateStamp(PtlContent content, float sizeStamp, String textDate, String textName)
{
using (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape()) // スタンプの円/線を描画するパラメーター
using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f)) // スタンプの色
using (PtlColorNone colorNone = new PtlColorNone())
using (PtlRect rectStamp = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp))
{
// スタンプの円/線を描画するパラメーター
paramDrawShape.setLineStyle(PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID);
paramDrawShape.setLineWidth(PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN);
paramDrawShape.setLineColor(color);
paramDrawShape.setFillColor(colorNone);
// スタンプの円を描画
content.drawCircle(rectStamp, paramDrawShape);
// 円の中心から15度で線と円が交わるとする
double rad = 15.0f * Math.PI / 180.0f;
// 円の半径
float r = sizeStamp / 2;
// 円の中心から交点へのx方向の長さ
float xx = r * (float)Math.Cos(rad);
// 円の中心から交点へのy方向の長さ
float yy = r * (float)Math.Sin(rad);
// 上線左端のx座標
float from_x = r - xx;
// 上線左端のy座標
float to_x = r + xx;
// 下線左端のx座標
float upper_y = r + yy;
// 下線左端のy座標
float lower_y = r - yy;
using (PtlPoint posFrom1 = new PtlPoint(from_x, upper_y))
using (PtlPoint posTo1 = new PtlPoint(to_x, upper_y))
using (PtlPoint posFrom2 = new PtlPoint(from_x, lower_y))
using (PtlPoint posTo2 = new PtlPoint(to_x, lower_y))
{
// 円の中に上線を描画
content.drawLine(posFrom1, posTo1, paramDrawShape);
// 円の中に下線を描画
content.drawLine(posFrom2, posTo2, paramDrawShape);
}
// 日付のフォントサイズ
float fontSizeDate = sizeStamp / 2.15f; // yyyy/mm/dd を想定
using (PtlParamWriteString paramWriteStringDate = new PtlParamWriteString())
using (PtlRect rectDate = new PtlRect(from_x, lower_y, to_x, upper_y))
using (PtlParamFont font = new PtlParamFont("MS-Gothic", fontSizeDate, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
paramWriteStringDate.setFont(font);
paramWriteStringDate.setTextColor(color);
paramWriteStringDate.setOutlineColor(color);
// 日付を描画
content.writeString(rectDate, PtlContent.ALIGN.ALIGN_CENTER, textDate, paramWriteStringDate);
}
// 下線から名前を描画する矩形までのマージン
float margin =sizeStamp / 40.0f;
// 名前のフォントサイズ
float fontSizeName = sizeStamp / 1.5f;
using (PtlParamWriteString paramWriteStringName = new PtlParamWriteString())
using (PtlRect rectName = new PtlRect(from_x, lower_y - (upper_y - lower_y) - margin, to_x, lower_y - margin))
using (PtlParamFont font = new PtlParamFont("MS-Mincho", fontSizeName, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
{
// 名前を書く矩形の高さ
float heightNameBase = upper_y - lower_y;
float heightName = fontSizeName * 25.4f / 72.0f;
while (heightName > heightNameBase)
{
fontSizeName -= 0.5f;
heightName = fontSizeName * 25.4f / 72.0f;
}
// 線の幅
float widthLine = 0.5f;
// 線幅を減じた円の半径
float rr = r - widthLine;
// 名前の高さのラジアン
float d = (float)Math.Asin(((heightName + yy + margin) / rr));
// 名前をおさめる矩形の幅
float widthName = rr * (float)Math.Cos(d) * 2;
// テキストの長さ
float textWidthName = font.getStringWidth(textName);
// 名前の長さによるフォントサイズの調整
while (textWidthName > widthName)
{
fontSizeName -= 0.5f;
font.setSize(fontSizeName);
textWidthName = font.getStringWidth(textName);
heightName = fontSizeName * 25.4f / 72.0f;
d = (float)Math.Asin((heightName + yy + margin) / rr);
widthName = rr * (float)Math.Cos(d) * 2;
}
paramWriteStringName.setFont(font);
paramWriteStringName.setTextColor(color);
paramWriteStringName.setOutlineColor(color);
// 名前を描画
content.writeString(rectName, PtlContent.ALIGN.ALIGN_CENTER, textName, paramWriteStringName);
}
}
}
}
}
実行例
コマンドラインでの実行例
AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_0.pdf 0 完了! AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_1.pdf 1 完了! AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_2.pdf 2 C:\in\icon.pdf 完了! AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_3.pdf 3 C:\in\icon.jpg 完了! AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_4.pdf 4 完了!
java -jar AppendAnnotStamp.jar C:\in\in.pdf C:\sav\outAppendAnnotStamp_0.pdf 0 -- 完了 -- java -jar AppendAnnotStamp.jar C:\in\in.pdf C:\sav\outAppendAnnotStamp_1.pdf 1 -- 完了 -- java -jar AppendAnnotStamp.jar C:\in\in.pdf C:\sav\outAppendAnnotStamp_2.pdf 2 C:\in\icon.pdf -- 完了 -- java -jar AppendAnnotStamp.jar C:\in\in.pdf C:\sav\outAppendAnnotStamp_3.pdf 3 C:\in\icon.jpg -- 完了 -- java -jar AppendAnnotStamp.jar C:\in\in.pdf C:\sav\outAppendAnnotStamp_4.pdf 4 -- 完了 --
AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_0.pdf 0 -- 完了 -- AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_1.pdf 1 -- 完了 -- AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_2.pdf 2 C:\in\icon.pdf -- 完了 -- AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_3.pdf 3 C:\in\icon.jpg -- 完了 -- AppendAnnotStamp.exe C:\in\in.pdf C:\sav\outAppendAnnotStamp_4.pdf 4 -- 完了 --
出力結果イメージ
出力されたPDFの1ページにはスタンプ注釈が追加されている。

