一个使用itext导出pdf的例子
来源:互联网 发布:首都国旅网络旗舰店 编辑:程序博客网 时间:2024/05/17 01:22
- Document doc = new Document (PageSize.A4);
- PdfWriter.getInstance (doc, new FileOutputStream ("c:/test/pdf/test.pdf"));
- doc.open ();
-
-
- BaseFont bfTitle = BaseFont.createFont("STSong-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font titleFont = new Font(bfTitle, 18, Font.NORMAL);
-
-
- BaseFont bfComic = BaseFont.createFont("STSong-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font font = new Font(bfComic, 9, Font.NORMAL);
-
- Paragraph titleP=new Paragraph("儿童信息 Child Information/n/n",titleFont);
- titleP.setAlignment(titleP.ALIGN_CENTER);
- doc.add(titleP);
-
- PdfPTable table = new PdfPTable (4);
- table.setWidthPercentage(100);
- table.setWidthPercentage(100);
- table.addCell (new Paragraph ("Children-id",font));
- PdfPCell cell = new PdfPCell (new Paragraph ("09140800002",font));
- cell.setColspan (3);
- table.addCell (cell);
-
- table.addCell (new Paragraph ("Name(CN)",font));
- table.addCell (new Paragraph ("党宁生",font));
- table.addCell (new Paragraph ("Name(EN)",font));
- table.addCell (new Paragraph ("DANG NING SHENG",font));
-
-
- table.addCell (new Paragraph ("Sex(CN)",font));
- table.addCell (new Paragraph ("男",font));
- table.addCell (new Paragraph ("Sex(EN)",font));
- table.addCell (new Paragraph ("MALE",font));
-
- table.addCell (new Paragraph ("Note",font));
- cell = new PdfPCell (new Paragraph ("儿童资料",font));
- cell.setColspan (3);
- table.addCell (cell);
-
-
- table.addCell (new Paragraph ("Pictures",font));
- Image photo=Image.getInstance("c:/test/pdf/1246588678828.jpg");
- cell = new PdfPCell (photo);
- cell.setColspan (3);
- table.addCell (cell);
-
- for(PdfPRow row:(ArrayList<PdfPRow>)table.getRows()){
- for(PdfPCell cells:row.getCells()){
- if(cells!=null){
- cells.setPadding(10.0f);
- }
- }
- }
-
- doc.add (table);
- doc.newPage();
-
-
- doc.newPage();
- Image image1 = Image.getInstance ("c:/test/pdf/1246588315281.jpg");
- image1.setAlignment(image1.ALIGN_CENTER);
- image1.scaleToFit( PageSize.A4.getHeight(),PageSize.A4.getWidth());
- doc.add (image1);
-
- doc.close ();