Hi All,

Apologies for popping through in the middle of the
discussion. 

Perhaps I can provide some additional info since I
am trying to generate PDF and RTF document from the
same template. I believe this is similar with what
Dhruba trying to accomplish.

I've written a small Junit test which generates the
following:
1. Complex table structure in RTF. Please refer to
test.rtf attached with email.
2. Complex table structure in PDF using Table
object.
Please refer to test.pdf attached with email.
3. Complex table structure in PDF using PdfPTable
and
PdfPCell. Please refer to test-nested.pdf attached
with email.

The JUnit test is stand alone, needing only itext &
junit jar on the classpath. The source code of the
JUnit is attached with email.

One interesting thing to note is when I ran this
unit test against itext 2.0.4, I do got the
ArrayIndexOutOfBounds exception. After building
latest build from SVN trunk, this problem goes away. I
can confirm the latest fix placed in by Bruno does
make
this error go away.

However, the format generated in the pdf is quite
different compared to when its generated into rtf
(please compare test.rtf and test.pdf).  Is it
possible to have the same formatting between rtf and
pdf using the same sets of Table and Cells, or we
need to use Table and Cells for rtf and PDF objects
for
pdf output?

Thanks in advance for your time,

Regards,

Felix

PS: I have to resend this email to itext mailing list
since the email was rejected because I attached the
java source in a zip file.



        
        
                
___________________________________________________________ 
New Yahoo! Mail is the ultimate force in competitive emailing. Find out more at 
the Yahoo! Mail Championships. Plus: play games and win prizes. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 

Attachment: test.pdf
Description: pat892128708

Attachment: test.rtf
Description: pat654306152

Attachment: test-nested.pdf
Description: pat1103879969

package test;

import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import junit.framework.TestCase;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

public class TestGenerateDocs extends TestCase {
        private File baseDir;
        private String baseDirPath;
        
        @Override
        protected void setUp() throws Exception {
                this.baseDir = new File("");
                this.baseDirPath = this.baseDir.getAbsolutePath();
                
                FontFactory.registerDirectories();
        }

        public void testGenerateRtf() throws Exception {
                String outputFile = this.baseDirPath + "/test.rtf";
                (new File(outputFile)).delete();
                
                OutputStream out = new BufferedOutputStream(new 
FileOutputStream(outputFile));
                
                Document document = new Document(PageSize.A4, 50.0f, 50.0f, 
50.0f, 50.0f);
                
                RtfWriter2.getInstance(document, out);
                
                this.generateDoc(document);
                        
                out.flush();
                out.close();
        }

        public void testGeneratePdf() throws Exception {
                String outputFile = this.baseDirPath + "/test.pdf";
                (new File(outputFile)).delete();
                
                OutputStream out = new BufferedOutputStream(new 
FileOutputStream(outputFile));
                
                Document document = new Document(PageSize.A4, 50.0f, 50.0f, 
50.0f, 50.0f);
                
                PdfWriter.getInstance(document, out);
                
                this.generateDoc(document);
                        
                out.flush();
                out.close();
        }
        
        public void testGeneratePdfNestedTable() throws Exception {
                String pdfOutputFile = this.baseDirPath + "/test-nested.pdf";
                (new File(pdfOutputFile)).delete();
                
                OutputStream out2 = new BufferedOutputStream(new 
FileOutputStream(pdfOutputFile));
                
                Document document = new Document(PageSize.A4, 50.0f, 50.0f, 
50.0f, 50.0f);
                
                PdfWriter.getInstance(document, out2);
                
                this.generatePdfWithNestedTable(document);
                
                out2.flush();
                out2.close();
        }
        
        protected void generateDoc(Document document) throws Exception {
                document.open();
                
                Font bold = FontFactory.getFont("Verdana", 10, Font.BOLD); 
                Font normal = FontFactory.getFont("Verdana", 9, Font.NORMAL);
                Color lightGray = new Color(0xee, 0xee, 0xee);
                
                Table table = new Table(5);
                table.setWidth(100.0f);
                table.setWidths(new int[]{20,5,15,45,15});
                table.setBorder(Table.NO_BORDER);
                table.getDefaultLayout().setBorder(Cell.BOX);
                table.getDefaultLayout().setBorderColor(lightGray);
                table.getDefaultLayout().setVerticalAlignment(Cell.ALIGN_TOP);
                table.setPadding(2.0f);
                
                Paragraph titlePara = new Paragraph();
                titlePara.setSpacingBefore(0.0f);
                titlePara.add(new Chunk("Title here", bold));
                Cell titleCell = new Cell();
                titleCell.setVerticalAlignment(Cell.ALIGN_TOP);
                titleCell.setBackgroundColor(lightGray);
                titleCell.addElement(titlePara);
                titleCell.setRowspan(3);
                table.addCell(titleCell);
                
                Cell paddingTitle = new Cell();
                paddingTitle.setRowspan(3);
                table.addCell(paddingTitle);
                
                Cell row1Col3 = new Cell();
                row1Col3.addElement(new Chunk("1.3", normal));
                table.addCell(row1Col3);

                Cell row1Col4 = new Cell();
                row1Col4.addElement(new Chunk("1.4", normal));
                table.addCell(row1Col4);

                Cell row1Col5 = new Cell();
                row1Col5.addElement(new Chunk("1.5", normal));
                table.addCell(row1Col5);

                Cell row2Col3To5 = new Cell();
                row2Col3To5.addElement(new Chunk("2.3 - 2.5", normal));
                row2Col3To5.setColspan(3);
                table.addCell(row2Col3To5);
                                
                Cell row3Col3To5 = new Cell();
                row3Col3To5.addElement(new Chunk("3.3 - 3.5", normal));
                row3Col3To5.setColspan(3);
                row3Col3To5.setRowspan(2);
                table.addCell(row3Col3To5);
                
                Cell paddingTitleRemainder = new Cell();
                paddingTitleRemainder.setRowspan(5);
                table.addCell(paddingTitleRemainder);

                Cell paddingRemainder = new Cell();
                paddingRemainder.setRowspan(5);
                table.addCell(paddingRemainder);

                
                Cell row4Col3 = new Cell();
                row4Col3.addElement(new Chunk("4.3", normal));
                table.addCell(row4Col3);

                Cell row4Col4 = new Cell();
                row4Col4.addElement(new Chunk("4.4", normal));
                table.addCell(row4Col4);

                Cell row4Col5 = new Cell();
                row4Col5.addElement(new Chunk("4.5", normal));
                table.addCell(row4Col5);

                Cell row5Col3To5 = new Cell();
                row5Col3To5.addElement(new Chunk("5.3 - 5.5", normal));
                row5Col3To5.setColspan(3);
                table.addCell(row5Col3To5);
                                
                Cell row6Col3To5 = new Cell();
                row6Col3To5.addElement(new Chunk("6.3 - 6.5", normal));
                row6Col3To5.setColspan(3);
                row6Col3To5.setRowspan(2);
                table.addCell(row6Col3To5);
                
                document.add(table);
                document.close();
        }
        
        protected void generatePdfWithNestedTable(Document document) throws 
Exception {
                document.open();
                
                Font bold = FontFactory.getFont("Verdana", 10, Font.BOLD); 
                Font normal = FontFactory.getFont("Verdana", 9, Font.NORMAL);
                Color lightGray = new Color(0xee, 0xee, 0xee);
                
                PdfPTable table = new PdfPTable(2);
                table.setWidths(new int[]{25,75});
                table.getDefaultCell().setBorder(Cell.BOX);
                table.getDefaultCell().setBorderColor(lightGray);
                table.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP);
                table.getDefaultCell().setPadding(0.0f);
                
                PdfPTable titleTable = new PdfPTable(2);
                titleTable.setWidths(new int[]{80,20});
                titleTable.getDefaultCell().setBorder(Cell.BOX);
                titleTable.getDefaultCell().setBorderColor(lightGray);
                
titleTable.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP);
                titleTable.getDefaultCell().setPadding(2.0f);

                
titleTable.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP);

                Paragraph titlePara = new Paragraph();
                titlePara.setSpacingBefore(0.0f);
                titlePara.add(new Chunk("Title here", bold));
                PdfPCell titleCell = new PdfPCell(titlePara);
                titleCell.setBackgroundColor(lightGray);
                titleCell.setBorder(Cell.BOX);
                titleCell.setBorderColor(lightGray);
                titleCell.setFixedHeight(45.0f);
                titleTable.addCell(titleCell);
                
                PdfPCell paddingTitleCell = new PdfPCell();
                paddingTitleCell.setFixedHeight(30.0f);
                paddingTitleCell.setBorder(Cell.BOX);
                paddingTitleCell.setBorderColor(lightGray);             
                titleTable.addCell(paddingTitleCell);                           
                table.addCell(titleTable);
                
                PdfPCell paddingExtra = new PdfPCell();
                paddingExtra.setBorder(Cell.BOX);
                paddingExtra.setBorderColor(lightGray);
                
                titleTable.addCell(paddingExtra);
                titleTable.addCell(paddingExtra);
                
                PdfPTable descTable = new PdfPTable(3);
                descTable.setWidths(new int[]{15,70,15});
                descTable.getDefaultCell().setBorder(Cell.BOX);
                descTable.getDefaultCell().setBorderColor(lightGray);
                descTable.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP);
                descTable.getDefaultCell().setPadding(2.0f);
                
                PdfPCell row1Col3 = new PdfPCell(new Paragraph("1.3", normal));
                row1Col3.setBorder(Cell.BOX);
                row1Col3.setBorderColor(lightGray);             
                descTable.addCell(row1Col3);

                PdfPCell row1Col4 = new PdfPCell(new Paragraph("1.4", normal));
                row1Col4.setBorder(Cell.BOX);
                row1Col4.setBorderColor(lightGray);                             
                descTable.addCell(row1Col4);

                PdfPCell row1Col5 = new PdfPCell(new Paragraph("1.5", normal));
                row1Col5.setBorder(Cell.BOX);
                row1Col5.setBorderColor(lightGray);                             
                
                descTable.addCell(row1Col5);

                PdfPCell row2Col3To5 = new PdfPCell(new Paragraph("2.3 - 2.5", 
normal));
                row2Col3To5.setBorder(Cell.BOX);
                row2Col3To5.setBorderColor(lightGray);                          
                                
                row2Col3To5.setColspan(3);
                descTable.addCell(row2Col3To5);

                PdfPCell row3Col3To5 = new PdfPCell(new Paragraph("3.3 - 
3.5\n\n", normal));
                row3Col3To5.setBorder(Cell.BOX);
                row3Col3To5.setBorderColor(lightGray);                          
                                
                row3Col3To5.setColspan(3);
                descTable.addCell(row3Col3To5);

                PdfPCell row4Col3 = new PdfPCell(new Paragraph("4.3", normal));
                row4Col3.setBorder(Cell.BOX);
                row4Col3.setBorderColor(lightGray);                             
                                                
                descTable.addCell(row4Col3);

                PdfPCell row4Col4 = new PdfPCell(new Paragraph("4.4", normal));
                row4Col4.setBorder(Cell.BOX);
                row4Col4.setBorderColor(lightGray);                             
                                                                
                descTable.addCell(row4Col4);

                PdfPCell row4Col5 = new PdfPCell(new Paragraph("4.5", normal));
                row4Col5.setBorder(Cell.BOX);
                row4Col5.setBorderColor(lightGray);                             
                                                                
                descTable.addCell(row4Col5);

                PdfPCell row5Col3To5 = new PdfPCell(new Paragraph("5.3 - 5.5", 
normal));
                row5Col3To5.setBorder(Cell.BOX);
                row5Col3To5.setBorderColor(lightGray);                          
                                                                
                row5Col3To5.setColspan(3);
                descTable.addCell(row5Col3To5);

                PdfPCell row6Col3To5 = new PdfPCell(new Paragraph("6.3 - 
6.5\n\n", normal));
                row6Col3To5.setBorder(Cell.BOX);
                row6Col3To5.setBorderColor(lightGray);                          
                                                                
                row6Col3To5.setColspan(3);
                descTable.addCell(row6Col3To5);
                
                table.addCell(descTable);
                
                document.add(table);
                document.close();
        }
        
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to