
文字列の角度を指定文字列全体の角度を指定して配置します。
文字列は、回転した後に、配置矩形と配置基準に従って配置されます。
package cookbook;
import java.io.*;
import jp.co.antenna.ptl.*;
public class AddTextSetAngle{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 6)
{
System.out.println("usage: java AddTextSetAngle in-pdf-file out-pdf-file page-num text-to-add text-align text-angle");
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
// PDFファイルをロードします。
doc.load(inputFile);
int pageToAdd = Integer.parseInt(args[2]);
int numPages = doc.getPageCount();
System.out.println("ページ数:" + numPages);
if((numPages < 0)||(numPages < pageToAdd))
{
System.err.println("テキスト挿入ページは全ページ数よりも小さい正の値を指定してください。");
return;
}
String textToAdd = args[3];
String textAlign = args[4];
float angle = Float.parseFloat(args[5]);
...【RemovePages.javaと同じ処理のため省略
・doc.getPages()メソッドを用いてPtlPages pagesにページコンテナを取得
・ページコンテナが空だった場合にエラーを出力して終了】...
try (PtlPage page = pages.get(pageToAdd - 1);// ページの取得(パラメータindexは0が先頭のため1を引く)
PtlContent content = page.getContent();// ページコンテントの取得
PtlRect outputRect = setOutputRect(br);// 出力矩形の設定(setOutputRectを用いて初期化)
PtlParamWriteString plainParam = new PtlParamWriteString()) // 文字描画のパラメータクラス。今回は何も設定しない。
{
// 文字列出力
content.writeString(outputRect, PtlContent.ALIGN.valueOf(textAlign), angle, textToAdd, plainParam);
}
catch (IllegalArgumentException ex){//PtlContent.ALIGNの指定が誤っていた場合のエラーメッセージ
System.out.println(ex.getMessage());
System.out.println("ERROR : alignにはPtlContent.ALIGNに含まれる名前を指定してください。");
ex.printStackTrace();
}
}
...【AppendPages.javaと同じ処理のため省略
・PtlParamOutputを用いてPtlPDFDocument docの内容を出力
・PtlException, Exception, Error を catchするエラー処理
・finally文で"--完了--"と表示する処理】...
}
public static PtlRect setOutputRect(BufferedReader br) throws IOException, PtlException, Exception, Error{
float top, bottom, left, right;
boolean isValueOkay = false;
PtlRect outputRect = new PtlRect();
while(!isValueOkay)
{
System.out.println("文字列の配置矩形の各数値を入力してください。");
System.out.print("top (mm) : ");
top = Float.parseFloat(br.readLine());
System.out.print("bottom (mm) : ");
bottom = Float.parseFloat(br.readLine());
if(top < bottom)
{
System.out.println("topの値はbottomよりも大きい値を指定して再度入力してください。");
continue;
}
System.out.print("left (mm) : ");
left = Float.parseFloat(br.readLine());
System.out.print("right (mm) : ");
right = Float.parseFloat(br.readLine());
if(right < left)
{
System.out.println("rightの値はleftよりも大きい値を指定して再度入力してください。");
continue;
}
//矩形を正しく指定できた場合の処理
isValueOkay = true;
outputRect.setLeft(left);
outputRect.setBottom(bottom);
outputRect.setRight(right);
outputRect.setTop(top);
}
return outputRect;
}
}
AddTextSetAngle.java
C:\samples>java cookbook.AddTextSetAngle usage: java AddTextSetAngle in-pdf-file out-pdf-file page-num text-to-add text-align text-angle C:\samples>java cookbook.AddTextSetAngle blankPage.pdf addTextSetAngle.pdf 1 テキストを45度傾けます ALIGN_TOP 45 ページ数:1 指定する矩形の各数値を入力してください。 top (mm) : 60 bottom (mm) : 40 left (mm) : 100 right (mm) : 150 -- 完了 --
