栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java生成输出pdf文件(itextpdf)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java生成输出pdf文件(itextpdf)

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=============");
    }
    
   
    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);
    }
   
    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);
    }

   
    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 {
           
            //宋体
            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();
        }
    }
            
   
    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 {
           

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

        } catch (documentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
            
   
    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文件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号