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 { Listlist = new ArrayList (); 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(""); html.append("") .append("") .append(" ") .append(" ") .append(" ") .append(" ") .append("") .append(""); html.append(" "); html.append(" "); html.append(""); 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(); } }}报表测试"); for(int i=0;i" + list.get(i) + ""); } html.append("
方式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 { Listlist = new ArrayList (); 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"); }}