使用iTextpdf保存PDF 表格到本地

public String SavePDFPath = sdpath + "PDF/";        

    //保存成PDF格式的表格
    public void toPDF(String pdf_save_address, String pdf_name, MeasureData measureData) {
        // 创建文档实例
        File f = new File(pdf_save_address);
        Utils.instance().makeDir(f);
        Document document = new Document(PageSize.A4);
        FileOutputStream fos = null;
        PdfWriter writer;
        try {
            fos = new FileOutputStream(pdf_save_address + "/" + pdf_name);
            // 获取PdfWriter实例
            writer = PdfWriter.getInstance(document, fos);
            // 设置每行的间距
            writer.setInitialLeading(30);
            // 打开文档
            document.open();
            // 添加标题内容
            document.add(new Paragraph("测量结果", setChineseFont()));
            // 添加标题内容
            document.add(new Paragraph(" ", setChineseFont()));
            String str = "     流量计编号: " + measureData.getDeviceNumber() + "         被测仪器型号: " + measureData.getOtherDeviceType() + "        被测仪器编号: " +
                    measureData.getOtherDeviceNumber() + "         测量时间: " + measureData.getTime_D() + " " + measureData.getTime_H();
            // 添加标题内容
            document.add(new Paragraph(str, setChineseFont2()));
            // 添加标题内容
            document.add(new Paragraph(" ", setChineseFont2()));
            // 添加标题内容
            document.add(new Paragraph("测量标准", setChineseFont2()));
            // 添加标题内容
            document.add(new Paragraph(" ", setChineseFont2()));
            // 添加表格,4列 注意:必须设置列数!!!
            PdfPTable table = new PdfPTable(6);
            // 设置table的宽度占父容器宽度的100%
            table.setWidthPercentage(100);
            // 设置表格上面空白宽度
            table.setSpacingBefore(10f);
            // 设置表格下面空白宽度
            table.setSpacingAfter(10f);
            // 表格只可以设置水平对齐
            table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
//            table.addCell(new Paragraph("示值误差", setChineseFont2()));
//            table.addCell(new Paragraph("带载误差", setChineseFont2()));
//            table.addCell(new Paragraph("稳定性", setChineseFont2()));
//            table.addCell(new Paragraph("重复性", setChineseFont2()));
//            table.addCell(new Paragraph("环境温度", setChineseFont2()));
//            table.addCell(new Paragraph("大气压", setChineseFont2()));
            for (int i = 0; i < 6; i++) {
                PdfPCell cell1 = new PdfPCell();
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                Paragraph paragraph=null;
                switch (i){
                    case 0:
                        paragraph= new Paragraph("示值误差", setChineseFont2());
                        break;
                    case 1:
                        paragraph= new Paragraph("带载误差", setChineseFont2());
                        break;
                    case 2:
                        paragraph= new Paragraph("稳定性", setChineseFont2());
                        break;
                    case 3:
                        paragraph= new Paragraph("重复性", setChineseFont2());
                        break;
                    case 4:
                        paragraph= new Paragraph("环境温度", setChineseFont2());
                        break;
                    case 5:
                        paragraph= new Paragraph("大气压", setChineseFont2());
                        break;

                }
                //设置该段落为居中显示
                cell1.setPhrase(paragraph);
                table.addCell(cell1);
            }
            PdfPCell cell14 = new PdfPCell();
            cell14.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell14.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getErrorOfIndication(), setChineseFont2()));
            table.addCell(cell14);
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getErrorOfIndication(), setChineseFont2()));
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getErrorLoading(), setChineseFont2()));
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getErrorLoading(), setChineseFont2()));
            table.addCell(cell14);
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getFlowRange(), setChineseFont2()));
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getFlowRange(), setChineseFont2()));
            table.addCell(cell14);
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getRepeatability(), setChineseFont2()));
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getRepeatability(), setChineseFont2()));
            table.addCell(cell14);
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getAmbientTemperature(), setChineseFont2()));
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getAmbientTemperature(), setChineseFont2()));
            table.addCell(cell14);
//            table.addCell(new Paragraph(measureData.getMeasurementStandard().getBarometricPressure(), setChineseFont2()));
            cell14.setPhrase(new Paragraph(measureData.getMeasurementStandard().getBarometricPressure(), setChineseFont2()));
            table.addCell(cell14);
            document.add(table);
            // 添加标题内容
            document.add(new Paragraph(" ", setChineseFont2()));
            // 添加标题内容
            document.add(new Paragraph("测量数据", setChineseFont2()));
            // 添加标题内容
            document.add(new Paragraph(" ", setChineseFont2()));

            // 添加表格,4列 注意:必须设置列数!!!
            PdfPTable table2 = new PdfPTable(12);
            // 设置table的宽度占父容器宽度的100%
            table2.setWidthPercentage(100);
            // 设置表格上面空白宽度
            table2.setSpacingBefore(10f);
            // 设置表格下面空白宽度
            table2.setSpacingAfter(10f);
            // 表格只可以设置水平对齐
            table2.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
            for (int i = 0; i < 12; i++) {
                PdfPCell cell1 = new PdfPCell();
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                Paragraph paragraph=null;
                switch (i){
                    case 0:
                         paragraph= new Paragraph("序号", setChineseFont2());
                        break;
                    case 1:
                        paragraph= new Paragraph("流量点", setChineseFont2());
                        break;
                    case 2:
                        paragraph= new Paragraph("平均次数", setChineseFont2());
                        break;
                    case 3:
                        paragraph= new Paragraph("平均值", setChineseFont2());
                        break;
                    case 4:
                        paragraph= new Paragraph("负载压力", setChineseFont2());
                        break;
                    case 5:
                        paragraph= new Paragraph("示值误差", setChineseFont2());
                        break;
                    case 6:
                        paragraph= new Paragraph("重复性", setChineseFont2());
                        break;
                    case 7:
                        paragraph= new Paragraph("稳定性", setChineseFont2());
                        break;
                    case 8:
                        paragraph= new Paragraph("最大值", setChineseFont2());
                        break;
                    case 9:
                        paragraph= new Paragraph("最小值", setChineseFont2());
                        break;
                    case 10:
                        paragraph= new Paragraph("极差", setChineseFont2());
                        break;
                    case 11:
                        paragraph= new Paragraph("结果", setChineseFont2());
                        break;
                }
                //设置该段落为居中显示
                cell1.setPhrase(paragraph);
                table2.addCell(cell1);
            }
            ArrayList metricalDatas = measureData.getMetricalDatas();
            for (int i = 0; i < metricalDatas.size(); i++) {
                MetricalData metricalData = (MetricalData) metricalDatas.get(i);
                PdfPCell cell1 = new PdfPCell();
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell1.setPhrase(new Paragraph((i + 1) + "", setChineseFont2()));
                table2.addCell(cell1);
                cell1.setPhrase(new Paragraph(metricalData.getTrafficPoint(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getTrafficPoint(), setChineseFont2()));
//                table2.addCell(new Paragraph(metricalData.getAverageNum(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getAverageNum(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getAverage(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getAverage(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getPressure(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getPressure(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getErrorOfIndication(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getErrorOfIndication(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getRepeatability(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getRepeatability(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getStability(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getStability(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getMaxinum(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getMaxinum(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getMininum(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getMininum(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getRange(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getRange(), setChineseFont2()));
                table2.addCell(cell1);
//                table2.addCell(new Paragraph(metricalData.getResult(), setChineseFont2()));
                cell1.setPhrase(new Paragraph(metricalData.getResult(), setChineseFont2()));
                table2.addCell(cell1);
            }

            document.add(table2);

            // 如果某一个单元格没有内容,也必须创建并添加单元格。
            // 如果某一行的所有列并没有填充完成,该表格的最后一行会不显示。
            // 因为这种流式添加数据,所以有某个单元格没有被添加,最后肯定表现在最后一行上。
            // 所以如果某一个单元格没有内容,也必须创建并添加单元格!使用completeRow()方法填充,需要注意compleRow方法只针对于当前行,并不是所有行。
            table.completeRow();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 操作完成后必须执行文档关闭操作
            if (document != null) {
                document.close();
            }
            // 关闭文件输出流
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 设置PDF字体(较为耗时)
     */
    public Font setChineseFont() {
        BaseFont bf = null;
        Font fontChinese = null;
        try {
            // STSong-Light : Adobe的字体
            // UniGB-UCS2-H : pdf 字体
            bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            fontChinese = new Font(bf, 14, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fontChinese;
    }

    /**
     * 设置PDF字体(较为耗时)
     */
    public Font setChineseFont2() {
        BaseFont bf = null;
        Font fontChinese = null;
        try {
            // STSong-Light : Adobe的字体
            // UniGB-UCS2-H : pdf 字体
            bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);

            fontChinese = new Font(bf, 10, Font.NORMAL);
            fontChinese.setColor(BaseColor.DARK_GRAY);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fontChinese;
    }

iTextpdf.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值