
テキストボックスによる描画の際にフォントや文字の色といったテキストのパラメータを指定します。
PtlTextBox.writeString()によるテキスト出力の際に、PtlParamWriteStringTextBoxクラスを用いてパラメータを設定できます。
PtlParamWriteStringTextBoxはPtlParamWriteStringのサブクラスであるため、文字列描画の際に指定できたテキストボックスフォントや文字の色といったパラメータを指定できます。
具体的には、フォント属性、文字の色、文字の輪郭色、描画テキストの不透明度を指定します。(PtlParamWriteStringと文字列描画で指定可能なパラメータの詳細は「『PDF CookBook』(第1巻)2.1 テキスト描画」を参照してください)
サンプルプログラムでは、テキストボックスで文字列を描画する際に、フォントファミリー名、フォントサイズ、テキストの色のRGB値、テキストの輪郭色のRGB値、透明度を指定します。その他の項目に関してはTBoxDrawTextBox.javaと同一の動作をします。
package cookbook;
import java.io.*;
import jp.co.antenna.ptl.*;
public class TBoxDrawTextBoxSetTextParams{
// そのクラスのusageを表示する関数
private static void printUsage() {
System.out.println("usage: java TBoxDrawTextBoxSetTextParams"
+ " in-pdf-file out-pdf-file"
+ " page-num text-to-add text-align"
+ " font-family font-size"
+ " text-color-R text-color-G text-color-B"
+ " text-outline-color-R text-outline-color-G text-outline-color-B"
+ " text-opacity");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 13) {
printUsage();
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];
String fontFamily = args[5];
float fontSize = Float.parseFloat(args[6]);
float textR = Float.parseFloat(args[7]);
float textG = Float.parseFloat(args[8]);
float textB = Float.parseFloat(args[9]);
float textOutlineR = Float.parseFloat(args[10]);
float textOutlineG = Float.parseFloat(args[11]);
float textOutlineB = Float.parseFloat(args[12]);
float opacity = Float.parseFloat(args[13]);
if((opacity < 0) || (1.0f < opacity)) {
System.err.println("0.0から1.0の範囲で値を設定し直してください。");
return;
}
try (PtlPages pages = doc.getPages()) {//ページコンテナの取得
// ページコンテナが空かどうか
if (pages.isEmpty()) {
System.err.println("ページコンテナが空");
return;
}
try (PtlPage page = pages.get(pageToAdd - 1);// ページの取得(パラメータindexは0が先頭のため1を引く)
PtlContent content = page.getContent();// ページコンテントの取得
PtlRect outputRect = new PtlRect();// 出力矩形の初期化
PtlTextBox textBox = content.drawTextBox(setRectCoordinate(br, outputRect), PtlContent.ALIGN.valueOf(textAlign), 210, 297);
PtlParamWriteStringTextBox textParam = new PtlParamWriteStringTextBox();// 文字描画のパラメータクラス。
PtlParamFont font = new PtlParamFont();
PtlColorDeviceRGB textColor = new PtlColorDeviceRGB(textR, textG, textB);
PtlColorDeviceRGB outlineColor =
new PtlColorDeviceRGB(textOutlineR, textOutlineG, textOutlineB)) {
// textParamの指定
// フォントの各種項目を指定
font.setName(fontFamily);
font.setSize(fontSize);
// パラメータにフォントを設定
textParam.setFont(font);
// 文字の色指定
textParam.setTextColor(textColor);
// 文字の背景色の指定
textParam.setOutlineColor(outlineColor);
// 文字の透明度の指定
textParam.setOpacity(opacity);
// 文字列出力
textBox.writeString(textToAdd, textParam);
textBox.terminate();
}
catch (IllegalArgumentException ex) {//PtlContent.ALIGNの指定が誤っていた場合のエラーメッセージ
System.out.println(ex.getMessage());
System.out.println("ERROR : alignにはPtlContent.ALIGNに含まれる名前を指定してください。");
ex.printStackTrace();
}
}
// ファイルに保存します。
doc.save(outputFile);
}catch (NumberFormatException numfe) {
System.out.println("RGB及び透明度の指定には数値を使用して下さい。");
System.out.println(numfe.getMessage());
numfe.printStackTrace();
}
...【GetPDFVersion.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
public static PtlRect setRectCoordinate(BufferedReader br, PtlRect outputRect)
throws IOException, PtlException, Exception, Error {
...【TBoxDrawTextBox.javaと同じ処理のため省略
・[top, bottom, left, right]を設定し、PtlRect型変数を返す関数】...
}
}
TBoxDrawTextBoxSetTextParams.java
C:\samples>java cookbook.TBoxDrawTextBoxSetTextParams usage: java TBoxDrawTextBoxSetTextParams in-pdf-file out-pdf-file page-num text-to-add text-align font-family font-size text-color-R text-color-G text-color-B text-outline-color-R text-outline-color-G text-outline-color-B text-opacity C:\samples>java cookbook.TBoxDrawTextBoxSetTextParams gray_landscape_A4blank.pdf Output_TBoxDrawTextBoxSetTextParams.pdf 1 "テキストボックス入力文章。このようにテキストボックスに入力された文章は折り返されます。" ALIGN_CENTER "MS 明朝" 24 1.0 0.0 0.0 0.0 1.0 0.0 0.7 ページ数:1 指定する矩形の各数値を入力してください。 top (mm) : 120 bottom (mm) : 80 left (mm) : 50 right (mm) : 130 -- 完了 --
この操作例ではフォントをMS 明朝、フォントサイズを24ポイント、輪郭線の色を緑、塗りつぶしの色を赤で指定しています。
