2013年1月21日月曜日

how can I change char-tracking, I mean letter-spacing by IText?

com.lowagie.text.Chunk
- added key CHAR_SPACING with relative getter and setter

com.lowagie.text.pdf.PdfChunk
- added attribute key CHAR_SPACING
- modified in several lines the code font.width(int) with getCharWidth(int)
- modified method width() to handle char spacing
- modified method getCharWidth(int) to handle char spacing

com.lowagie.text.pdf.PdfDocument (method writeLineToContent)
- added char spacing handling to PdfContentByte
- when justified it takes care of char spacing too
- resetted the char spacing attribute


TestCharSpacing.java
package test;

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class TestCharSpacing {
/**
* @param args
*/
public static void main(String[] args) {
try {
Document document = new Document(PageSize.A4, 40, 40, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TestCharSpacing.pdf"));
document.open();

// 1# CHUNK WITH DIFFERENT CHAR SPACING INSIDE NOT JUSTIFIED PARAGRAPH
Chunk c1 = new Chunk("AAA");
Chunk c2 = new Chunk("BBB");
c2.setCharacterSpacing(10f);
Chunk c3 = new Chunk("CCC");
c3.setCharacterSpacing(20f);
Paragraph parag = new Paragraph();
for (int i = 0; i < 10; i++) {
parag.add(c1);
parag.add(c2);
parag.add(c3);
}
document.add(parag);

// 2# AS ABOVE BUT WITH JUSTIFIED PARAGRAPH
parag = new Paragraph();
parag.setAlignment(Paragraph.ALIGN_JUSTIFIED);
for (int i = 0; i < 10; i++) {
parag.add(c1);
parag.add(c2);
parag.add(c3);
}
document.add(parag);

// 3# AS 1# WITH DIFFERENT TEXT
String shortText = " hello world ";
String longText = "this is not a short string this is not a short string this is not a short string this is not a short string this is not a short string this is not a short string";
c1 = new Chunk(longText);
c2 = new Chunk(shortText);
c2.setCharacterSpacing(20f);
c3 = new Chunk(longText);
c3.setCharacterSpacing(10f);
parag = new Paragraph();
for (int i = 0; i < 2; i++) {
parag.add(c1);
parag.add(c2);
parag.add(c3);
}
document.add(parag);

// 4# AS 2# WITH DIFFERENT TEXT
parag = new Paragraph();
parag.setAlignment(Paragraph.ALIGN_JUSTIFIED);
for (int i = 0; i < 2; i++) {
parag.add(c1);
parag.add(c2);
parag.add(c3);
}
document.add(parag);

// 5# AS 4# USING TABLE
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
for (int j = 6; j < 20; j++) {
for (int i = 0; i < 2; i++) {
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
cell.setLeading(j, -0.4f);
cell.setPadding(0);

c1 = new Chunk(longText);
c1.setFont(new Font(Font.COURIER, j));
c2 = new Chunk(shortText);
c2.setCharacterSpacing(20f);
c2.setFont(new Font(Font.COURIER, j));
c3 = new Chunk(longText);
c3.setCharacterSpacing(10f);
c3.setFont(new Font(Font.COURIER, j));
parag = new Paragraph();
parag.setAlignment(Paragraph.ALIGN_JUSTIFIED);
for (int k = 0; k < 2; k++) {
parag.add(c1);
parag.add(c2);
parag.add(c3);
}
cell.setPhrase(parag);

table.addCell(cell);
}
}
document.add(table);

document.close();

} catch (Exception e) {
e.printStackTrace();
}

System.out.println("done");
}
}

0 件のコメント:

コメントを投稿