Hello,

I still have trouble printing (or painting) any Swing-Component
into an IText-Graphics2d!

Below you find a simple "main" that creates a Graphics2D
from an IText-Template and paints a simple JLabel into this
graphics.
The result is a corrupt PDF-Document that Acrobat-Reader
does not open.
The stdout-output of my program shows what the JLabel does
on the graphics:

        Graphics(0-1) Enabling debug
        Graphics(0-1) Setting color: java.awt.Color[r=0,g=0,b=0]
        Graphics(0-1) Setting font: 
java.awt.Font[family=Arial,name=Arial,style=plain,size=18]
        Graphics(2-1) Setting color: java.awt.Color[r=0,g=0,b=0]
        Graphics(2-1) Drawing string: "Hello" at: java.awt.Point[x=241,y=389]

If I do the same myself on the graphics, I get a fine PDF-document!
(see "main2" below).

So can anybody tell my, what the Component (here JLabel, but I've got the same
problem with any Swing-Component!) does on the graphics and what to do to get
an intact PDF-file???

Thanks a lot for any advice!

Lars

*************** main - program creates corrupt PDF ************************

  public static void main(String args[]){
    try {
      // Initializing my PDF-Graphics2d
      // mostly copy and paste from "Graphics2d-Example"
      Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream("c:\\TEST.pdf"));
      document.open();
      DefaultFontMapper mapper = new DefaultFontMapper();
      mapper.insertDirectory("c:\\winnt\\fonts");
      int w = 523; // i want the whole page!
      int h = 764;
      int x = 36;
      int y = 72;
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(w, h);
      tp.setWidth(w);
      tp.setHeight(h);
      Graphics2D g2d = tp.createGraphics(w, h, mapper);

      // now create a simple component.
      // a JLabel with the black text Hello in Arial
      JLabel test = new JLabel("Hello");
      test.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 18));
      test.setBackground(Color.white);
      test.setForeground(Color.black);
      test.setHorizontalAlignment(SwingConstants.CENTER);
      test.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION); // debug the 
graphics-output
      test.setSize( w, h);
      test.validate();

      // Now paint my component into the PDF-Graphics!
      test.paint(g2d);

      // Finish the PDF-Document
      g2d.dispose();
      cb.addTemplate(tp,x,y);
      document.close();
    } catch (Exception e){
      e.printStackTrace();
    }
  }
****************************************************************************

**************** main2 - program creates fine PDF **************************

  public static void main(String args[]){
    try {
      // Initializing my PDF-Graphics2d
      // mostly copy and paste from "Graphics2d-Example"
      Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream("c:\\TEST.pdf"));
      document.open();
      DefaultFontMapper mapper = new DefaultFontMapper();
      mapper.insertDirectory("c:\\winnt\\fonts");
      int w = 523; // i want the whole page!
      int h = 764;
      int x = 36;
      int y = 72;
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(w, h);
      tp.setWidth(w);
      tp.setHeight(h);
      Graphics2D g2d = tp.createGraphics(w, h, mapper);

      g2d.setColor(new Color(0,0,0));
      g2d.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 18));
      g2d.setColor(new Color(0,0,0));
      g2d.drawString("Hello",241,389);

      // Finish the PDF-Document
      g2d.dispose();
      cb.addTemplate(tp,x,y);
      document.close();
    } catch (Exception e){
      e.printStackTrace();
    }
  }
****************************************************************************

_______________________________________
OSC-Information Management AG
Dipl.-Inform. Lars Sitzmann
Leiter Competence Center GIS
Industriestra�e 11, 26121 Oldenburg
Tel.: 04 41/3 50 42-3 04, Fax: -3 80, Zentrale -0
E-Mail: [EMAIL PROTECTED]
URL: http://www.osc-im.de
     http://www.InterGIS.de



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to