jar 包 :core-renderer.jar iText-2.0.8.jar iTextAsian.jar
方式1:
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.pdf.BaseFont; public class pdf { public static void main(String[] args) throws FileNotFoundException { List<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { list.add("test"); } String outputFile = "e:/firstdoc.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); StringBuffer html = new StringBuffer(); //组装成符合W3C标准的html文件,否则不能正确解析 html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">") .append("<head>") .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />") .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: Arial Unicode MS;}</style>") .append("<style type=\"text/css\">img {width: 500px;}</style>") .append("<style type=\"text/css\">table {font-size:13px;}</style>") .append("</head>") .append("<body>"); html.append("<center>"); html.append("<div>报表测试</div>"); for(int i=0;i<list.size();i++) { html.append("<div>" + list.get(i) + "</div>"); } html.append("</center>"); html.append("</body></html>"); try { renderer.setDocumentFromString(html.toString()); // ITextFontResolver fontResolver = renderer.getFontResolver(); // fontResolver.addFont("e:/fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解决图片的相对路径问题,图片路径必须以file开头 // renderer.getSharedContext().setBaseURL("file:/" + rootpath); renderer.layout(); renderer.createPDF(os); os.close(); System.out.println("ok"); } catch (Exception e) { e.printStackTrace(); } } }
方式2
import java.awt.Color; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class pdf2 { public static void main(String[] args) throws FileNotFoundException, Exception { List<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { list.add("test"); } String outputFile = "e:/firstdoc.pdf"; OutputStream ops = new FileOutputStream(outputFile); Document document = new Document(); PdfWriter.getInstance(document, ops); document.open(); // set chinese font BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font f2 = new Font(bfChinese, 2, Font.NORMAL); Font f6 = new Font(bfChinese, 6, Font.NORMAL); Font f8 = new Font(bfChinese, 8, Font.NORMAL); Font f10 = new Font(bfChinese, 10, Font.NORMAL); Font f12 = new Font(bfChinese, 12, Font.BOLD); // int rowNumer = 0; // PdfPTable table = new PdfPTable(2); // table.setWidthPercentage(80); // table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER); // PdfPCell titleCell = new PdfPCell(); // titleCell.setBackgroundColor(new Color(213, 141, 69)); // titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // titleCell.setPhrase(new Paragraph("人口指标", f8)); // table.addCell(titleCell); // titleCell.setPhrase(new Paragraph("人口指标2", f8)); // table.addCell(titleCell); // document.add(table); PdfPTable table = new PdfPTable(3); PdfPCell cell; cell = new PdfPCell(new Phrase("Cell with colspan 3")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setColspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); document.add(table); document.close(); ops.close(); System.out.println("ok"); } }
时间: 2024-08-11 05:31:52