iText では、範囲内にうまく文書を収めることはできないんだろうか?
とりあえず、テキストの範囲の大きさを測れないか試してみる。


その前に iText が表現できる文書の構造についてメモしておく。

Paragraph は、最も使われるテキストオブジェクトです。これは、ひとつもしくはそれ以上の数の高機能オブジェクト―Chunk、Phrase、List、Imageなど―の ArrayList です。Chunk と違い、Paragraph は異なるフォントのテキストを含ませることができます。
(中略)
この節は、完全性のための Phrase という風変わりなクラスを説明します。ほとんどの場合、Paragraph クラスを使うことになるでしょう。
(中略)
Chunk は、文書にテキストを追加するときの最小単位であり、高機能オブジェクトを構成する基本要素となります。Chunk 内は、同じ性質―フォント、サイズ、スタイル、色など―をしています。
(中略)
List オブジェクト は、HTMLにおける <UL> あるいは <OL> タグに似た働きをします。iText において <LI> と同値なものは、ListItem です。

チュートリアル: iText.NET - 基本的なテキストオブジェクト

Paragraph > Phrase > Chunk > java.lang.String, com.lowagie.text.Image

Paragraph や Phrase に Chunk, String, Image を追加していく感じらしい。

実際に Chunk を追加していくたびに Paragraph や Phrase のサイズを確認してみたが、

Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0

という数値からぜんぜん変化がない。
範囲のサイズを測るには、Chunk のフォント情報から大きさを自分で計算していくしかないのかも?

サンプルソースコード(SizeSample.java)


import java.io.*;
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
 
public class SizeSample {
 
  public static void main(String[] args) throws Exception {
 
    Document doc = new Document();
 
    PdfWriter.getInstance(doc,
      new FileOutputStream("20070804_size.pdf"));
 
    doc.open();
 
    // Fonts in TrueType collections are addressed by index such as "msgothic.ttc,0".
    //String name = "C:\\WINDOWS\\Fonts\\msgothic.ttc,0";
    String name = "C:\\WINDOWS\\Fonts\\Diego Regular.ttf";
    String encoding = BaseFont.IDENTITY_H; // The Unicode encoding with horizontal writing.
    boolean embedded = BaseFont.EMBEDDED; // if the font has to be embedded
    BaseFont baseFont = BaseFont.createFont(name, encoding, embedded);
    
    String[] s = {
      "hello, world",
      "oh la la",
      "it's a beautiful day",
      "Folklore, legends, myths and fairy tales have followed childhood through the ages, for every healthy youngster has a wholesome and instinctive love for stories fantastic, marvelous and manifestly unreal. The winged fairies of Grimm and Andersen have brought more happiness to childish hearts than all other human creations.",
    };
    
    final int loop = 3;
    
    Chunk[] chunk = new Chunk[s.length * loop];
    
    // Phrase と Paragraph Chunk を追加していくときのちがい
    Phrase phrase = new  Phrase();
    Paragraph paragraph = new Paragraph();
    for(int i=0; i<loop; i++){
      float fontsize = 12 * (i + 1);
      Font font = new Font(baseFont, fontsize);
      System.out.println("----------" + fontsize + "----------");
      for(int j=0; j<s.length; j++){
        chunk[j] = new Chunk(s[j], font);
        phrase.add(chunk[j]);
        phrase.add(Chunk.NEWLINE); // 改行
        paragraph.add(chunk[j]);
        paragraph.add(Chunk.NEWLINE); // 改行
        // 諸情報の出力
        System.out.println(s[j]);
        System.out.println("BaseFont#AscentPoint=" + baseFont.getAscentPoint(s[j], fontsize));
        System.out.println("BaseFont#DescentPoint=" + baseFont.getDescentPoint(s[j], fontsize));
        System.out.println("BaseFont#WidthPoint=" + baseFont.getWidthPoint(s[j], fontsize));
        System.out.println("Chunk#WidthPoint=" + chunk[j].getWidthPoint());
        System.out.println("Chunk#TextRise=" + chunk[j].getTextRise());
        System.out.println("Phrase#Leading=" + phrase.getLeading());
        System.out.println("Paragraph#Leading=" + paragraph.getLeading());
        System.out.println("Paragraph#MultipliedLeading=" + paragraph.getMultipliedLeading());
        System.out.println("Paragraph#TotalLeading=" + paragraph.getTotalLeading());
      }
    }
    
    doc.add(phrase);
    doc.add(Chunk.NEWLINE);
    doc.add(paragraph);
    doc.add(Chunk.NEWLINE);
 
    doc.close();
  }
  
}

諸情報の出力結果


----------12.0----------
hello, world
BaseFont#AscentPoint=7.26
BaseFont#DescentPoint=-1.32
BaseFont#WidthPoint=53.088005
Chunk#WidthPoint=53.088005
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
oh la la
BaseFont#AscentPoint=7.248
BaseFont#DescentPoint=-0.13200001
BaseFont#WidthPoint=32.22
Chunk#WidthPoint=32.22
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
it's a beautiful day
BaseFont#AscentPoint=9.132
BaseFont#DescentPoint=-2.196
BaseFont#WidthPoint=84.36
Chunk#WidthPoint=84.36
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
Folklore, legends, myths and fairy tales have followed childhood through the ages, for every healthy youngster has a wholesome and instinctive love for stories fantastic, marvelous and manifestly unreal. The winged fairies of Grimm and Andersen have brought more happiness to childish hearts than all other human creations.
BaseFont#AscentPoint=9.348001
BaseFont#DescentPoint=-2.328
BaseFont#WidthPoint=1525.8601
Chunk#WidthPoint=1525.8601
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
----------24.0----------
hello, world
BaseFont#AscentPoint=14.52
BaseFont#DescentPoint=-2.64
BaseFont#WidthPoint=106.17601
Chunk#WidthPoint=106.17601
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
oh la la
BaseFont#AscentPoint=14.496
BaseFont#DescentPoint=-0.26400003
BaseFont#WidthPoint=64.44
Chunk#WidthPoint=64.44
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
it's a beautiful day
BaseFont#AscentPoint=18.264
BaseFont#DescentPoint=-4.392
BaseFont#WidthPoint=168.72
Chunk#WidthPoint=168.72
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
Folklore, legends, myths and fairy tales have followed childhood through the ages, for every healthy youngster has a wholesome and instinctive love for stories fantastic, marvelous and manifestly unreal. The winged fairies of Grimm and Andersen have brought more happiness to childish hearts than all other human creations.
BaseFont#AscentPoint=18.696001
BaseFont#DescentPoint=-4.656
BaseFont#WidthPoint=3051.7202
Chunk#WidthPoint=3051.7202
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
----------36.0----------
hello, world
BaseFont#AscentPoint=21.78
BaseFont#DescentPoint=-3.9600003
BaseFont#WidthPoint=159.264
Chunk#WidthPoint=159.264
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
oh la la
BaseFont#AscentPoint=21.744001
BaseFont#DescentPoint=-0.39600003
BaseFont#WidthPoint=96.66
Chunk#WidthPoint=96.66
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
it's a beautiful day
BaseFont#AscentPoint=27.396002
BaseFont#DescentPoint=-6.5880003
BaseFont#WidthPoint=253.08
Chunk#WidthPoint=253.08
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0
Folklore, legends, myths and fairy tales have followed childhood through the ages, for every healthy youngster has a wholesome and instinctive love for stories fantastic, marvelous and manifestly unreal. The winged fairies of Grimm and Andersen have brought more happiness to childish hearts than all other human creations.
BaseFont#AscentPoint=28.044003
BaseFont#DescentPoint=-6.984
BaseFont#WidthPoint=4577.58
Chunk#WidthPoint=4577.58
Chunk#TextRise=0.0
Phrase#Leading=16.0
Paragraph#Leading=16.0
Paragraph#MultipliedLeading=0.0
Paragraph#TotalLeading=16.0

出力したPDF -> 20070804_size.pdf

tags: zlashdot Java Java PDF iText

Posted by NI-Lab. (@nilab)