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

package com.ljt.study.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Study {
    public static void main(String[] args) {
        Document document = new Document(PageSize.A4);
        /** 创建 PdfWriter 对象 */// 文档对象的引用
        // 为document创建一个监听,并把PDF流写到文件中
        String filePath = "D:\temp.pdf";
        try {
            PdfWriter.getInstance(document, new FileOutputStream(filePath)); // 文件的输出路径+文件的实际名称
            document.open();// 打开文档

            //"C:\Windows\Fonts\simfang.ttf"
            BaseFont bf = BaseFont.createFont("C:\Windows\Fonts\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font titleFont = new Font(bf, 10, Font.BOLD); // 24号加粗斜体
            // 空格代码
            Paragraph blank = new Paragraph(" ");

            Paragraph titleParagraph = new Paragraph("RISEN ENERGY CO.,LTD", titleFont);
            document.add(titleParagraph); // 文档标题
            document.add(blank);
            Paragraph addressParagraph = new Paragraph("Address: NO.1 SHUINAN ROAD,ZHIXI INDUSTRIAL ZONE,JINTAN CITY CHANGZHOU, JIANGSU PROVINCE, CHINAnTel: +86 519-82359071nFax: info@risenenergy.com", new Font(bf, 10, Font.NORMAL));
            document.add(addressParagraph); // 客户地址
            document.add(blank);
            document.add(blank);
            document.add(blank);


            // 画横线
            //1.线宽度
            //2.直线长度,是个百分百,0-100之间
            //3.直线颜色
            //4.直线位置
            //5.上下移动位置
            LineSeparator line = new LineSeparator(2f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, 0f);
            document.add(line); // 画横线
            Paragraph invoiceParagraph = new Paragraph("INVOICE", new Font(bf, 20, Font.BOLD));
            invoiceParagraph.setSpacingBefore(20);
            document.add(invoiceParagraph); // invoice
            document.add(blank);
            Paragraph billTo = new Paragraph("Bill To", new Font(bf, 12, Font.BOLD));
            document.add(billTo); // billto



            Paragraph headerInfoParagraph = new Paragraph("头部信息", new Font(bf, 12, Font.NORMAL));
            headerInfoParagraph.setIndentationRight(PageSize.A4.getWidth() / 2 - 30);
            Paragraph addressParagraph_ = new Paragraph("北京市朝阳区XXX号", new Font(bf, 12, Font.NORMAL));
            Paragraph telFaxParagraph_ = new Paragraph("888888888", new Font(bf, 12, Font.NORMAL));
            document.add(headerInfoParagraph);
            document.add(addressParagraph_);
            document.add(telFaxParagraph_);

            document.add(blank);

            //处理表格
            List<String> invoiceList = createInvoiceList();
            float[] columnWidths = {1.2f, // 件号
                    1.5f, // 英文品名
                    1f, // 数量
                    0.8f, // 单位
                    1.1f, // 币制
                    1.1f, // 单价
                    1.5f, // 总价
            };
            // 设置表格列宽参数
            getCiTableList(document, 7, columnWidths, invoiceList, "CI", 100, new Font(bf, 13, Font.BOLD), new Font(bf, 13, Font.NORMAL));

            document.close();

        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }



    public static List<String> createInvoiceList() {
        List<String> invoiceList = new ArrayList<>();
        /****标题行****/
        invoiceList.add("Part No.");
        invoiceList.add("DESCRIPTION");
        invoiceList.add("QTY.");
        invoiceList.add("UNIT");
        invoiceList.add("CURRENCY");
        invoiceList.add("Unit price");
        invoiceList.add("AMOUNT");


        /****每行的值****/
        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("10"); // 件号物料号
        invoiceList.add("KG"); // 单位
        invoiceList.add("KG"); // 币制
        invoiceList.add("20"); // 单价
        invoiceList.add("100"); // 总价

        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("10"); // 件号物料号
        invoiceList.add("KG"); // 单位
        invoiceList.add("KG"); // 币制
        invoiceList.add("20"); // 单价
        invoiceList.add("100"); // 总价

        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("10"); // 件号物料号
        invoiceList.add("KG"); // 单位
        invoiceList.add("KG"); // 币制
        invoiceList.add("20"); // 单价
        invoiceList.add("100"); // 总价

        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("SA-14400-1"); // 件号物料号
        invoiceList.add("10"); // 件号物料号
        invoiceList.add("KG"); // 单位
        invoiceList.add("KG"); // 币制
        invoiceList.add("20"); // 单价
        invoiceList.add("100"); // 总价

        return invoiceList;
    }


    public static void getCiTableList(Document document, int colspan, float[] columnWidths, List<String> list, String type,
                                      float widthPercentage, Font headFont, Font childFont) throws DocumentException {
        PdfPTable table = new PdfPTable(colspan);// 设置列数
        table.setSpacingBefore(20);
        table.setWidthPercentage(widthPercentage);// 表格宽度为100%
        if (columnWidths != null) {//自定义列宽
            table.setWidths(columnWidths);
        }
        int number = 0;
        //设置打印行数
        for (int i = 0; i < list.size() / colspan; i++) {//打印条数 = 数据个数除以列数
            //每行每列生成一个单元格
            for (int j = 0; j < colspan; j++) { //打印列数
                PdfPCell cell = new PdfPCell(); //创建单元格
                cell.setMinimumHeight(30F);//表格高度
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
                if (i == 0) {//第一行标题栏
                    cell.setPhrase(new Paragraph(list.get(number), headFont));
                } else {
                    cell.setPhrase(new Paragraph(list.get(number), childFont));
                }
                table.addCell(cell);
                number++;
            }
        }
        // 处理最后一行
        if ("CI".equalsIgnoreCase(type)) {
            BigDecimal totalQty = new BigDecimal("0");
            BigDecimal totalAmount = new BigDecimal("0");
            /*for (ApplyInvoices a : applyInvoices) {
                totalQty = totalQty.add(isNotEmpty(a.getQty()) ? a.getQty() : new BigDecimal("0"));
                totalAmount = totalAmount.add(isNotEmpty(a.getValue()) ? a.getValue() : new BigDecimal("0"));
            }*/
            PdfPCell cell = new PdfPCell(); //创建单元格
            cell.setMinimumHeight(30F);//表格高度
            cell.setColspan(2);
            cell.setUseAscender(true); //开启单元格内文字位置设计
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell.setPhrase(new Paragraph("TOTAL", headFont));
            table.addCell(cell);
            PdfPCell cell1 = new PdfPCell(); //创建单元格
            cell1.setMinimumHeight(30F);//表格高度
            cell1.setUseAscender(true); //开启单元格内文字位置设计
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell1.setPhrase(new Paragraph(totalQty.toString(), headFont));
            table.addCell(cell1);
            PdfPCell cell2 = new PdfPCell(); //创建单元格
            cell2.setMinimumHeight(30F);//表格高度
            cell2.setUseAscender(true); //开启单元格内文字位置设计
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell2.setPhrase(new Paragraph("", headFont));
            table.addCell(cell2);
            PdfPCell cell3 = new PdfPCell(); //创建单元格
            cell3.setMinimumHeight(30F);//表格高度
            cell3.setUseAscender(true); //开启单元格内文字位置设计
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell3.setPhrase(new Paragraph("", headFont));
            table.addCell(cell3);
            PdfPCell cell4 = new PdfPCell(); //创建单元格
            cell4.setMinimumHeight(30F);//表格高度
            cell4.setUseAscender(true); //开启单元格内文字位置设计
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell4.setPhrase(new Paragraph("", headFont));
            table.addCell(cell4);
            PdfPCell cell5 = new PdfPCell(); //创建单元格
            cell5.setMinimumHeight(30F);//表格高度
            cell5.setUseAscender(true); //开启单元格内文字位置设计
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER); //设置单元格的水平居中
            cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置单元格的垂直居中
            cell5.setPhrase(new Paragraph(totalAmount.toString(), headFont));
            table.addCell(cell5);
        }
        document.add(table);
    }


}

原文地址:https://blog.csdn.net/LJT666888/article/details/134572355

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_578.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注