It's a bug. It works if the destination is a PdfContentByte but not if it's
a PdfTemplate. The fix is in PdfGrahics2D:
public Graphics create() {
PdfGraphics2D g2 = new PdfGraphics2D();
g2.onlyShapes = this.onlyShapes;
g2.transform = new AffineTransform(this.transform);
g2.baseFonts = this.baseFonts;
g2.fontMapper = this.fontMapper;
g2.kids = this.kids;
g2.paint = this.paint;
g2.background = this.background;
g2.setFont(this.font);
g2.stroke = this.stroke;
g2.cb = this.cb.getDuplicate();
g2.cb.saveState();
g2.width = this.width;
g2.height = this.height;
g2.clip = new Area(new Rectangle2D.Float(0, 0, width, height));
g2.clip(g2.clip);
g2.cb.saveState();
g2.followPath(this.clip, CLIP);
g2.kid = true;
synchronized (kids) {
kids.add(g2);
}
return g2;
}
Best Regards,
Paulo Soares
> -----Original Message-----
> From: Lars Sitzmann [SMTP:[EMAIL PROTECTED]]
> Sent: Thursday, September 26, 2002 9:03
> To: [EMAIL PROTECTED]
> Subject: [iText-questions] Trouble painting Java-Component into
> PDF-Graphics2d
>
> 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
-------------------------------------------------------
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