1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package itext.test;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class TestTable2 {
private static PdfPCell createCell(boolean top, boolean right, boolean bottom, boolean left, String string) {
PdfPCell cell = new PdfPCell(new Phrase(string)) ;
cell.setBorderWidth(0) ;
if(top) { cell.setBorderWidthTop(1) ; }
if(right) { cell.setBorderWidthRight(1) ; }
if(bottom) { cell.setBorderWidthBottom(1) ; }
if(left) { cell.setBorderWidthLeft(1) ; }
return cell;
}
private static PdfPCell createCell(boolean top, boolean right, boolean bottom, boolean left, String[] strings) {
PdfPCell cell = new PdfPCell() ;
for(String string : strings) {
cell.addElement(new Paragraph(string) ) ;
}
cell.setBorderWidth(0) ;
if(top) { cell.setBorderWidthTop(1) ; }
if(right) { cell.setBorderWidthRight(1) ; }
if(bottom) { cell.setBorderWidthBottom(1) ; }
if(left) { cell.setBorderWidthLeft(1) ; }
return cell;
}
private static PdfPCell createCell(PdfPTable table) {
PdfPCell cell = new PdfPCell(table) ;
cell.setBorderWidth(0) ;
cell.setPadding(5) ;
return cell;
}
public static void main(String[] args) {
OutputStream output = null ;
Document document = null ;
try {
// Initialisation
output = new FileOutputStream("c:/Test.pdf", false) ; //FIXME : mettre le chemin qui va bien !
document = new Document() ;
PdfWriter.getInstance(document, output);
document.open() ;
// Tableau 1 : une ligne par cellule
PdfPTable table1 = new PdfPTable(new float[]{1}) ;
table1.setWidthPercentage(100) ;
table1.addCell( createCell(true, true, false, true, "Lorem ipsum dolor sit amet") );
for(int index=0; index<3; index++) {
table1.addCell( createCell(false, true, false, true, "Lorem ipsum dolor sit amet") );
}
table1.addCell( createCell(false, true, true, true, "Lorem ipsum dolor sit amet") );
// Tableau 2 : 1 cellule avec un paragraphe multi-ligne dedans
PdfPTable table2 = new PdfPTable(new float[]{1}) ;
table2.setWidthPercentage(100) ;
table2.addCell( createCell(true, true, true, true, new String[]{"Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet"}) );
// Mise en page
PdfPTable content = new PdfPTable(new float[]{1}) ;
content.setWidthPercentage(100) ;
content.addCell(createCell(table1)) ;
content.addCell(createCell(table2)) ;
document.add(content) ;
} catch(Exception e) {
e.printStackTrace(System.err) ;
} finally {
if(document != null) { try{document.close() ;} catch(Exception ignore){} }
if(output != null) { try{output.close();} catch(Exception ignore){} }
}
}
} |