java生成输出pdf文件(itextpdf)

这段代码展示了如何使用iText库在Java中创建PDF文档。首先,它下载了必要的jar包,然后创建一个PDF文档,设置了页面大小、页边距,并添加了内容。内容包括头部信息(如项目编号、项目名称等)、专家抽取表格和尾部数据。字体工具类提供了不同类型的字体排版。整个过程涉及到PDF的布局、表格创建和字体设置。
摘要由CSDN通过智能技术生成

1.下载jar

itext-asian-5.2.0.jar

itextpdf-5.5.13.jar

2.创建一个pdf文档,代码如下

    
public class CreatePdfText {
    public static void main(String[] args) {
        System.out.println("===========start=============");
        try {
            Document doc = createPdf("D://gitpath/caigou_wzgd/src/product/src/test/resources/test6.pdf");
            //生成  合同文件
            createFile(doc,new PdfHeadInfo());
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("===========end=============");
    }
    
    /**
     * 创建一个pdf并打开
     * @param outpath  pdf路径
     */
    public static Document createPdf(String outpath) throws DocumentException, IOException{
        //页面大小
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向
        //如果没有则创建
        File saveDir = new File(outpath);
        File dir = saveDir.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
        //PDF版本(默认1.4)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        //页边空白
        doc.setMargins(20, 20, 20, 20);
        //打开文档
        doc.open();
        return doc;
    }
    
    public static void createFile(Document doc,PdfHeadInfo pdfHeadInfo) throws DocumentException{
        createHead(doc,pdfHeadInfo);
        createExpertInfo(doc);
        createTail(doc);
    }
    /*
     * 创建PDF文件中尾部数据
     */
    private static void createTail(Document doc) throws DocumentException {
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell;
        //表头
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "注姓名带星号表示补抽专家")));
        cell.setBorder(0);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "招标人签字")));
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "代理人员签字")));
        cell.setBorder(0);
        table.addCell(cell);
        doc.add(table);
    }
    /*
     * 创建专家信息表格
     */
    private static void createExpertInfo(Document doc) throws DocumentException {
        PdfPTable table = new PdfPTable(11);
        table.setWidthPercentage(100);
        setExpertTableHead(table);
        //数据
        setExpertTableInfo(doc, table);
        setExpertTableInfo(doc, table);
        setExpertTableInfo(doc, table);
        setExpertTableInfo(doc, table);
        setExpertTableInfo(doc, table);
        doc.add(table);
    }
    /*
     * 专家抽取人员信息
     */
    private static void setExpertTableInfo(Document doc, PdfPTable table) throws DocumentException {
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "外部专家1")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "3101***8641X")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "男")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "显示与抽取方案一致的专业层级,而非实际专业")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "根据专家信息显示")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "")));
        setCellCenter(cell);
        table.addCell(cell);
    }
    /*
     * 专家抽取记录表头
     */
    private static void setExpertTableHead(PdfPTable table) {
        PdfPCell cell;
        //表头
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "姓名")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "身份证")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "性别")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "专业")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "职称")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "工作单位")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "签到")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "备注")));
        setCellCenter(cell);
        table.addCell(cell);
    }

    /*
     * 创建PDF文件中头部数据
     */
    private static void createHead(Document doc,PdfHeadInfo pdfHeadInfo) throws DocumentException {
        doc.add(PdfFontUtils.getFont(1, "这是标题"));
        doc.add(PdfFontUtils.getFont(2, "这是标题二"));
        PdfPTable table = new PdfPTable(6);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell;
        // 设置单元格样式
        //表头
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "项目编号")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "A1234")));
        setCellCenter(cell);
        cell.setColspan(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "项目名称")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "项目名称")));
        setCellCenter(cell);
        cell.setColspan(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "业主单位")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "公司")));
        setCellCenter(cell);
        cell.setColspan(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "代理单位")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "代理单位")));
        setCellCenter(cell);
        cell.setColspan(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "抽取终端")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "终端")));
        setCellCenter(cell);
        cell.setColspan(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "备注信息")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "第1次抽取")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "评标室")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "评标室")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "抽取方式")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "随机抽取")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "评标时间")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "2021年10月15日9点30分 ")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "外部专家要求:")));
        cell.setBorder(0);
        cell.setColspan(6);
        cell.setPaddingTop(8);
        cell.setPaddingBottom(8);;
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "是否仅本地")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "否")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "职称级别")));
        setCellCenter(cell);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "不限")));
        setCellCenter(cell);
        cell.setColspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfSTKAITIFontUtils.getFont(5, "抽取结果记录:")));
        cell.setBorder(0);
        cell.setColspan(6);
        cell.setPaddingTop(8);
        cell.setPaddingBottom(8);;
        table.addCell(cell);
        doc.add(table);
        doc.add(PdfSTKAITIFontUtils.getFont(6, ""));//表格和汉字空一行
        doc.add(PdfSTKAITIFontUtils.getFont(6, ""));//表格和汉字空一行
    }

    private static void setCellCenter(PdfPCell cell) {
        cell.setUseAscender(true);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setPaddingTop(8);
        cell.setPaddingBottom(8);;
    }
}

3.创建字体工具类

public class PdfFontUtils {
 // 字体
    private static BaseFont baseFont = null;
    
    static{
        try {
            /**
             * 设置字体
             *
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */
            //宋体
            baseFont = BaseFont.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
            
    /**
     * 文档超级  排版
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text){
        Font font = new Font(baseFont);
        if(1 == type){//1-标题
            font.setSize(22f);
//            font.setStyle(Font.BOLD);
        } else if(2 == type){//2-标题一
            font.setSize(18f);
//            font.setStyle(Font.BOLD);
        } else if(3 == type){//3-标题二
            font.setSize(14f);
            font.setStyle(Font.BOLD);
        } else if(4 == type){//4-标题三
            font.setSize(14f);
        } else if(5 == type){//5-正文
            font.setSize(10.5f);
        } else if(6 == type){//6-左对齐
            font.setSize(10.5f);
        } else if(7 == type){//7-项目信息
            font.setSize(12);
//            font.setStyle(Font.BOLD);
        }else if(8 == type){//8-专家信息
            font.setSize(11);
//          font.setStyle(Font.BOLD);
        }else if(9 == type){//9-专家名称
            font.setSize(14);
//          font.setStyle(Font.BOLD);
        }else {
            font.setSize(10.5f);//默认大小
        }
        //注: 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        if(1 == type){
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(2 == type){//2-标题一
            paragraph.setAlignment(Element.ALIGN_CENTER); //居中
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        } else if(3 == type){
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(4 == type){//4-标题三
            //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(5 == type){
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(6 == type){//左对齐
            paragraph.setAlignment(Element.ALIGN_LEFT);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(7 == type){//剧中
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        }else if(8 == type){//剧中
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(3f);//上间距
            paragraph.setSpacingAfter(3f);//下间距
        }else if(9 == type){//剧中
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(3f);//上间距
            paragraph.setSpacingAfter(3f);//下间距
        }
        return paragraph;
    }
}

public class PdfSTKAITIFontUtils {
 // 字体
    private static BaseFont baseFont = null;
    
    static{
        try {
            /**
             * 设置字体
             *
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */

          //华文开题
            baseFont = BaseFont.createFont("C:/Windows/fonts/STKAITI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
            
    /**
     * 文档超级  排版
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text){
        Font font = new Font(baseFont);
        if(1 == type){//1-标题
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(2 == type){//2-标题一
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(3 == type){//3-标题二
            font.setSize(14f);
            font.setStyle(Font.BOLD);
        } else if(4 == type){//4-标题三
            font.setSize(14f);
        } else if(5 == type){//5-正文
            font.setSize(10.5f);
        } else if(6 == type){//6-左对齐
            font.setSize(10.5f);
        } else if(7 == type){//7-加粗
            font.setSize(10.5f);
            font.setStyle(Font.BOLD);
        }else {
            font.setSize(10.5f);//默认大小
        }
        //注: 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        if(1 == type){
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        } else if(2 == type){//2-标题一
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(3 == type){
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(4 == type){//4-标题三
            //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(5 == type){
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(6 == type){//左对齐
            paragraph.setAlignment(Element.ALIGN_LEFT);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(7 == type){//剧中
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        }
        return paragraph;
    }

上述代码即可完成pdf文件

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值