用itext生成PDF时画的字符串和用Java画的不一样长(字体一样), 怎么解决?(急!!)
比较一下java画的String和itext画的String,不一样长,好象是字间距不一样,为什么呢,在java的字体中怎么调整kerning,使得两者画的一样长(调整java)
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class testDebug extends JFrame {
public void paint(Graphics g2)
{
Graphics2D g = (Graphics2D)g2;
String s = "herzlichen Dank f Ihren telefonischen Auftrag vom 25.10.2004, den wir unter Hinweis auf unse";
try {
Font f =
Font.createFont(
Font.TRUETYPE_FONT,
new FileInputStream("C:\\WINNT\\Fonts\\arial.TTF"));
f = f.deriveFont(Font.PLAIN, 40);
BaseFont bf =
BaseFont.createFont(
"C:\\WINNT\\Fonts\\arial.TTF",
BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
g.setColor(Color.GRAY);
g.fillRect(200, 80, 456, 20);
g.setFont(f);
g.setColor(Color.BLACK);
g.drawString(s,200,100);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test1.pdf"));
document.open();
PdfContentByte contentBytes = writer.getDirectContent();
contentBytes.beginText();
//如果你系统的DPI是72,别那么Size就用40, 是96就用30
contentBytes.setFontAndSize(bf, 30);
contentBytes.setTextMatrix(0, 600);
contentBytes.showText(s);
contentBytes.endText();
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws DocumentException, IOException {
testDebug t = new testDebug();
t.setSize(300,300);
t.show();
}
}