在Java itext表格单元格中导出HTML格式的数据,而不会丢失格式

时间:2013-12-03 09:26:25

标签: java itext

FileOutputStream out = new FileOutputStream(new File("./WebContent/WEB-INF/Data/test.pdf"));

Document document = new Document();

PdfWriter.getInstance(document, out);

PdfPTable table = new PdfPTable(3);

PdfPCell cell = new PdfPCell(new Paragraph ("Java4s.com"));
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(10.0f);
cell.setBackgroundColor(new BaseColor(0, 255, 48));                                   

table.addCell(cell);           
document.open();

String k = "<html><body> <b>This is my Project </b></body></html>";

PdfPCell cell1 = new PdfPCell (new Paragraph ("Java4s.com"));

HTMLWorker htmlWorker = new HTMLWorker(document);

htmlWorker.parse(new StringReader(k));

cell1.setColspan(3);
table.addCell(cell1);     
table.setSpacingBefore(30.0f);       
table.setSpacingAfter(30.0f); 

0 个答案:

没有答案