package com.dxhy.core.util;

import java.io.FileOutputStream;
import java.util.ArrayList;

import org.springframework.stereotype.Component;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

/**
 * @author Gy Chen
 * @date 2021/3/17
 */
@Component
@SuppressWarnings({"AliDeprecation", "AlibabaUndefineMagicConstant"})
public class PDFExportUtil {
    private static PdfWriter writer;
    private static ArrayList<PdfPRow> listRow;
    private static PdfPTable table;

    public static void pdfExport(String fileTitle, String filePath, String orgInfo, java.util.List<String[]> dataList,
        String[] titleColumns, float[] columnWidths, boolean firstPage, boolean lastPage) {
        Document document = new Document(PageSize.A4);
        try {

            // 中文字体，解决中文不能显示问题
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 正文字体风格
            Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
            if (firstPage) {

                // 两列的表
                table = new PdfPTable(titleColumns.length);
                // 宽度100%填充
                table.setWidthPercentage(100);
                // 前间距
                table.setSpacingBefore(10f);
                // 后间距
                table.setSpacingAfter(10f);
                listRow = table.getRows();
                // 设置列宽比例 1:3
                table.setWidths(columnWidths);
                // 一行两格
                PdfPCell[] cells1 = new PdfPCell[titleColumns.length];
                PdfPRow row1 = new PdfPRow(cells1);
                for (int i = 0; i < titleColumns.length; i++) {
                    cells1[i] = new PdfPCell(new Paragraph(titleColumns[i], contextFont));
                    cells1[i].setUseAscender(true);
                    cells1[i].setUseDescender(true);
                    cells1[i].setHorizontalAlignment(Element.ALIGN_CENTER);
                }

                listRow.add(row1);
            }

            for (String[] datas : dataList) {
                // 一行两格
                PdfPCell[] cells1 = new PdfPCell[titleColumns.length];
                PdfPRow row1 = new PdfPRow(cells1);
                for (int i = 0; i < datas.length; i++) {
                    cells1[i] = new PdfPCell(new Paragraph(datas[i], contextFont));
                    cells1[i].setUseAscender(true);
                    cells1[i].setUseDescender(true);
                    if (i == 7 || i == 8 || i == 9) {
                        cells1[i].setHorizontalAlignment(Element.ALIGN_RIGHT);
                    } else {
                        cells1[i].setHorizontalAlignment(Element.ALIGN_LEFT);
                    }
                }
                listRow.add(row1);
            }
            if (lastPage) {
                document = new Document(new RectangleReadOnly(842.0F, 595.0F));
                // 新建一个pdf文件
                writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
                document.open();
                // 红标题字体风格
                Font redTitleFont = new Font(bfChinese, 20, Font.BOLD, BaseColor.BLACK);
                // 红标题抬头
                Paragraph redTitle = new Paragraph(fileTitle, redTitleFont);
                // 居中
                redTitle.setAlignment(Element.ALIGN_CENTER);
                // 文档中加入红标题字段
                document.add(redTitle);

                // 标题字体风格
                Font titleFont = new Font(bfChinese, 12, Font.NORMAL);
                // 标题内容
                Paragraph title = new Paragraph(orgInfo, titleFont);
                // 离上一段落（标题）空的行数
                title.setSpacingBefore(25);
                // 设置标题格式对齐方式
                title.setAlignment(Element.ALIGN_LEFT);
                // 文档中加入标题字段
                document.add(title);
                // 把表格添加到文件中
                document.add(table);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lastPage) {
                document.close();
                writer.close();
            }
        }
    }
}
