java: itext 7.2.1 using jdk 14.0.2

java: itext 7.2.1 using jdk 14.0.2

 

 /**
	      * 获取统一字体
	      * 
	      * **/
	    public static PdfFont getPdfFont(){
	        PdfFont pdfFont = null;
	        try {
	            pdfFont = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return pdfFont;
	    }
	    
	    
	    /**
	     * 生成简单PDF
	     * 涂聚文 Geovin Du
	     * 
	     * */
	    public static void duPdf(){
	        //语法糖
	        try (PdfWriter writer = new PdfWriter(new File("src/geoivndu4.pdf"));
	             PdfDocument pdf = new PdfDocument(writer);
	             Document document = new Document(pdf, PageSize.A4.rotate());
	        ){

	            //设置文档属性
	            pdf.getDocumentInfo().setAuthor("涂聚文");
	            pdf.getDocumentInfo().setTitle("IText测试PDF");
	            pdf.getDocumentInfo().setSubject("涂聚文公司");
	            pdf.getDocumentInfo().setMoreInfo("涂聚文","111");
	            pdf.getDocumentInfo().setCreator("geovindu");
	            pdf.getDocumentInfo().setKeywords("IText涂聚文");

	            //注册事件监听
	            pdf.addEventHandler(PdfDocumentEvent.END_PAGE, new DuEventHandler());

	            //设置字体
	            document.setFont(getPdfFont());

	            //页边距
	            document.setMargins(20, 20, 20, 20);

	            //简单文字
	            document.add(new Paragraph("简单文字"));
	            document.add(new Paragraph("Hello Word!").add(new Tab()).add(new Text("你好!").addStyle(new Style().setFontSize(24))));

	            //简单图片
	            document.add(new Paragraph("简单图片"));
	            document.add(new Image(ImageDataFactory.create("src/resource/geovindulogo.jpg")));

	            //简单表格
	            document.add(new Paragraph("简单表格"));
	            Table table = new Table(new float[]{3, 3, 4});
	            PdfFont font = getPdfFont();
	            //标题、内容
	            process(table, "姓名;年龄;电话号码", font, true);
	            for (int i = 0; i < 5; i++) {
	                process(table, "涂聚文"+i+";"+(18+i)+";1500000000"+i, font, false);
	            }
	            document.add(table);

	            //超链接
	            document.add(new Paragraph("超链接"));
	            PdfLinkAnnotation annotation = new PdfLinkAnnotation(new Rectangle(0, 0));
	            annotation.setAction(PdfAction.createURI("https://itextpdf.com/"));
	            Paragraph p = new Paragraph("更多精彩内容,猛戳:").add(new Link("这里", annotation));
	            document.add(p);
	            
	            pdf.addNewPage();
	            //换一页
	            document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
	            pdf.addNewPage();
	            document.close();
	            System.out.println("操作完成!");

	        } catch (IOException e) {
	            e.printStackTrace();
	            System.err.println("操作异常...");
	        }
	    }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » java: itext 7.2.1 using jdk 14.0.2