java生成pdf

java生成pdf(完整版)

package websm.cn;

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 java.io.*;
import java.util.Map;
import java.util.Set;


/**
 * @Author: [seim]
 * @emil: [webseim@126.com]
 * @ClassName creatPdf
 * @date 2022/3/25 16:26
 * @Version 15
 */
public class creatPdf {
    //定义全局字体静态变量
    private static Font titleFont;
    //定义全局字体静态变量
    private static Font textFont;
    //最大宽度
    private static int maxWidth = 480;

    static {
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titleFont = new Font(bfChinese, 16, Font.NORMAL);
            textFont = new Font(bfChinese, 16, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void generatePdf() {
        File file = new File(File.separator);
        try {
            //    建立document文档
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            //建立书写器
            file.createNewFile();
            PdfWriter.getInstance(document, new FileOutputStream(file));

            //打开文档
            document.open();
            document.addTitle("标题");
            document.addAuthor("作者");
            document.addSubject("主题");
            document.addKeywords("关键字");
            document.addCreator("创建者");

            //在文档中创建内容
            createPdfFile(document); //后面添加信息
            //关闭文档
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * pdf样式模板
     * @param document
     * @throws DocumentException
     * @throws IOException
     */
    private void createPdfFile(Document document) throws DocumentException, IOException {
        //段落
        Paragraph paragraph = titleText("标题");
        PdfPTable table = CreateTable(new float[]{100, 140, 100, 140});
        setTableCellInfo(table); //后面信息

        //添加印章  路径项目资源下
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[8192];
        int read;
        while ((read = is.read(buf, 0, buf.length)) >= 0) {
            out.write(buf, 0, read);
        }

        Image image = Image.getInstance(out.toByteArray());
        image.setAbsolutePosition(400, 260); //图片的绝对位置
        image.scaleAbsolute(100, 100);

        //说明
        Paragraph p = createParagraphNoInde();
        Phrase ph = new Paragraph("说明", textFont);
        Phrase ph1 = new Paragraph("重要说明............................", textFont);

        p.add(ph);
        p.add(ph1);

        document.add(paragraph);// 段落信息
        document.add(table); //客户信息
        document.add(image); //图片
        document.add(p); //说明文字

    }

    /**
     * 表格信息表
     *
     * @param table //Info info
     */
    private void setTableCellInfo(PdfPTable table) {
        table.addCell(createCell("三千业务", textFont, Element.ALIGN_CENTER, 4));
        table.addCell(createCell("姓名", textFont));
        table.addCell(createCell("性别", textFont));
        table.addCell(createCell("年龄", textFont));

       /* //补充字段
        Map<String ,Map<String, String>> addText=info.getAddText();
        if (null!=addText){
            Map<String, String> nameInfo = addText.get("nameInfo");
            if (null!=nameInfo){
                Set strings = nameInfo.keySet();
                for (int i=1;i< strings.size()/2+1;i++){
                    table.addCell(createCell(nameInfo.get("key"+1), textFont));
                    table.addCell(createCell(nameInfo.get("value"+1), textFont));
                }
            }
        }*/
    }

    /**
     * 标题文字
     *
     * @param text
     * @return
     */
    private Paragraph titleText(String text) {
        Paragraph paragraph = new Paragraph(text, titleFont);
        paragraph.setAlignment(1); //设置文字居中 0靠左 1居中 2靠右
        paragraph.setIndentationLeft(12); //设置左缩进
        paragraph.setIndentationRight(12); //设置右缩进
        paragraph.setFirstLineIndent(24); //设置首行缩进
        paragraph.setLeading(20F); // 设置行间距
        paragraph.setSpacingBefore(5F); // 设置段落上空白
        paragraph.setSpacingAfter(10F); // 设置段落下空白
        return paragraph;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable CreateTable(float[] widths) {
        PdfPTable pdfPTable = new PdfPTable(widths);
        try {
            pdfPTable.setTotalWidth(maxWidth);
            pdfPTable.setLockedWidth(true);
            pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfPTable.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdfPTable;
    }

    /**
     * 创建单元格 (指定字符,水平...)
     *
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setMinimumHeight(25);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格 (指定字符,水平...)
     *
     * @param value
     * @param align
     * @param colspan
     * @return
     */
    public static PdfPCell createCell(Paragraph value, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setMinimumHeight(25);
        cell.setPhrase(value);
        return cell;
    }

    /**
     * 创建单元格 (指定字符)
     *
     * @param value
     * @param font
     * @return
     */
    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setMinimumHeight(25);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格(指定字符)
     *
     * @return
     */
    public static Paragraph createParagraphNoInde() {
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(0); //设置文字居中 0靠左 1居中 2靠右
        paragraph.setIndentationLeft(12);//左缩进
        paragraph.setIndentationRight(12);//右缩进
        paragraph.setFirstLineIndent(0);//设置首行缩进
        paragraph.setLeading(20F);//设置行间距
        return paragraph;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值