Android使用itextpdf操作PDF文档

发布于:2024-05-09 ⋅ 阅读:(32) ⋅ 点赞:(0)

1、导入jar包:

  • itext-asian.jar
  • itextpdf-5.5.8.jar

Paragraph 和 Phrase 的区别:

在 iTextPDF 库中,Paragraph 和 Phrase 是用于创建和组织文本内容的两个不同的类。

Paragraph(段落):
Paragraph 是一个块级元素,用于创建一个段落,并可以包含多个短语(Phrase)。它是一个有自己的对齐方式、缩进和间距的独立文本块。Paragraph 类提供了更高级的文本布局和格式化功能。

Paragraph paragraph = new Paragraph("这是一个段落");
paragraph.setAlignment(Element.ALIGN_CENTER); // 设置对齐方式
paragraph.setIndentationLeft(20); // 设置左缩进
paragraph.setSpacingAfter(10); // 设置段后间距

Phrase(短语):
Phrase 是一个内联元素,用于创建一个短语,它可以包含文本、字体样式和 Chunk(文本块)。Phrase 是 Paragraph 的组成部分,可以被添加到 Paragraph 中。

Phrase phrase = new Phrase("这是一个短语");
Font boldFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
Chunk chunk = new Chunk("加粗文本", boldFont);
phrase.add(chunk);

总结:

  • Paragraph 用于创建段落,是一个独立的文本块,可以设置对齐方式、缩进和间距等。
  • Phrase 用于创建短语,是一个内联元素,可以包含文本、字体样式和 Chunk。Phrase 可以被添加到 Paragraph
    中,用于组织和格式化文本内容。

2、使用示例:


    private void createPdf(RecordEntity recordEntity, String hospitalNameStr, String numberStr, String resultStr) {
        FileUtils.copyFileIfNeed(getContext());//复制字体文件

        Executors.newSingleThreadExecutor().execute(new Runnable() {
            
            public void run() {

                long createTime = DateUtils.formatTimeToLong(recordEntity.getCreateTime(), Constants.TIME_FORMAT_UTC);
                PdfUtil pdfUtil = new PdfUtil();
                try {
                    pdfUtil.buildPdf(recordEntity, hospitalNameStr, numberStr, resultStr).build();

                    getActivity().runOnUiThread(new Runnable() {
                        
                        public void run() {
                            ToastUtil.showShortToast("生成成功");
                            File lastFile = FileUtils.getLastPdfFile(createTime);
                            }
                        }
                    });
                } catch (Exception e) {
                    Log.w("caowj", "生成PDF报告异常:" + e.getMessage());
                } finally {
                    disProgressDialog();
                    try {
                        pdfUtil.dispose();
                    } catch (Exception e) {
                        Log.w("caowj", "pdf流关闭异常:" + e.getMessage());
                    }
                }
            }
        });
    }

3、pdf工具类:

package com.kl.common_base.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.UiThread;
import android.util.Log;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.kl.common_base.R;
import com.kl.common_base.base.BaseApplication;
import com.kl.common_base.bean.NoteEntity;
import com.kl.common_base.bean.RecordEntity;
import com.kl.common_base.constants.Constants;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Objects;

public class PdfUtil {
    private File file;
    private Font fontTitle1;
    private Font fontTitle2;
    private Font fontTitle3;
    //    private Font fontText;
//    private Font fontText2;
//    private Font fontText3;
    private Document doc;
    private PdfWriter writer;
    private FileOutputStream fos;
//    public static final String PDF_ROTE_PATH = BaseApplication.getApplication().getExternalFilesDir("pdf").getAbsolutePath();
//    public final String MS = "ms";
//    public final String BPM = "bpm";
//    public final String MS_2 = "ms²";
//    private int minValue = 0;
//    private int cellHeight = 25;

    /**
     * 创建和分享pdf
     * 要在子线程执行
     */
    
    public PdfUtil buildPdf(RecordEntity recordEntity, String hospitalNameStr, String numberStr, String resultStr) throws Exception {
        long createTime = DateUtils.formatTimeToLong(recordEntity.getCreateTime(), Constants.TIME_FORMAT_UTC);
        file = FileUtils.createPdfFile(createTime);
        //1创建document实例,无参默认A4纸
        doc = new Document(PageSize.A4, 5f, 5f, 25f, 15f);//创建一个document对象
        fos = new FileOutputStream(file); //pdf_address为Pdf文件保存到sd卡的路径
        //2创建Writer实例,pdf直接使用的DocWriter的实现类PdfWriter
        writer = PdfWriter.getInstance(doc, fos);
        writer.setPageEvent(new HeaderAndFooter());
        doc.open();
//        doc.setPageCount(1);
        fontTitle1 = setChineseFont(26, Font.BOLD, Constants.FILE_PATH_FONT);
//        fontTitle1.setColor(76, 199, 191);
        fontTitle2 = setChineseFont(16, Font.BOLD, Constants.FILE_PATH_FONT);
//        fontTitle2.setColor(76, 199, 191);
        fontTitle3 = setChineseFont(12, Font.NORMAL, Constants.FILE_PATH_FONT);
        fontTitle3.setColor(76, 199, 191);
//        fontText = setChineseFont(12, Font.NORMAL, fontPath);
//        fontText2 = setChineseFont(8, Font.NORMAL, fontPath);
//        fontText3 = setChineseFont2(12, Font.NORMAL, fontPath);
        Paragraph paragraph = new Paragraph(hospitalNameStr, fontTitle1);
        paragraph.setLeading(paragraph.getLeading() - 10);//行距
        paragraph.setAlignment(Element.TITLE);//设置文字水平居中
        paragraph.setSpacingBefore(26);
        doc.add(paragraph);
        paragraph = new Paragraph("心音图报告", fontTitle2);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.TITLE);//设置文字水平居中
        paragraph.setSpacingBefore(20);
        doc.add(paragraph);
        doc.add(Chunk.NEWLINE);//换行
        doc.add(Chunk.NEWLINE);
        NoteEntity mNoteEntity = JsonUtils.formJsonString(recordEntity.getNote(), NoteEntity.class);
        paragraph = new Paragraph("姓名:" + mNoteEntity.getName(), fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);//行距
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        paragraph = new Paragraph("年龄:" + mNoteEntity.getAge(), fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        Resources resource = BaseApplication.getApplication().getResources();
        String gender = mNoteEntity.getGender() == 1 ? resource.getString(R.string.gender_female) : resource.getString(R.string.gender_male);
        paragraph = new Paragraph("性别:" + gender, fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        paragraph = new Paragraph("门诊/住院号:" + numberStr, fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        paragraph = new Paragraph("备注信息:" + mNoteEntity.getComment(), fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        // 图片
//        Image image2 = Image.getInstance(new URL("http://c.hiphotos.baidu.com/image/pic/item/9c16fdfaaf51f3de1e296fa390eef01f3b29795a.jpg"));
//        //将图片2添加到pdf文件中
//        doc.add(image2);
        doc.add(Chunk.NEWLINE);
        String fileDir = Constants.FILE_PATH_DATA + "/" + DateUtils.getFormatDate(createTime, Constants.TIME_FORMAT_FILE) + "/wave_view/";
        Log.d("caowj", "截图目录:" + fileDir);
        File file = new File(fileDir);
        if (file.exists() && file.isDirectory()) {
//            doc.newPage();
            float scale = (SizeUtils.getScreenWidth() * 1.0f - SizeUtils.dp2px(20)) / SizeUtils.dp2px(175f);
            float imgWidth = PageSize.A4.getWidth() - 10f;
            float imgHeight = imgWidth / scale;
            File flist[] = file.listFiles();
            for (File f : flist) {
                Log.d("caowj", "截图=" + f.getName());
                Image image = Image.getInstance(f.getAbsolutePath());
//                image.scaleAbsolute(120, 45);
//                image.scaleAbsoluteHeight(100f); // 将图像缩放到绝对高度。
//                image.scaleAbsoluteWidth(pageWidth); // 将图像缩放到绝对宽度。
//                image.scalePercent(30f);
                image.scaleAbsolute(imgWidth, imgHeight);
                image.setAlignment(Element.ALIGN_LEFT);
                image.setIndentationLeft(2);
                doc.add(image);
                doc.add(Chunk.NEWLINE);
            }
        }
        doc.add(Chunk.NEWLINE);
        paragraph = new Paragraph("听诊总时长:" + DateUtils.getFormatAudioMinSec(recordEntity.getTotalTime()), fontTitle3);
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        paragraph = new Paragraph("结论:" + resultStr, fontTitle3);
//        paragraph.setLeading(paragraph.getLeading() - 10);
//        paragraph.setLeading(1.5f);
        paragraph.setMultipliedLeading(1.2f);
        paragraph.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
        paragraph.setSpacingBefore(14);
        paragraph.setIndentationLeft(14);
        doc.add(paragraph);
        doc.add(Chunk.NEWLINE);//换行
        doc.add(Chunk.NEWLINE);//换行
        doc.add(Chunk.NEWLINE);//换行
        doc.add(new Paragraph("\n"));
        doc.add(new Paragraph("\n"));
        doc.add(new Paragraph("\n"));
        doc.add(new Paragraph("\n"));
        Image image = Image.getInstance(getSignImage());
        image.scaleAbsolute(50, 50); // 设置图像的大小
        paragraph = new Paragraph("医生签字:", fontTitle3);
        Log.d("caowj", "行间距:" + paragraph.getLeading());
        paragraph.add(new Chunk(image, 10, -20)); // 将图像添加到短语中
        paragraph.setLeading(paragraph.getLeading() - 10);
        paragraph.setAlignment(Element.ALIGN_CENTER);//设置文字水平居中
//        paragraph2.setSpacingBefore(20);// 前间距
//        paragraph2.setSpacingAfter(80);// 后间距
        paragraph.setIndentationLeft(200f);// 右缩进
        doc.add(paragraph);
        doc.add(Chunk.NEWLINE);//换行
        doc.add(Chunk.NEWLINE);//换行
        doc.add(Chunk.NEWLINE);//换行
//        //设置属性
//        //标题
//        doc.addTitle("this is a title");
//        //作者
//        doc.addAuthor("H__D");
//        //主题
//        doc.addSubject("this is subject");
//        //关键字
//        doc.addKeywords("Keywords");
//        //创建时间
//        doc.addCreationDate();
//        //应用程序
//        doc.addCreator("hd.com");
//        //表格
//        float[] widths = {144, 113, 191};
//        PdfPTable table = new PdfPTable(widths);
//        table.setTotalWidth(458);
//        table.setHorizontalAlignment(Element.ALIGN_LEFT);
//        Object[][] datas = {{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省", 9045, 2256}, {"广东省", 3000, 690}};
//
//        for (int i = 0; i < datas.length; i++) {
//            for (int j = 0; j < datas[i].length; j++) {
//                PdfPCell pdfCell = new PdfPCell(); //表格的单元格
//                Paragraph paragraph3 = new Paragraph("" + datas[i][j], fontTitle1);
//                pdfCell.setPhrase(paragraph3);
//                table.addCell(pdfCell);
//            }
//        }
//
//        doc.add(table); //pdf文档中加入table
        return this;
    }

    private byte[] getSignImage() {
        Resources resource = BaseApplication.getApplication().getResources();
        Bitmap bitmap = BitmapFactory.decodeResource(resource, R.drawable.icon_collected);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        byte[] bytes = outputStream.toByteArray();
//        outputStream.close();
        return bytes;
    }

    //
//    public PdfUtil createUserInfo(RecordBean patient, String hotName, boolean isNewPage, String pdfLogo) throws Exception {
//        if (isNewPage) {
//            doc.newPage();
            Image image = Image.getInstance(pdfLogo);
            image.scaleAbsolute(120, 45);
            image.setAlignment(Element.ALIGN_LEFT);
            image.setIndentationLeft(20);

            doc.add(image);
            doc.add(Chunk.NEWLINE);
//            Paragraph paragraph = new Paragraph("糖尿病心脏自主神经病变分析报告", fontTitle1);
//            paragraph.setLeading(paragraph.getLeading() - 10);
//            paragraph.setAlignment(Element.TITLE);//设置文字水平居中
//            paragraph.setSpacingBefore(20);
//            doc.add(paragraph);
//
//        }
//        doc.add(Chunk.NEWLINE);//换行
//        PdfPTable table = new PdfPTable(3);
//        String name = patient.getPatientName();
//        String gender = patient.getGender() == 0 ? "女" : "男";
//        String age = patient.getAge() + "岁";
//        String department = patient.getDepartment();
//        String admissionNo = patient.getAdmissionNo();
//        String bedNo = patient.getBedNo();
//        String[] type = {
//                "医院:" + hotName,
//                "姓名:" + name,
//                "性别:" + gender,
//                "年龄:" + age,
//                "科室:" + department,
//                "住院号:" + admissionNo,
//                "床号:" + bedNo
//        };
//        // 设置表格默认为无边框
//        table.getDefaultCell().setBorder(0);
//        for (int i = 0; i < type.length; i++) {
//            Paragraph userInfo = new Paragraph(type[i], fontText);
//            PdfPCell cell = new PdfPCell(userInfo);
//            if (i == 0) {
//                cell.setColspan(3);
//            }
//            cell.setBorder(Rectangle.NO_BORDER);
//            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//            cell.setFixedHeight(cellHeight);
//            // 设置垂直居中
//            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.A);
//            table.addCell(cell);
//        }
//        table.setWidthPercentage(85);
        if(!isNewPage)
        table.setSpacingBefore(20);
//        doc.add(table);
//        return this;
//    }
//
//
//    /**
//     * 添加神经反射表格
//     */
//    public PdfUtil createReflectTable(double[] result0, double[] result1, double[] result2, float hrDiff) throws DocumentException {
//        doc.add(Chunk.NEWLINE);//换行
//        Paragraph paragraph = new Paragraph("心脏自主神经反射实验", fontTitle2);
//        paragraph.setLeading(paragraph.getLeading() - 15);
//        paragraph.setSpacingBefore(20);
//        paragraph.setAlignment(Element.TITLE);//设置文字水平居中
//        doc.add(paragraph);
//        doc.add(Chunk.NEWLINE);//换行
//        int[] width = {20, 14, 14, 21, 11, 14, 12};
//        PdfPTable table2 = new PdfPTable(7);
//        table2.setWidths(width);
//        String[] type1 = {" ", "最长RR(s)", "最短RR(s)", "比值/心率差", "正常", "临界", "异常"};
//        //创建表格第一行
//        for (int i = 0; i < type1.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type1[i], fontText), cellHeight);
//            table2.addCell(index);
//        }
//        //------------------------------------------------------------------
//        DecimalFormat decimalFormat = new DecimalFormat("0.000#");
//        String result0_0 = result0[0] <= minValue ? "--" : decimalFormat.format(result0[0]);
//        String result0_1 = result0[1] <= minValue ? "--" : decimalFormat.format(result0[1]);
//        String result0_2 = result0[2] <= minValue ? "--" : decimalFormat.format(result0[2]);
//
//        String[] type2 = new String[]{"卧立位心率变化", result0_0,
//                result0_1, result0_2, "≥1.04", "1.01-1.03", "≤1.00"};
//        //创建表格第二行
//        for (int i = 0; i < type2.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type2[i], fontText), cellHeight);
//            table2.addCell(index);
//        }
//        //------------------------------------------------------------------
//        double[] result1_copy = result1.clone();
//        Arrays.sort(result1_copy);
//        String result1_max1, result1_max2, result1_max3;
//        String result1_min1, result1_min2, result1_min3;
//
//        if (result1.length < 3) {
//            result1_max1 = "--";
//            result1_max2 = "--";
//            result1_max3 = "--";
//            result1_min1 = "--";
//            result1_min2 = "--";
//            result1_min3 = "--";
//        } else {
//            result1_max1 = result1_copy[result1.length - 1] <= minValue ? "--" : result1_copy[result1.length - 1] + "";
//            result1_max2 = result1_copy[result1.length - 2] <= minValue ? "--" : result1_copy[result1.length - 2] + "";
//            result1_max3 = result1_copy[result1.length - 3] <= minValue ? "--" : result1_copy[result1.length - 3] + "";
//            result1_min1 = result1_copy[0] <= minValue ? "--" : result1_copy[0] + "";
//            result1_min2 = result1_copy[1] <= minValue ? "--" : result1_copy[1] + "";
//            result1_min3 = result1_copy[2] <= minValue ? "--" : result1_copy[2] + "";
//        }
//        String[] type3 = new String[]{"深呼吸心率差", result1_max1,
//                result1_min1, hrDiff + "", "≥15", "11-14", "≤10", result1_max2, result1_min2, result1_max3, result1_min3};
//        //创建表格第三行
//        for (int i = 0; i < type3.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type3[i], fontText), cellHeight);
//            if (i == 0 || i == 3 || i == 4 || i == 5 || i == 6) {
//                index.setRowspan(3);
//            }
//            table2.addCell(index);
//        }
//        //------------------------------------------------------------------
//        double[] result2_copy = result2.clone();
//        Arrays.sort(result2_copy);
//        String result2_max1, result2_max2, result2_max3;
//        String result2_min1, result2_min2, result2_min3;
//        double avg2_max, avg2_min;
//        String avg2;
//
//        if (result2.length < 3) {
//            result2_max1 = "--";
//            result2_max2 = "--";
//            result2_max3 = "--";
//            result2_min1 = "--";
//            result2_min2 = "--";
//            result2_min3 = "--";
//            avg2 = "--";
//        } else {
//            result2_max1 = result2_copy[result2.length - 1] <= minValue ? "--" : result2_copy[result2.length - 1] + "";
//            result2_max2 = result2_copy[result2.length - 2] <= minValue ? "--" : result2_copy[result2.length - 2] + "";
//            result2_max3 = result2_copy[result2.length - 3] <= minValue ? "--" : result2_copy[result2.length - 3] + "";
//            result2_min1 = result2_copy[0] <= minValue ? "--" : result2_copy[0] + "";
//            result2_min2 = result2_copy[1] <= minValue ? "--" : result2_copy[1] + "";
//            result2_min3 = result2_copy[2] <= minValue ? "--" : result2_copy[2] + "";
//            avg2_max = (result2_copy[result2.length - 1] + result2_copy[result2.length - 2] + result2_copy[result2.length - 3]) / 3;
//            avg2_min = (result2_copy[0] + result2_copy[1] + result2_copy[2]) / 3;
//            avg2 = String.format("%.3f", avg2_max / avg2_min);
//        }
//
//        String[] type4 = new String[]{"Valsalva动作指数", result2_max1,
//                result2_min1, avg2, "≥1.21", "1.11-1.20", "≤1.10", result2_max2, result2_min2, result2_max3, result2_min3};
//        //创建表格第四行
//        for (int i = 0; i < type4.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type4[i], fontText), cellHeight);
//            if (i == 0 || i == 3 || i == 4 || i == 5 || i == 6) {
//                index.setRowspan(3);
//            }
//            table2.addCell(index);
//        }
//        table2.setWidthPercentage(85);
//        doc.add(table2);
//        return this;
//    }
//
//    /**
//     * 添加神经反射表格
//     */
//    public PdfUtil createBPChangeTable(BloodPressure[] result) throws DocumentException {
//        if (result == null || result.length == 0) {
//            Log.e("PdfUtil", "为空");
//            result = new BloodPressure[]{new BloodPressure(), new BloodPressure(), new BloodPressure(), new BloodPressure(), new BloodPressure()};
            return this;
//        }
//
//        doc.add(Chunk.NEWLINE);
//        int[] width = {36, 12, 12, 12, 12, 12, 12};
//        PdfPTable table2 = new PdfPTable(7);
//        table2.setWidths(width);
//
//        String[] type1 = {" ", "卧位平均", "立位", "差值", "正常", "临界", "异常"};
//        //创建表格第一行
//        for (int i = 0; i < type1.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type1[i], fontText), cellHeight);
//            table2.addCell(index);
//        }
//        float avgSystolic = 0;//两次平均卧位收缩压
//        float avgDiastolic = 0;//两次平均平卧位舒张压
//        int systolic0 = 0;
//        int systolic1 = 0;
//        int systolic2 = 0;
//
//
//        int diastolic0 = 0;
//        int diastolic1 = 0;
//        int diastolic2 = 0;
//
//        int minSystolic0 = 0;
//        int minSystolic1 = 0;
//        int minSystolic2 = 0;
//        int minDiastolic0 = 0;
//        int minDiastolic1 = 0;
//        int minDiastolic2 = 0;
//        float diffS = 0F;
//        float diffD = 0F;
//        if (result.length == 1) {
//            avgSystolic = result[0].getSystolic();
//            systolic0 = result[0].getSystolic();
//            avgDiastolic = result[0].getDiastolic();
//            diastolic0 = result[0].getDiastolic();
//        }
//        if (result.length == 2) {
//            systolic0 = result[0].getSystolic();
//            systolic1 = result[1].getSystolic();
//            diastolic0 = result[0].getDiastolic();
//            diastolic1 = result[1].getDiastolic();
//            avgSystolic = (result[0].getSystolic() + result[1].getSystolic()) / 2F;
//            avgDiastolic = (result[0].getDiastolic() + result[1].getDiastolic()) / 2F;
//
//        }
//        if (result.length == 3) {
//            systolic0 = result[0].getSystolic();
//            systolic1 = result[1].getSystolic();
//            diastolic0 = result[0].getDiastolic();
//            diastolic1 = result[1].getDiastolic();
//            avgSystolic = (result[0].getSystolic() + result[1].getSystolic()) / 2F;
//            avgDiastolic = (result[0].getDiastolic() + result[1].getDiastolic()) / 2F;
//            minSystolic0 = result[2].getSystolic();
//            minDiastolic0 = result[2].getDiastolic();
//
//        }
//        if (result.length == 4) {
//            systolic0 = result[0].getSystolic();
//            systolic1 = result[1].getSystolic();
//            diastolic0 = result[0].getDiastolic();
//            diastolic1 = result[1].getDiastolic();
//            avgSystolic = (result[0].getSystolic() + result[1].getSystolic()) / 2F;
//            avgDiastolic = (result[0].getDiastolic() + result[1].getDiastolic()) / 2F;
//            minSystolic0 = result[2].getSystolic();
//            minSystolic1 = result[3].getSystolic();
//            minDiastolic0 = result[2].getDiastolic();
//            minDiastolic1 = result[3].getDiastolic();
//
//        }
//        if (result.length == 5) {
//            systolic0 = result[0].getSystolic();
//            systolic1 = result[1].getSystolic();
//            diastolic0 = result[0].getDiastolic();
//            diastolic1 = result[1].getDiastolic();
//            avgSystolic = (result[0].getSystolic() + result[1].getSystolic()) / 2F;
//            avgDiastolic = (result[0].getDiastolic() + result[1].getDiastolic()) / 2F;
//            int minS = Math.min(result[2].getSystolic(), result[3].getSystolic());
//            minSystolic0 = result[2].getSystolic();
//            minSystolic1 = result[3].getSystolic();
//            minSystolic2 = result[4].getSystolic();
//            minDiastolic0 = result[2].getDiastolic();
//            minDiastolic1 = result[3].getDiastolic();
//            minDiastolic2 = result[4].getDiastolic();
//        }
//        int minS = Math.min(Math.min(minSystolic0, minSystolic1), minSystolic2);
//        diffS = avgSystolic - minS;
//        int minD = Math.min(Math.min(minDiastolic0, minDiastolic1), minDiastolic2);
//        diffD = avgDiastolic - minD;
//        String avgS = avgSystolic <= minValue ? "--" : avgSystolic + "";
//        String avgD = avgDiastolic <= minValue ? "--" : avgDiastolic + "";
//        String difS = diffS + "";
//        String difD = diffD + "";
        String[] type2 = new String[]{"卧立位收缩压变化", avgSys,
                minSys, dif, "≤10", "11-29", "≥30"};
//        String[] type2 = new String[]{"卧立位收缩压变化(mmHg)", systolic0<=minValue?"--":systolic0+"",
//                minSystolic0 <= minValue ? "--" : minSystolic0 + "", difS, "≤10", "11-29", "≥30",systolic1<=minValue?"--":systolic1+"", minSystolic1 <= minValue ? "--" : minSystolic1 + "",systolic2<=minValue?"":systolic2+"", minSystolic2 <= minValue ? "--" : minSystolic2 + "", "卧立位舒张压变化(mmHg)", diastolic0<=minValue?"--":diastolic0+"",
//                minDiastolic0 <= minValue ? "--" : minDiastolic0 + "", difD, "--", "--", "≥10",diastolic1<=minValue?"--":diastolic1+"", minDiastolic1 <= minValue ? "--" : minDiastolic1 + "", diastolic2<=minValue?"":diastolic2+"",minDiastolic2 <= minValue ? "--" : minDiastolic2 + ""};
//        //创建表格第二行
//        for (int i = 0; i < type2.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type2[i], fontText), cellHeight);
//
//            if (i!=1 && i != 2 && i != 7 && i != 8 && i!=9 && i!=10&& i != 12&&i!=13 && i != 18 && i != 19 && i!=20&& i!=21) {
//                index = getChatCell(new Paragraph(type2[i], fontText), cellHeight);
//                index.setRowspan(3);
//            }
//            table2.addCell(index);
//        }
//
//
//        table2.setWidthPercentage(85);
//
//        doc.add(table2);
//        return this;
//    }
//
//    /**
//     * 添加心率变异性table
//     */
//    public PdfUtil createHrvTable(HrvResult hrvResult, String doctor, String time) throws DocumentException {
//        if (hrvResult == null) {
//            Log.d("TAG", "createHrvTable: 空");
            return this;
//            hrvResult = new HrvResult();
//        }
        doc.add(Chunk.NEWLINE);//换行
//        doc.add(Chunk.NEWLINE);//换行
//        Paragraph paragraph = new Paragraph("心率变异性时域与频域分析", fontTitle2);
//        //设置行间距
//        paragraph.setLeading(paragraph.getLeading() - 15);
//        paragraph.setSpacingBefore(20);
        paragraph.setExtraParagraphSpace(30);
//        paragraph.setAlignment(Element.TITLE);//设置文字水平居中
//        doc.add(paragraph);
//        doc.add(Chunk.NEWLINE);//换行
//        String hrv = hrvResult.getAvrHR() <= minValue ? "--" : hrvResult.getAvrHR() + BPM;
        Paragraph avgHrv = new Paragraph("平均静息心率:" + hrv, fontText);
//        paragraph.setLeading(paragraph.getLeading()-15);
//        paragraph.setExtraParagraphSpace(30);
        avgHrv.setAlignment(Element.ALIGN_LEFT);//设置文字水平居中
//        avgHrv.setLeading(avgHrv.getLeading()-30);
        //设置下方间距
        avgHrv.setSpacingAfter(5.0f);
        avgHrv.setIndentationLeft(43);
        doc.add(avgHrv);
//
        doc.add(Chunk.NEWLINE);//换行
//
//        //表格
//        PdfPTable table = new PdfPTable(4);
//        PdfPTable table1 = new PdfPTable(4);
//        //表格
//        PdfPTable table2 = new PdfPTable(4);
//
//        String NNVGR = hrvResult.getNNVGR() <= minValue ? "--" : hrvResult.getNNVGR() + MS;
//        String STDHR = hrvResult.getSTD_HR() + BPM;
//        String SDNN = hrvResult.getSDNN() <= minValue ? "--" : hrvResult.getSDNN() + MS;
//        String MAX_HR = hrvResult.getMaxHR() <= minValue ? "--" : hrvResult.getMaxHR() + BPM;
//        String SDANN = hrvResult.getSDANN() <= minValue ? "--" : hrvResult.getSDANN() + MS;
//        String MIN_HR = hrvResult.getMinHR() <= minValue ? "--" : hrvResult.getMinHR() + BPM;
//        String rMSSD = hrvResult.getrMSSD() <= minValue ? "--" : hrvResult.getrMSSD() + MS;
//        String SDSD = hrvResult.getSDSD() <= minValue ? "--" : hrvResult.getSDSD() + MS;
//        String SDNN_Index = hrvResult.getSDNN_Index() <= minValue ? "--" : hrvResult.getSDNN_Index() + MS;
//        String PNN50 = hrvResult.getpNN50() < minValue ? "--" : hrvResult.getpNN50() + "%";
//        String TP = hrvResult.getTP() <= minValue ? "--" : hrvResult.getTP() + MS_2;
//        String VLF = hrvResult.getVLF() <= minValue ? "--" : hrvResult.getVLF() + MS_2;
//        String LF = hrvResult.getLF() <= minValue ? "--" : hrvResult.getLF() + MS_2;
//        String HF = hrvResult.getHF() <= minValue ? "--" : hrvResult.getHF() + MS_2;
//        String LF_HF = hrvResult.getLF_HF_RATIO() <= minValue ? "--" : hrvResult.getLF_HF_RATIO() + "";
//        String VLF_HF = hrvResult.getVLF_HF_RATIO() <= minValue ? "--" : hrvResult.getVLF_HF_RATIO() + "";
//
//        String[] type = {"基本参数", "平均静息心率", hrv, "MAX HR", MAX_HR,
//                "STD HR", STDHR, "MIN HR", MIN_HR,
//                 "NNVGR", NNVGR, "", ""};
//        String[] type1 = {"时域分析",
//                "SDNN", SDNN, "RMSSD", rMSSD,
//                "SDANN", SDANN, "SDSD", SDSD,
//                "SDNN index", SDNN_Index, "pNN50", PNN50};
//        String[] type2 = {"频域分析", "TP", TP, "VLF", VLF,
//                "LF", LF, "HF", HF,
//                "LF/HF", LF_HF + "", "VLF/HF", VLF_HF};
//        for (int i = 0; i < type.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type[i], fontText),cellHeight);
//            if (i % 2 == 0) {
//                index = getChatCell(new Paragraph(type[i], fontText),cellHeight);
//            }
//            if (i == 0) {
//                index = getChatCell(new Paragraph(type[i], fontTitle3),cellHeight);
//                index.setColspan(4);
//            }
//            table.addCell(index);
//        }
//        for (int i = 0; i < type1.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type1[i], fontText), cellHeight);
//            if (i % 2 == 0) {
//                index = getChatCell(new Paragraph(type1[i], fontText), cellHeight);
//            }
//            if (i == 0) {
//                index = getChatCell(new Paragraph(type1[i], fontTitle3), cellHeight);
//                index.setColspan(4);
//            }
//            table1.addCell(index);
//        }
//        for (int i = 0; i < type2.length; i++) {
//            PdfPCell index = getChatCell(new Paragraph(type2[i], fontText3), cellHeight);
//            if (i % 2 == 0) {
//                index = getChatCell(new Paragraph(type2[i], fontText3), cellHeight);
//            }
//            if (i == 0) {
//                index = getChatCell(new Paragraph(type2[i], fontTitle3), cellHeight);
//                index.setColspan(4);
//            }
//            table2.addCell(index);
//        }
//        table.setWidthPercentage(85);
//        table1.setWidthPercentage(85);
//        table2.setWidthPercentage(85);
//        doc.add(table);
//        doc.add(table1);
//
        doc.add(Chunk.NEWLINE);//换行
//        doc.add(table2);
//        createDoctorInfo(doctor, time);
//        return this;
//    }
//
//    public PdfUtil createAllRR(int lieStandSelectMaxRRPos, int lieStandSelectMinRRPos, double[] allLieStandRR, double[] allDeepRR, double[] allValsalvaRR) throws DocumentException {
//        String[] rrType = {"卧立位所有RR间期", "深呼吸所有RR间期", "Valsalva所有RR间期"};
//        Log.d("PdfUtil", "allLieStandRR长度=" + allLieStandRR.length + "allDeepRR长度=" + allDeepRR.length + "allValsalvaRR长度=" + allValsalvaRR.length);
//        double[] allLieStandRRCopy = allLieStandRR.clone();
//        Arrays.sort(allLieStandRRCopy);
//        double[] allDeepCopy = allDeepRR.clone();
//        Arrays.sort(allDeepCopy);
//        double[] allValsalvaRRCopy = allValsalvaRR.clone();
//        Arrays.sort(allValsalvaRRCopy);
//        double[][] allRR = {allLieStandRR, allDeepRR, allValsalvaRR};
//        double[][] allRRCopy = {allLieStandRRCopy, allDeepCopy, allValsalvaRRCopy};
//        for (int i = 0; i < rrType.length; i++) {
//            if (allRR[i].length == 0) {
//                continue;
//            }
//            doc.newPage();
//            Paragraph avgHrv = new Paragraph(rrType[i], fontTitle2);
//            avgHrv.setAlignment(Element.TITLE);//设置文字水平居中
//            //设置下方间距
//            avgHrv.setSpacingAfter(15.0f);
//            doc.add(avgHrv);
//            PdfPTable table = new PdfPTable(3);
//            String[] type = {
//                    "",
//                    "RR间期(s)",
//                    "备注"
//
//            };
//            for (int j = 0; j < 3; j++) {
//                PdfPCell index = getChatCell(new Paragraph(type[j], fontTitle3));
//                table.addCell(index);
//            }
//            boolean rrMax1 = false, rrMax2 = false, rrMax3 = false, rrMin1 = false, rrMin2 = false, rrMin3 = false;
//
//            for (int k = 0; k < allRR[i].length; k++) {
//
//                PdfPCell index1 = getChatCell(new Paragraph((k + 1) + "", fontText));
//                table.addCell(index1);
//                PdfPCell index2 = getChatCell(new Paragraph(allRR[i][k] + "", fontText));
//                table.addCell(index2);
//                if (i == 0) {
//                    if (lieStandSelectMaxRRPos == k) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最长", fontText));
//                        table.addCell(index3);
                      rrMax1 = true;
//                    } else if (lieStandSelectMinRRPos == k) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最短", fontText));
//                        table.addCell(index3);
                        rrMin1 = true;
//                    } else {
//                        PdfPCell index3 = getChatCell(new Paragraph("", fontText));
//                        table.addCell(index3);
//
//                    }
//
//                } else {
//                    if (allRR[i].length < 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("", fontText));
//                        table.addCell(index3);
//                        continue;
//                    }
//                    if (allRRCopy[i][allRRCopy[i].length - 1] == allRR[i][k] && !rrMax1 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最长1", fontText));
//                        table.addCell(index3);
//                        rrMax1 = true;
//                    } else if (allRRCopy[i][allRRCopy[i].length - 2] == allRR[i][k] && !rrMax2 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最长2", fontText));
//                        table.addCell(index3);
//                        rrMax2 = true;
//                    } else if (allRRCopy[i][allRRCopy[i].length - 3] == allRR[i][k] && !rrMax3 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最长3", fontText));
//                        table.addCell(index3);
//                        rrMax3 = true;
//                    } else if (allRRCopy[i][0] == allRR[i][k] && !rrMin1 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最短1", fontText));
//                        table.addCell(index3);
//                        rrMin1 = true;
//                    } else if (allRRCopy[i][1] == allRR[i][k] && !rrMin2 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最短2", fontText));
//                        table.addCell(index3);
//                        rrMin2 = true;
//                    } else if (allRRCopy[i][2] == allRR[i][k] && !rrMin3 && allRR[i].length >= 6) {
//                        PdfPCell index3 = getChatCell(new Paragraph("最短3", fontText));
//                        table.addCell(index3);
//                        rrMin3 = true;
//                    } else {
//                        PdfPCell index3 = getChatCell(new Paragraph("", fontText));
//                        table.addCell(index3);
//                    }
//                }
//
//
//            }
//
//
//            table.setWidthPercentage(85);
//            doc.add(table);
//
//
//        }
//
//        return this;
//    }
//
//    /**
//     * 结论
//     */
//    public PdfUtil createConclusion(String conclusion, String doctor, String time) throws DocumentException {
//        PdfPTable table = new PdfPTable(4);
//        String[] type = {
//                "结论:",
//                conclusion,
//                "检查医生:",
//                doctor,
//                "检查时间:",
//                time,
//                "注:输出的临床结果仅用于辅助医生诊断糖尿病心脏自主神经病变,最终结果由医生结合临床综合判断并签字确认后生效。"
//        };
//
//        // 设置表格默认为无边框
//        table.getDefaultCell().setBorder(0);
//        for (int i = 0; i < type.length; i++) {
//            Paragraph userInfo = new Paragraph(type[i], fontTitle3);
//            if (i == 1 || i == 3 || i == 5) {
//                userInfo = new Paragraph(type[i], fontText);
//            }
//            if (i == 6) {
//                userInfo = new Paragraph(type[i], fontText2);
//            }
//
//            PdfPCell cell = new PdfPCell(userInfo);
//            cell.setBorder(Rectangle.NO_BORDER);
//            if (i == 0 || i == 1 || i == 6) {
//                cell.setColspan(4);
//            }
//            if (i == 1) {
//                cell.setFixedHeight(50);
//            } else {
//                cell.setFixedHeight(45);
//            }
//            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//
//            // 设置垂直居中
//            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//            table.addCell(cell);
//        }
//
//        table.setWidthPercentage(85);
//        doc.add(table);
//        return this;
//    }
//
//    /**
//     * 医生信息
//     */
//    private PdfUtil createDoctorInfo(String doctor, String time) throws DocumentException {
//        PdfPTable table = new PdfPTable(4);
//        String[] type = {
//                "检查医生:",
//                doctor,
//                "检查时间:",
//                time
//        };
//
//        // 设置表格默认为无边框
//        table.getDefaultCell().setBorder(0);
//        for (int i = 0; i < type.length; i++) {
//            Paragraph userInfo = new Paragraph(type[i], fontTitle3);
//            if (i == 1 || i == 3) {
//                userInfo = new Paragraph(type[i], fontText);
//            }
//
//
//            PdfPCell cell = new PdfPCell(userInfo);
//            cell.setBorder(Rectangle.NO_BORDER);
//
//            cell.setFixedHeight(25);
//
//            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//
//            // 设置垂直居中
//            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//            table.addCell(cell);
//        }
//
//        table.setWidthPercentage(85);
//        table.setSpacingBefore(250);
//        doc.add(table);
//        return this;
//    }
//
    String imgPath;

    //
//    public PdfUtil createEcgImg(int[] page, int[] time, int[] gain, String path, float scale) throws Exception {
//        imgPath = path;
//        String[] ecgType = {"卧立位心电图", "深呼吸心电图", "Valsalva心电图"};
//        for (int i = 0; i < page.length; i++) {
//            Log.e("PdfUtil", "page0size=" + page[i]);
//            if (page[i] == 0) {
//                continue;
//            }
//            doc.newPage();
//            Paragraph paragraph = new Paragraph(ecgType[i], fontTitle2);
//            paragraph.setLeading(paragraph.getLeading() - 15);
//            paragraph.setAlignment(Element.TITLE);//设置文字水平居中
//            doc.add(paragraph);
//            doc.add(Chunk.NEWLINE);//换行
//            Paragraph paragraph2 = new Paragraph("增益:" + gain[i] + "mm/mV 走速:25mm/s 时长:" + DateUtils.INSTANCE.formatEcgDuration(time[i]), fontText2);
//            paragraph2.setAlignment(Element.ALIGN_RIGHT);//设置文字水平居中
            paragraph2.setLeading(paragraph.getLeading() - 15);
//            paragraph2.setIndentationRight(30);
//            doc.add(paragraph2);
//            float imgWidth = PageSize.A4.getWidth() - 60f;
//            float imgHeight = imgWidth / scale;
//            doc.add(Chunk.NEWLINE);//换行
//            for (int k = 0; k < page[i]; k++) {
//                Image image1 = Image.getInstance(path + "/img" + i + "_" + k + ".png");
//                // 按百分比缩放
//                //image1.scalePercent(30);
//                image1.scaleAbsolute(imgWidth, imgHeight);
//                Log.d("PdfUtil", "createEcgImg: " + PageSize.A4.getWidth());
//                image1.setAlignment(Image.MIDDLE);
//                doc.add(image1);
//            }
//            doc.newPage();
//        }
//
        fos.flush();
        if (doc != null) {
            doc.close();
        }
//
//        return this;
//
//    }
    public void build() throws Exception {
        dispose();
    }

    private void deleteAllImgs() {
        File file = new File(imgPath);
        File[] imgs = file.listFiles();
        for (int i = 0; i < Objects.requireNonNull(imgs).length; i++) {
            imgs[i].delete();
        }
    }

    public void dispose() throws Exception {
        if (doc != null && doc.isOpen()) {
            doc.close();
        }
        if (fos == null)
            return;
        fos.flush();
        fos.close();
        if (imgPath != null) {
            deleteAllImgs();
        }
    }
//
//    /**
//     * 获取每个单元格
//     *
//     * @param paragraph
//     * @return
//     */
//    private PdfPCell getChatCell(Paragraph paragraph, int height) {
//        PdfPCell cell2 = new PdfPCell(paragraph);
//        // 设置高度
//        cell2.setFixedHeight(height);
//        // 设置内容水平居中显示
//        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
//        // 设置垂直居中
//        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
//        cell2.setBorderColor(new BaseColor(0, 0, 0, 125));
//
//        return cell2;
//    }
//
//    /**
//     * 获取每个单元格
//     *
//     * @param paragraph
//     * @return
//     */
//    private PdfPCell getChatCell(Paragraph paragraph) {
//        return getChatCell(paragraph, 20);
//    }
//
//    private static PdfPCell getChatCell(Paragraph paragraph, int height, boolean showBack, boolean isCenter) {
//        PdfPCell cell2 = new PdfPCell(paragraph);
//        // 设置高度
//        cell2.setFixedHeight(height);
//        // 设置内容水平居中显示
//        if (isCenter) {
//            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
//        } else {
//            cell2.setPaddingLeft(5);
//        }
//        // 设置垂直居中
//        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
//        cell2.setBorderColor(new BaseColor(0, 0, 0, 125));
//        if (showBack)
//            cell2.setBackgroundColor(new BaseColor(0, 0, 0, 35));
//        return cell2;
//    }

    /**
     * 设置PDF字体(较为耗时)
     */
    public Font setChineseFont(int size, int style, String fontPath) {
        BaseFont bf = null;
        Font fontChinese = null;
        try {
            // STSong-Light : Adobe的字体
            // UniGB-UCS2-H : pdf 字体
            //G:\AndroidProject\dcan-android\app\src\main\assets
            if (fontPath != null && new File(fontPath).exists()) {
                bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED);
            } else {
                bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                        BaseFont.NOT_EMBEDDED);
            }
            fontChinese = new Font(bf, size, style);
        } catch (Exception e) {
            e.printStackTrace();
            return new Font();
        }
        return fontChinese;
    }

    /**
     * 设置PDF字体(较为耗时)
     */
    public Font setChineseFont2(int size, int style, String fontPath) {
        BaseFont bf = null;
        Font fontChinese = null;
        try {
            // STSong-Light : Adobe的字体
            // UniGB-UCS2-H : pdf 字体
            if (fontPath != null) {
                bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED);
            } else {
                bf = BaseFont.createFont("wusheng", BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED);
            }
            fontChinese = new Font(bf, size, style);
        } catch (Exception e) {
            e.printStackTrace();
            return new Font();
        }
        return fontChinese;
    }

    /*
     * HeaderAndFooter class
     */
    public class HeaderAndFooter extends PdfPageEventHelper {
        
        public void onEndPage(PdfWriter writer, Document document) {
            Font headFont = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED, 9, Font.NORMAL, BaseColor.BLACK);
            //添加标题文本
            StringBuffer underline = new StringBuffer();
            for (int i = 0; i < 116; i++) {
                underline.append("_");
            }
//            Phrase contentPh = new Phrase("深圳市跃瑞医疗科技有限公司", headFont);
//            Phrase underlinePh = new Phrase(underline.toString(), headFont);
            Phrase pageNumberPh = new Phrase(String.valueOf(writer.getPageNumber()), headFont);
            float center = doc.getPageSize().getRight() / 2;//页面的水平中点
//            float top = doc.getPageSize().getTop() - 15;
            float bottom = doc.getPageSize().getBottom();
            /** 参数xy是指文本显示的页面上的哪个店。alignment指文本在坐标点的对齐方式 */
//            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, contentPh, center, top, 0); //页眉
//            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, underlinePh, center, top - 3, 0); //页眉
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, pageNumberPh, center, bottom, 0); //页码
        }
    }
}