public class PdfSomainUtil { public static void main(String[] args) { String[] str1= {"沈阳有限公司1", "1234561", "1元/kg", "大米", "123456", "A12仓","500件","10000Kg","2000Kg","8000Kg","8000Kg","100","100","无意见1","d:/dossp/pdf/1.jpg"}; String[] str2 = {"虎林有限公司2", "1234562", "1元/kg", "大米", "虎123456", "A12仓","500件","10000Kg","2000Kg","8000Kg","8000Kg","100","100","无意见2","d:/dossp/pdf/2.jpg"}; String[] str3= {"合肥有限公司3", "1234563", "1元/kg", "大米", "皖123456", "A12仓","500件","10000Kg","2000Kg","8000Kg","8000Kg","100","100","无意见3","d:/dossp/pdf/111.jpg"}; String[] str4 = {"唐山有限公司4", "1234564", "1元/kg", "大米", "冀123456", "A12仓","500件","10000Kg","2000Kg","8000Kg","8000Kg","100","100","无意见4","d:/dossp/pdf/111.jpg"}; String[] str5 = {"富平有限公司5", "1234565", "1元/kg", "大米", "陕123456", "A12仓","500件","10000Kg","2000Kg","8000Kg","8000Kg","100","100","无意见5","d:/dossp/pdf/5.jpg"}; java.util.List list=new ArrayList(); list.add(str1); list.add(str2); list.add(str3); list.add(str4); list.add(str5); int count = list.size();// 总记录数 int pageCount = 4;// 每页记录数 int page = page=list.size()/4;// 总共页数 if(list.size()%4 != 0){ page=list.size()/4 +1; } File file = new File("d:/dossp/itext.pdf");//生成pdf名称及路径 Document document = new Document(PageSize.A4); BaseFont bf = null; Font fontChinese = null; Font fontChar = new Font(); fontChar.setSize(7); fontChar.setColor(BaseColor.DARK_GRAY); FileOutputStream out=null; try { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); fontChinese = new Font(bf, 10, Font.NORMAL);// 中文字体 fontChinese.setSize(10);//字体大小 out=new FileOutputStream(file); PdfWriter pdfWriter = PdfWriter.getInstance(document, out);// 文档输出流。 document.open(); int k=0; for (int item = 0; item < page; item++) {// 页数 document.newPage(); int times=pageCount; if((page-item)==1 && list.size()%4!=0){ times=list.size()-item*4; } for(int j=0;j<times;j++){ //每页循环次数 String[] model=(String[]) list.get(k); PdfPTable table = new PdfPTable(new float[]{23, 35, 23, 35, 80}); table.setTotalWidth(600); PdfPCell cell = new PdfPCell(new Phrase("单位:", fontChinese)); PdfPCell cell1 = new PdfPCell(new Phrase(model[0], fontChinese)); cell1.setColspan(3); table.addCell(cell); table.addCell(cell1); Image image = Image.getInstance(model[14]); image.scaleAbsolute(170f, 124f); image.setAlignment(Image.MIDDLE); PdfPCell cell_img = new PdfPCell(image); cell_img.setRowspan(9); table.addCell(cell_img); table.addCell(new Paragraph("合同号:", fontChinese)); table.addCell(new Paragraph(model[1], fontChinese)); table.addCell(new Paragraph("合同单价:", fontChinese)); table.addCell(new Paragraph(model[2], fontChinese)); table.addCell(new Paragraph("品名:", fontChinese)); table.addCell(new Paragraph(model[3], fontChinese)); table.addCell(new Paragraph("车号:", fontChinese)); table.addCell(new Paragraph(model[4], fontChinese)); table.addCell(new Paragraph("仓号", fontChinese)); table.addCell(new Paragraph(model[5], fontChinese)); table.addCell(new Paragraph("件数:", fontChinese)); table.addCell(new Paragraph(model[6], fontChinese)); table.addCell(new Paragraph("重车:", fontChinese)); table.addCell(new Paragraph(model[7], fontChinese)); table.addCell(new Paragraph("空车:", fontChinese)); table.addCell(new Paragraph(model[8], fontChinese)); table.addCell(new Paragraph("净重:", fontChinese)); table.addCell(new Paragraph(model[9], fontChinese)); table.addCell(new Paragraph("结算:", fontChinese)); table.addCell(new Paragraph(model[10], fontChinese)); table.addCell(new Paragraph("容重", fontChinese)); table.addCell(new Paragraph(model[11], fontChinese)); table.addCell(new Paragraph("扣:", fontChinese)); table.addCell(new Paragraph(model[12], fontChinese)); PdfPCell cell_rong = new PdfPCell(new Phrase("采购意见:", fontChinese)); PdfPCell cell_rongVal = new PdfPCell(new Phrase(model[13], fontChinese)); cell_rong.setFixedHeight(25); cell_rong.setRowspan(2); cell_rongVal.setColspan(3); cell_rongVal.setRowspan(2); table.addCell(cell_rong); table.addCell(cell_rongVal); table.setSpacingAfter(30); document.add(table); k++; } } pdfWriter.flush(); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
表格生成pdf
最新推荐文章于 2023-09-22 10:29:30 发布