Paulo Soares wrote:

Post code that can be run standalone.

Paulo
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Antonio Gurisatti
Sent: Friday, May 19, 2006 5:25 AM
To: iText
Subject: [iText-questions] New paragraph is appending data from previous one.

I'm brand new using iText and I'm trying to build some reports in an Eclipse RCP application.

The report is very simple. It reads some records from a database and prints them.

This is the source:

package com.maguri.derbypad.reports;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.maguri.derbypad.db.sql.NavigatorSql;
import com.maguri.utilities.DateUtilities;

/**
* @author agurisatti
*
*/
public class ObjectInventoryReport {

   Font tit1 = new Font(Font.HELVETICA, 12, Font.BOLD);

   Font tit2 = new Font(Font.HELVETICA, 9, Font.BOLD);

   Font tit3 = new Font(Font.HELVETICA, 7, Font.NORMAL);

   Font paragraph1 = new Font(Font.HELVETICA, 8, Font.NORMAL);

   protected Document document;

   protected Paragraph title1;

   protected Paragraph title2;

   protected Paragraph title3;

   protected Paragraph emptyline = new Paragraph(" ");

   protected Paragraph pschema;

   protected Chunk schemaLabel = new Chunk(" Schema: ", tit2);

   protected Chunk schemaName;

   /**
    *
    */
   public ObjectInventoryReport() {
       initialize();
   }

   private void initialize() {
       document = new Document();
       try {
           PdfWriter.getInstance(document, new FileOutputStream(
                   "D:/Temporal/HelloWorld.pdf"));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (DocumentException e) {
           e.printStackTrace();
       }
       document.setPageSize(PageSize.LETTER);
       document.addTitle("Object Inventory");
       document.addSubject("The inventory of database objects");
       document.addAuthor("maguriWorks");
       document.addCreator("DerbyPad 1.0");

       // Left, right, top, bottom in points (72 x inch)
       document.setMargins(72, 40, 72, 36);
       configureTitles();
   }

   private void configureTitles() {
       title1 = new Paragraph("DerbyPad 1.0", tit3);
       title2 = new Paragraph(
"Generated on " + DateUtilities.getCurrentDate(), tit3);
       title3 = new Paragraph("Object Inventory", tit1);
   }

   public void generateDocument() {
       document.open();
       printTitles();
       ResultSet rs = NavigatorSql.getSchemas("%");
       try {
           while (rs.next()) {
               printSchemaTitle(rs.getString(1));
           }
       } catch (SQLException e) {
           e.printStackTrace();
       } finally {
           if (rs != null) {
               try {
                   rs.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
       }
       document.close();
   }

   private void printTitles() {
       try {
           document.add(title1);
           document.add(title2);
           document.add(title3);
       } catch (DocumentException e) {
           e.printStackTrace();
       }
   }

   private void printSchemaTitle(String schName) {
       pschema = new Paragraph();
       Chunk ch = new Chunk(schName, tit2);
*        pschema.add(schemaLabel);
       pschema.add(ch);*
       try {
           document.add(emptyline);
           document.add(pschema);
       } catch (DocumentException e) {
           e.printStackTrace();
       }
   }
}

The result is quite odd:

DerbyPad 1.0
Generated on 2006-5-18 22:34:40
Object Inventory
Schema: APP
Schema: APPDERBYPAD
Schema: APPDERBYPADESQUEMA1
Schema: APPDERBYPADESQUEMA1ESQUEMA2
Schema: APPDERBYPADESQUEMA1ESQUEMA2ESQUEMA3
Schema: APPDERBYPADESQUEMA1ESQUEMA2ESQUEMA3LOADER_TESTS
Schema: APPDERBYPADESQUEMA1ESQUEMA2ESQUEMA3LOADER_TESTSMGCOMUN

As you can see eache new schema name is appended to the previous one.

The odd thing is that if a change the order of the two statements in bold,

instead of
*        pschema.add(schemaLabel);
       pschema.add(ch);*
use
*        pschema.add(ch);*
*        pschema.add(schemaLabel);

*then the result is:

DerbyPad 1.0
Generated on 2006-5-18 23:20:4
Object Inventory
APP Schema:
DERBYPAD Schema:
ESQUEMA1 Schema:
ESQUEMA2 Schema:
ESQUEMA3 Schema:
LOADER_TESTS Schema:
MGCOMUN Schema:
MGCONTABIL Schema:

I've spent quite a time trying to find the problem but unfortunatelly couldn't.

So the question is WHAT AM I DOING WRONG?

Thanks a lot for any help





-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&;
dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions



Aviso Legal:

Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.


Disclaimer:

This message is destined exclusively to the intended receiver. It may contain 
confidential or legally protected information. The incorrect transmission of 
this message does not mean the loss of its confidentiality. If this message is 
received by mistake, please send it back to the sender and delete it from your 
system immediately. It is forbidden to any person who is not the intended 
receiver to use, distribute or copy any part of this message.




-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

I'm attaching a tiny app showing the problem.

Thanks again

package prueba.itext;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.KeyStroke;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JDialog;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JButton;

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

public class ChunkTest1 extends JFrame {

        private JPanel jContentPane = null;

        private JMenuBar jJMenuBar = null;

        private JMenu fileMenu = null;

        private JMenu editMenu = null;

        private JMenu helpMenu = null;

        private JMenuItem exitMenuItem = null;

        private JMenuItem aboutMenuItem = null;

        private JMenuItem cutMenuItem = null;

        private JMenuItem copyMenuItem = null;

        private JMenuItem pasteMenuItem = null;

        private JMenuItem saveMenuItem = null;

        private JButton jButton = null;

        // Report variables
        Font tit1 = new Font(Font.HELVETICA, 12, Font.BOLD);

        Font tit2 = new Font(Font.HELVETICA, 9, Font.BOLD);

        Font tit3 = new Font(Font.HELVETICA, 7, Font.NORMAL);

        Font paragraph1 = new Font(Font.HELVETICA, 8, Font.NORMAL);

        protected Document document;

        protected Paragraph title1;

        protected Paragraph title2;

        protected Paragraph title3;

        protected Paragraph emptyline = new Paragraph(" ");
        
        protected Paragraph emptyline1 = new Paragraph(" ", tit3);

        protected Paragraph pschema;

        protected Chunk schemaLabel = new Chunk(" Schema: ", tit2);

        protected Chunk schemaName;
        
        // Main program
        public ChunkTest1() throws HeadlessException {
                super();
                // TODO Auto-generated constructor stub
                initialize();
        }

        public ChunkTest1(GraphicsConfiguration arg0) {
                super(arg0);
                // TODO Auto-generated constructor stub
                initialize();
        }

        public ChunkTest1(String arg0) throws HeadlessException {
                super(arg0);
                // TODO Auto-generated constructor stub
                initialize();
        }

        public ChunkTest1(String arg0, GraphicsConfiguration arg1) {
                super(arg0, arg1);
                // TODO Auto-generated constructor stub
                initialize();
        }

        /**
         * This method initializes jButton      
         *      
         * @return javax.swing.JButton  
         */
        private JButton getJButton() {
                if (jButton == null) {
                        jButton = new JButton();
                        jButton.setText("RunTest");
                        jButton.addActionListener(new 
java.awt.event.ActionListener() {
                                public void 
actionPerformed(java.awt.event.ActionEvent e) {
                                        generateDocument();
                                }
                        });
                }
                return jButton;
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                ChunkTest1 application = new ChunkTest1();
                application.setVisible(true);
        }

        /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setJMenuBar(getJJMenuBar());
                this.setSize(300, 200);
                this.setContentPane(getJContentPane());
                this.setTitle("Application");
                
                // Initialize document
                document = new Document();
                try {
                        PdfWriter.getInstance(document, new FileOutputStream(
                                        "D:/Temporal/HelloWorld.pdf"));
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
                document.setPageSize(PageSize.LETTER);
                document.addTitle("Object Inventory");
                document.addSubject("The inventory of database objects");
                document.addAuthor("maguriWorks");
                document.addCreator("DerbyPad 1.0");

                // Left, right, top, bottom in points (72 x inch)
                document.setMargins(72, 36, 72, 36);
                configureTitles();
                
        }

        private void configureTitles() {
                title1 = new Paragraph("DerbyPad 1.0", tit3);
                title2 = new Paragraph(
                                "Generated on " + new Date(), tit3);
                title3 = new Paragraph("Object Inventory", tit1);
        }
        
        // Generates the PDF document
        public void generateDocument() {
                document.open();
                printTitles();
                String schName = "";
                for (int i = 0; i < 10; i++) {
                        printSchemaTitle(" Schema_" + i);
                }
                document.close();
        }

        // Prints document titles
        private void printTitles() {
                try {
                        document.add(title1);
                        document.add(title2);
                        document.add(title3);
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }

        // Prints the schema subtitle
        private void printSchemaTitle(String schName) {
                pschema = new Paragraph();
                Chunk ch = new Chunk(schName, tit2);
                
                // If you swap the next two lines the problem disappears but,
                // obviously the report is wrong.
                pschema.add(schemaLabel);
                pschema.add(ch);
                
                // Swapped lines
//              pschema.add(ch);
//              pschema.add(schemaLabel);

                try {
                        document.add(emptyline);
                        document.add(pschema);
                        document.add(emptyline1);
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }
        
        /**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
        private JPanel getJContentPane() {
                if (jContentPane == null) {
                        jContentPane = new JPanel();
                        jContentPane.setLayout(new BorderLayout());
                        jContentPane.add(getJButton(), 
java.awt.BorderLayout.CENTER);
                }
                return jContentPane;
        }

        /**
         * This method initializes jJMenuBar    
         *      
         * @return javax.swing.JMenuBar 
         */
        private JMenuBar getJJMenuBar() {
                if (jJMenuBar == null) {
                        jJMenuBar = new JMenuBar();
                        jJMenuBar.add(getFileMenu());
                        jJMenuBar.add(getEditMenu());
                        jJMenuBar.add(getHelpMenu());
                }
                return jJMenuBar;
        }

        /**
         * This method initializes jMenu        
         *      
         * @return javax.swing.JMenu    
         */
        private JMenu getFileMenu() {
                if (fileMenu == null) {
                        fileMenu = new JMenu();
                        fileMenu.setText("File");
                        fileMenu.add(getSaveMenuItem());
                        fileMenu.add(getExitMenuItem());
                }
                return fileMenu;
        }

        /**
         * This method initializes jMenu        
         *      
         * @return javax.swing.JMenu    
         */
        private JMenu getEditMenu() {
                if (editMenu == null) {
                        editMenu = new JMenu();
                        editMenu.setText("Edit");
                        editMenu.add(getCutMenuItem());
                        editMenu.add(getCopyMenuItem());
                        editMenu.add(getPasteMenuItem());
                }
                return editMenu;
        }

        /**
         * This method initializes jMenu        
         *      
         * @return javax.swing.JMenu    
         */
        private JMenu getHelpMenu() {
                if (helpMenu == null) {
                        helpMenu = new JMenu();
                        helpMenu.setText("Help");
                        helpMenu.add(getAboutMenuItem());
                }
                return helpMenu;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getExitMenuItem() {
                if (exitMenuItem == null) {
                        exitMenuItem = new JMenuItem();
                        exitMenuItem.setText("Exit");
                        exitMenuItem.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                        System.exit(0);
                                }
                        });
                }
                return exitMenuItem;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getAboutMenuItem() {
                if (aboutMenuItem == null) {
                        aboutMenuItem = new JMenuItem();
                        aboutMenuItem.setText("About");
                        aboutMenuItem.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                        new JDialog(ChunkTest1.this, "About", 
true).show();
                                }
                        });
                }
                return aboutMenuItem;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getCutMenuItem() {
                if (cutMenuItem == null) {
                        cutMenuItem = new JMenuItem();
                        cutMenuItem.setText("Cut");
                        
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                                        Event.CTRL_MASK, true));
                }
                return cutMenuItem;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getCopyMenuItem() {
                if (copyMenuItem == null) {
                        copyMenuItem = new JMenuItem();
                        copyMenuItem.setText("Copy");
                        
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
                                        Event.CTRL_MASK, true));
                }
                return copyMenuItem;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getPasteMenuItem() {
                if (pasteMenuItem == null) {
                        pasteMenuItem = new JMenuItem();
                        pasteMenuItem.setText("Paste");
                        
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
                                        Event.CTRL_MASK, true));
                }
                return pasteMenuItem;
        }

        /**
         * This method initializes jMenuItem    
         *      
         * @return javax.swing.JMenuItem        
         */
        private JMenuItem getSaveMenuItem() {
                if (saveMenuItem == null) {
                        saveMenuItem = new JMenuItem();
                        saveMenuItem.setText("Save");
                        
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                                        Event.CTRL_MASK, true));
                }
                return saveMenuItem;
        }

}

Reply via email to