itext制作pdf表格

简介: java中使用itext制作pdf表格

引入依赖:

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.7</version>
</dependency>

<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.6</version>
</dependency>

代码:

    public void doPrintInput(HttpServletRequest request, HttpServletResponse response, List<ItemAddition> list) {
   
        /**
         * 创建Document文档对象(页面的大小为A4,左、右、上、下的页边距)
         * rotate():pdf横向显示
         */
        Document document = new Document(PageSize.A4.rotate(), 20, 20, 10, 10);
        try {
   
            FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf");

            response.setContentType("application/pdf;charset=UTF8");
            //比较关键的是Content-Disposition是inline而不是attachment,这样提示浏览器来显示文档而不是下载
            response.setHeader("Content-Disposition", "inline;fileName=文档.pdf");
            //如果是下载而不是预览的话,将下边的注释打开即可(把上边的注释掉)
            //response.setHeader("Content-Disposition", "attachment;filename=aaa.pdf");

            //PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

            String path = request.getContextPath();
            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
            String emptyTemplateUrl = "resources/common/font/SIMYOU.TTF";


            BaseFont bfChinese = BaseFont.createFont(basePath + emptyTemplateUrl, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font = new Font(bfChinese, 12, Font.NORMAL);

            HeaderFooter footer = new HeaderFooter(new Phrase("-", font), new Phrase("-", font));

            footer.setBorder(Rectangle.UNDEFINED);
            /**
             * 设置页码的显示位置
             * 0是靠左
             * 1是居中
             * 2是居右
             */
            footer.setAlignment(1);

            document.setFooter(footer);

            document.open();

            //设置表格的标题
            Paragraph title = new Paragraph("事业单位收费目录清单", new Font(bfChinese, 15));
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);


            PdfPTable table = new PdfPTable(7); // 7 列
            //设置行数
            table.setWidthPercentage(100); // Width 100%
            table.setSpacingBefore(10f); // Space before table
            table.setSpacingAfter(10f); // Space after table


            // 设置单元格宽度
            float[] columnWidths = {
   0.3f, 0.8f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f};
            table.setWidths(columnWidths);

            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            cell1.setBorderColor(Color.BLACK);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell1.setPhrase(new Paragraph("序号", font));
            //cell1.setRowspan(2); 设置一个单元格占几行


            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            cell2.setBorderColor(Color.BLACK);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell2.setPhrase(new Paragraph("收费项目代码", font));

            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
            cell3.setBorderColor(Color.BLACK);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell3.setPhrase(new Paragraph("收费项目名称", font));

            PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
            cell4.setBorderColor(Color.BLACK);
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell4.setPhrase(new Paragraph("收费标准", font));

            PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
            cell5.setBorderColor(Color.BLACK);
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell5.setPhrase(new Paragraph("标准确定方式", font));

            PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 6"));
            cell6.setBorderColor(Color.BLACK);
            cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell6.setPhrase(new Paragraph("政策依据", font));

            PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 7"));
            cell7.setBorderColor(Color.BLACK);
            cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell7.setPhrase(new Paragraph("资金管理方式", font));

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);
            table.addCell(cell5);
            table.addCell(cell6);
            table.addCell(cell7);


            //循环获取数据并添加导表格里
            for (int i = 0; i < list.size(); i++) {
   
                //content.get(i).get
                String num = String.valueOf(i + 1);
                //序号
                PdfPCell cell11 = new PdfPCell(new Paragraph("Cell 11" + num));
                //收费项目代码
                PdfPCell cell22 = new PdfPCell(new Paragraph("Cell 22" + num));
                //收费项目名称
                PdfPCell cell33 = new PdfPCell(new Paragraph("Cell 33" + num));
                //收费标准
                PdfPCell cell44 = new PdfPCell(new Paragraph("Cell 44" + num));
                //标准确定方式
                PdfPCell cell55 = new PdfPCell(new Paragraph("Cell 55" + num));
                //政策依据
                PdfPCell cell66 = new PdfPCell(new Paragraph("Cell 66" + num));
                //资金管理方式
                PdfPCell cell77 = new PdfPCell(new Paragraph("Cell 77" + num));

                cell11.setBorderColor(Color.BLACK);
                cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell11.setPhrase(new Paragraph(num, font));

                cell22.setBorderColor(Color.BLACK);
                cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell22.setPhrase(new Paragraph(list.get(i).getFitemCode(), font));

                cell33.setBorderColor(Color.BLACK);
                cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell33.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell33.setPhrase(new Paragraph(list.get(i).getFitemName(), font));

                cell44.setBorderColor(Color.BLACK);
                cell44.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell44.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell44.setPhrase(new Paragraph(list.get(i).getFchargeStd(), font));

                cell55.setBorderColor(Color.BLACK);
                cell55.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell55.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell55.setPhrase(new Paragraph(list.get(i).getFstdConfirmWayName(), font));

                cell66.setBorderColor(Color.BLACK);
                cell66.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell66.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell66.setPhrase(new Paragraph(list.get(i).getPolicys(), font));

                cell77.setBorderColor(Color.BLACK);
//                cell77.setPaddingLeft(10);
                cell77.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell77.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell77.setPhrase(new Paragraph(list.get(i).getFfundManageWayName(), font));

                table.addCell(cell11);
                table.addCell(cell22);
                table.addCell(cell33);
                table.addCell(cell44);
                table.addCell(cell55);
                table.addCell(cell66);
                table.addCell(cell77);

            }

            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {
   
            e.printStackTrace();
        }
    }
相关文章
|
2月前
|
Java
java Itext创建pdf
java Itext创建pdf
33 0
|
2月前
使用Itext导出PDF
使用Itext导出PDF
24 0
|
2月前
|
机器学习/深度学习 数据可视化 数据挖掘
【办公自动化】使用Python一键提取PDF中的表格到Excel
【办公自动化】使用Python一键提取PDF中的表格到Excel
72 0
|
3月前
|
数据采集 数据安全/隐私保护 Python
Python 快速合并PDF表格转换输出CSV文件
Python 快速合并PDF表格转换输出CSV文件
49 0
Python 快速合并PDF表格转换输出CSV文件
|
4月前
|
Java
Java 读取 Excel 模板,将数据填入Excel表格,后转换为PDF文件(实用)
Java 读取 Excel 模板,将数据填入Excel表格,后转换为PDF文件(实用)
103 0
|
8月前
|
JavaScript 数据安全/隐私保护
VUE 表格导出PDF格式
VUE 表格导出PDF格式
125 0
|
9月前
|
文字识别
解析pdf图片格式的表格到excel表格
解析pdf图片格式的表格到excel表格,原理是把pdf转换成图片,根据直方图定位表格单元格线条位置,按照单元格切分原始pdf,按顺序ocr单元格内容,最终拼接成完整excel
122 1
|
10月前
|
Java BI
iText导出PDF多表格
iText导出PDF多表格
151 0
|
XML 存储 JSON
2.基于Label studio的训练数据标注指南:(智能文档)文档抽取任务、PDF、表格、图片抽取标注等
2.基于Label studio的训练数据标注指南:(智能文档)文档抽取任务、PDF、表格、图片抽取标注等
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
86 0