java 操作 PDF

设置路径的方法:   


public String getPath(String path) {
        path = ("C:\\" + path + ".pdf");
        return path;
    }


 // 设置字体的方法
    public static Font font() {
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font = new Font(baseFont, 12, Font.BOLD, Color.BLUE);
        return font;
    }

 // 设置字体的方法
    public static Font ChineseFont() {
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font chineseFont = new Font(baseFont, 8, Font.NORMAL, Color.BLUE);
        return chineseFont;
    }
    // 表格是否有数据
    public String isNull(Object object) {
        if (object == null) {
            return "";
        }
        return object + "";
    }

具体设计的代码:


 public String getRBPDF(QcyUnitedOrderInfo pageObject) throws Exception {

        // TODO Auto-generated method stub
        float totalsum = 0;
        float totalnum = 0;
        List<Object[]> list = (List<Object[]>) this.findRBAllProperty(pageObject);//获取所需要显示的数据
        // (1)实例化文档对象
        // 第一个参数是页面的大小,接下来的参数分别是,左、右、上和下页边距
        Document doc = new Document(PageSize.A4, 14, 14, 20, 20);
        // (2)创建写入器。第一个参数是对文档对象的引用
        // 第二个参数是输出的文件,将out和document连接起来
        PdfWriter writers = PdfWriter.getInstance(doc, new FileOutputStream(new File(getPath(pageObject.getNo()))));//将编号传入,设置好路径和文件名称
        doc.addCreationDate();
        //生成PDF页眉
        HeaderFooter footer = new HeaderFooter(new Phrase(), true);
        footer.setBorder(0);//设置边框
        footer.setAlignment(Element.ALIGN_CENTER);//对齐设置
        doc.setFooter(footer);
        // 打开文档,准备写入内容
        doc.open();
        
        // 表头设置,创建小节标题,往小节中写文本内容
//        Paragraph par3 = new Paragraph("苏州九龙珠餐饮管理有限公司", font());
        Paragraph par3 = new Paragraph("#####有限公司", font());
        par3.setAlignment(Element.ALIGN_CENTER);
        //将章节对象加入到文档中
        doc.add(par3);
        
        // 表头设置,创建小节标题,往小节中写文本内容
        Paragraph par4 = new Paragraph("销  货  订  单", font());
        par4.setAlignment(Element.ALIGN_CENTER);
        doc.add(par4);
        // 表头设置结束
        
        // 表头设置,创建小节标题,往小节中写文本内容
        Paragraph paragraph1 = new Paragraph("开单日期:", ChineseFont());
        paragraph1.setIndentationLeft(100);//左边缩进设置
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Chunk chunk1 = new Chunk(sdf.format(pageObject.getReceiptDate()) + "         ");
        Chunk chunk2 = new Chunk("                                                                           " + "单   号:  ");
        Chunk chunk13 = new Chunk(pageObject.getNo());
        paragraph1.add(chunk1);
        paragraph1.add(chunk2);
        paragraph1.add(chunk13);
        doc.add(paragraph1);

        Paragraph paragraph = new Paragraph("分   部 :", ChineseFont());
        paragraph.setSpacingBefore(10);//之前间隔多少
        paragraph.setSpacingAfter(10);//之后间隔多少
        paragraph.setIndentationLeft(100);//左边缩进设置
        Chunk chunk = new Chunk(pageObject.getRequestBranchName());
        paragraph.add(chunk);
        Chunk chunks1 = new Chunk("                                                                                           " + "送货地址:  ");
        Chunk chunks11 = new Chunk("");
        paragraph.add(chunks1);
        paragraph.add(chunks11);
        doc.add(paragraph);

        float[] withs = { 0.05f, 0.15f, 0.12f, 0.14f, 0.1f,0.1f, 0.06f, 0.1f, 0.1f, 0.1f, 0.08f };
        // 创建表格对象
        PdfPTable table = new PdfPTable(withs);
        
        PdfPCell cell1 = new PdfPCell(new Phrase("序号", ChineseFont()));// 建立一个单元格
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置内容水平居中显示
        cell1.setVerticalAlignment(Element.ALIGN_LEFT);// 设置内容水平居中显示
        cell1.setBackgroundColor(Color.lightGray);//背景颜色

        PdfPCell cell2 = new PdfPCell(new Phrase("存货编码", ChineseFont()));
        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell2.setVerticalAlignment(Element.ALIGN_CENTER);
        cell2.setBackgroundColor(Color.lightGray);

        PdfPCell cell3 = new PdfPCell(new Phrase("存货名称", ChineseFont()));
        cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell3.setVerticalAlignment(Element.ALIGN_CENTER);
        cell3.setBackgroundColor(Color.lightGray);

        PdfPCell cell4 = new PdfPCell(new Phrase("规格", ChineseFont()));
        cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell4.setVerticalAlignment(Element.ALIGN_CENTER);
        cell4.setBackgroundColor(Color.lightGray);

        PdfPCell cell5 = new PdfPCell(new Phrase("数量", ChineseFont()));
        cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell5.setVerticalAlignment(Element.ALIGN_CENTER);
        cell5.setBackgroundColor(Color.lightGray);
        
        PdfPCell cell6 = new PdfPCell(new Phrase("预估量", ChineseFont()));
        cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell6.setVerticalAlignment(Element.ALIGN_CENTER);
        cell6.setBackgroundColor(Color.lightGray);
        
        PdfPCell cell7 = new PdfPCell(new Phrase("单位", ChineseFont()));
        cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell7.setVerticalAlignment(Element.ALIGN_CENTER);
        cell7.setBackgroundColor(Color.lightGray);

        PdfPCell cell8 = new PdfPCell(new Phrase("单价", ChineseFont()));
        cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell8.setVerticalAlignment(Element.ALIGN_CENTER);
        cell8.setBackgroundColor(Color.lightGray);

        PdfPCell cell9 = new PdfPCell(new Phrase("货款", ChineseFont()));
        cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell9.setVerticalAlignment(Element.ALIGN_CENTER);
        cell9.setBackgroundColor(Color.lightGray);

        PdfPCell cell10 = new PdfPCell(new Phrase("交易日期", ChineseFont()));
        cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell10.setVerticalAlignment(Element.ALIGN_CENTER);
        cell10.setBackgroundColor(Color.lightGray);

        PdfPCell cell11 = new PdfPCell(new Phrase("备注", ChineseFont()));
        cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell11.setVerticalAlignment(Element.ALIGN_CENTER);
        cell11.setBackgroundColor(Color.lightGray);
        
        // (6)往小节中写表格
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
        table.addCell(cell7);
        table.addCell(cell8);
        table.addCell(cell9);
        table.addCell(cell10);
        table.addCell(cell11);

        for (int i = 0; i < list.size(); i++) {
            float a = 0;
            float b = 0;
            if (null != list.get(i)[4] && null != list.get(i)[6]) {
                a = Float.parseFloat("" + list.get(i)[4]);
                b = Float.parseFloat("" + list.get(i)[6]);
                totalnum += Float.parseFloat("" + list.get(i)[4]);
                totalsum += (a * b);
            }
            PdfPCell cell111 = new PdfPCell(new Phrase(i + 1 + ""));//序号
            PdfPCell cell222 = new PdfPCell(new Phrase("" + isNull(list.get(i)[0]), ChineseFont()));//编码
            PdfPCell cell333 = new PdfPCell(new Phrase("" + isNull(list.get(i)[1]), ChineseFont()));//名称
            PdfPCell cell444 = new PdfPCell(new Phrase("" + isNull(list.get(i)[2]), ChineseFont())); // 规格字段添加
            PdfPCell cell555 = new PdfPCell(new Phrase("" + isNull(list.get(i)[4]), ChineseFont()));//审核量
            PdfPCell cell666 = new PdfPCell(new Phrase("" + isNull(list.get(i)[5]), ChineseFont()));//预估量
            PdfPCell cell777 = new PdfPCell(new Phrase("" + isNull(list.get(i)[3]), ChineseFont()));//单位
            PdfPCell cell888 = new PdfPCell(new Phrase("" + isNull(list.get(i)[6]), ChineseFont()));//单价
            PdfPCell cell999 = new PdfPCell(new Phrase("" + isNull(a * b), ChineseFont()));//货款        
            PdfPCell cell100 = new PdfPCell(new Phrase("" + isNull(list.get(i)[7]), ChineseFont()));//单价日期
            PdfPCell cell120 = new PdfPCell(new Phrase("" + isNull(list.get(i)[8]), ChineseFont()));//备注

            cell111.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell111.setVerticalAlignment(Element.ALIGN_CENTER);

            cell222.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell222.setVerticalAlignment(Element.ALIGN_CENTER);

            cell333.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell333.setVerticalAlignment(Element.ALIGN_CENTER);

            cell444.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell444.setVerticalAlignment(Element.ALIGN_CENTER);

            cell555.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell555.setVerticalAlignment(Element.ALIGN_CENTER);

            cell666.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell666.setVerticalAlignment(Element.ALIGN_CENTER);

            cell777.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell777.setVerticalAlignment(Element.ALIGN_CENTER);

            cell888.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell888.setVerticalAlignment(Element.ALIGN_CENTER);

            cell999.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell999.setVerticalAlignment(Element.ALIGN_CENTER);

            cell100.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell100.setVerticalAlignment(Element.ALIGN_CENTER);
            
            cell120.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell120.setVerticalAlignment(Element.ALIGN_CENTER);

         
            table.addCell(cell111);
            table.addCell(cell222);
            table.addCell(cell333);
            table.addCell(cell444);
            table.addCell(cell555);
            table.addCell(cell666);
            table.addCell(cell777);
            table.addCell(cell888); 
            table.addCell(cell999); 
            table.addCell(cell100);
            table.addCell(cell120);
        }

        PdfPCell cell = new PdfPCell(new Phrase("合      计", fonts()));
        cell.setColspan(2);// 设置合并单元格的列数
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);// 增加单元格

        PdfPCell ce = new PdfPCell();
        ce.setColspan(3);
        table.addCell(ce);

        PdfPCell ce11 = new PdfPCell(new Phrase("" + totalnum, ChineseFont()));
        ce11.setHorizontalAlignment(Element.ALIGN_CENTER);
        ce11.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(ce11);

        PdfPCell ce1 = new PdfPCell();
        ce1.setColspan(2);
        table.addCell(ce1);

        PdfPCell ce22 = new PdfPCell(new Phrase("" + totalsum, ChineseFont()));
        ce22.setHorizontalAlignment(Element.ALIGN_CENTER);
        ce22.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(ce22);

        PdfPCell c = new PdfPCell();
        // ce1.setColspan(1);
        table.addCell(c);

        PdfPCell cc = new PdfPCell();
        // ce1.setColspan(1);
        table.addCell(cc);

        PdfPCell ce2 = new PdfPCell(new Phrase("摘     要", fonts()));
        ce2.setColspan(2);
        ce2.setHorizontalAlignment(Element.ALIGN_CENTER);
        ce2.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(ce2);

        PdfPCell ce3 = new PdfPCell(new Phrase("" + isNull(""), fonts())); // 摘要字段添加
        ce3.setColspan(9);
        table.addCell(ce3);


        Paragraph paragraph3 = new Paragraph("总部地址及电话 :  ", ChineseFont());
        paragraph3.setIndentationLeft(80);
        Chunk chunk3 = new Chunk("上海##      电话: 233454678     传  真:  123456783454");
        paragraph3.add(chunk3);

        Paragraph parag = new Paragraph("工商行政管理机关提示:", fonts());
        Chunk ch = new Chunk("该供货单可作为进货台账,须备案存档至少两年");
        parag.setIndentationLeft(110);
        parag.add(ch);

        Paragraph pa = new Paragraph("工商行政管理局监制", fonts());
        pa.setIndentationLeft(210);

        Paragraph pat = new Paragraph("购货单位 (章):                 制单人:                      审核人:                      业务员", ChineseFont());
        pat.setIndentationLeft(80);
        
        //将章节对象加入到文档中
        doc.add(table);
        doc.add(paragraph3);
        doc.add(parag);
        doc.add(pa);
        doc.add(pat);
        doc.newPage();
        // 关闭文档
        doc.close();
        writers.close();
        return getPath(pageObject.getNo());

    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值