hello, I want to use iText for my application to print some rich text from a JTextPane with varying textstyles under Sun JRE 1.4.x / 1.5.x.
But unfortunately I run in a quality-issue as shown in following example. If you look at the screen-output, there is no gap between the text and the surrounding rectangele calculated from FontMetric. But if you look at the pdf-output you see big gap. It seems that the FontMetric from the com.lowagie.text.pdf.PdfGraphics2D dosen't calculate the correct fontlength. //----------------------- SNIP Test.java-------------------------------- import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import com.lowagie.text.Document; import com.lowagie.text.FontFactory; import com.lowagie.text.pdf.DefaultFontMapper; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; public class Test { public Test() { JFrame frame; Container contentpane; JPanel panel; frame = new JFrame("Test"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); contentpane = frame.getContentPane(); contentpane.setLayout(new BorderLayout()); //-------------------- PDF start --------------------------- // step 1: creation of a document-object Document document = new Document(); try { // step 2: creation of the writer java.util.Date date = new java.util.Date(); File dir = new File("."); File tmpFile = File.createTempFile("iText", ".pdf",dir); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tmpFile)); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it // we create a fontMapper and read all the fonts in the font directory DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\Winnt\\fonts"); // we create a template and a Graphics2D object that corresponds with it int w = 650; int h = 850; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h, mapper); tp.setWidth(w); tp.setHeight(h); g2.setColor(Color.black); new MyPanel().paint(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); g2.dispose(); } catch (Exception ex){ ex.printStackTrace(); } document.close(); //-------------------- PDF END --------------------------- contentpane.add(new MyPanel(), BorderLayout.CENTER); frame.pack(); frame.setSize(900,500); frame.show(); } public static void main(String strArg[]) { new Test(); } private class MyPanel extends JPanel{ public MyPanel() { super(); } public void paint(Graphics g) { FontMetrics fm; Font font; String strText; Graphics2D g2; AffineTransform at; int iW; int iAsc; int iDesc; strText = "some text fitting in the rectangle"; super.paint(g); g2 = (Graphics2D) g; at = new AffineTransform(); at.setToScale(1.1,1.1); int yPos = 20; int leftIndent = 2; for (int i = 1; i<20 ; i++){ System.out.println(g.getClass().getName()); font = new Font("Arial", Font.BOLD, 10); g.setFont(font); g.drawString(strText, leftIndent,yPos); fm = g.getFontMetrics(font); iW = fm.stringWidth(strText); iAsc = fm.getMaxAscent(); iDesc = fm.getMaxDescent(); g.drawRect(leftIndent,yPos-iAsc,iW,iAsc+iDesc); font = new Font("Arial", Font.PLAIN, 10); fm = g.getFontMetrics(font); g.setFont(font); g.drawString(strText, leftIndent+iW,yPos); g.drawRect(leftIndent+iW,yPos-iAsc,fm.stringWidth(strText),iAsc+iDesc); // Transform for next Run g2.transform(at); yPos = yPos + 10; } } } } //----------------------- SNAP-------------------------------- Is there something wrong in my code or is there any workaround? May anybody help me? kind regards, Mark ------------------------------------------------------- This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual core and dual graphics technology at this free one hour event hosted by HP, AMD, and NVIDIA. To register visit http://www.hp.com/go/dualwebinar _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions