package monentreprise.monproduit; import java.awt.Color; import java.io.FileOutputStream; import java.io.OutputStream; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfPageEventHelper; import com.lowagie.text.pdf.PdfWriter; public class TestGeneration { public static void main(String[] args) { OutputStream output = null ; Document document = null; try { Rectangle pageSize = PageSize.A4.rotate() ; String outputPath = "D:/workspace/tests/Generateur de fiches/wwwcontent/output/tst@ace/vacation1/_test.pdf" ; output = new FileOutputStream(outputPath, false) ; document = new Document() ; PdfWriter pdfWriter = PdfWriter.getInstance(document, output); pdfWriter.setPageEvent( new PdfPageEventHelper(){ public void onCloseDocument(PdfWriter writer, Document document) { try { Image watermark = Image.getInstance("D:/workspace/tests/Generateur de fiches/wwwcontent/input/tst@ace/static/exemple.png"); watermark.setAbsolutePosition(200, 200) ; writer.getDirectContentUnder().addImage(watermark); } catch(Exception e) { e.printStackTrace(System.err) ; } } }); document.setPageSize(pageSize) ; document.open(); document.setMargins(42.5f, 42.519684f, 28.346457f, 28.346457f) ; PdfPTable table = generatePage() ; document.add(table); } 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){} } } } private static PdfPTable generatePage() { // structure PdfPTable structure = new PdfPTable(new float[]{0.5f,0.5f}) ; structure.setWidthPercentage(100); structure.setSplitLate(false); structure.setExtendLastRow(true); // content structure.addCell( new PdfPCell(column1()) ); structure.addCell( new PdfPCell(cell21()) ); // fin return structure ; } private static PdfPTable column1() { // structure PdfPTable structure = new PdfPTable(new float[]{1}) ; // content structure.addCell( new PdfPCell(cell11()) ); structure.addCell( new PdfPCell(cell12()) ); // fin return structure ; } private static PdfPTable cell11() { // structure PdfPTable structure = new PdfPTable(new float[]{1}) ; // content PdfPCell structureContent = new PdfPCell(new Paragraph("cellule 1.1")) ; structureContent.setFixedHeight(cmToPoint(10)) ; // assemblage structureContent.setBorderWidth(1f) ; structureContent.setBorderColor(Color.BLUE) ; structureContent.setPadding(0f) ; structureContent.setVerticalAlignment(Element.ALIGN_MIDDLE) ; structureContent.setHorizontalAlignment(Element.ALIGN_CENTER) ; structure.addCell(structureContent) ; // fin return structure ; } private static PdfPTable cell21() { // structure PdfPTable structure = new PdfPTable(new float[]{1}) ; // content PdfPCell structureContent = new PdfPCell(new Paragraph("cellule 2.1")) ; // assemblage structureContent.setBorderWidth(1f) ; structureContent.setBorderColor(Color.RED) ; structureContent.setPadding(0f) ; structureContent.setVerticalAlignment(Element.ALIGN_MIDDLE) ; structureContent.setHorizontalAlignment(Element.ALIGN_CENTER) ; structure.addCell(structureContent) ; // fin return structure ; } private static PdfPTable cell12() { // structure PdfPTable structure = new PdfPTable(new float[]{1}) ; //structure.setExtendLastRow(true); // content PdfPCell structureContent = new PdfPCell(new Paragraph("cellule 1.1")) ; // assemblage structureContent.setBorderWidth(1f) ; structureContent.setBorderColor(Color.YELLOW) ; structureContent.setPadding(0f) ; structureContent.setVerticalAlignment(Element.ALIGN_MIDDLE) ; structureContent.setHorizontalAlignment(Element.ALIGN_CENTER) ; structure.addCell(structureContent) ; // fin return structure ; } private static float cmToPoint(float size) { return size * 72 / 2.54f ; } }