首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java: itext 7.2.1 using jdk 17.0.1

java: itext 7.2.1 using jdk 17.0.1

作者头像
geovindu
发布2026-06-19 09:40:21
发布2026-06-19 09:40:21
820
举报
代码语言:javascript
复制
  /**
     *
     *
     *
     * */
   public static PdfFont getDefaultFont() {
       try {
//         return PdfFontFactory.createFont("C://windows//fonts//simsun.ttc,1", PdfEncodings.IDENTITY_H, false);// 引用系统字体
           return PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");// 使用itext字体
       } catch (IOException e) {
           // 记录日志
           e.printStackTrace();
       }
       return null;
   }
     
    /**
     *
     *
     *
     * */
   public static PdfFont getImportFont(String fontName) {
       // 获取resource下文件夹路径
       String path = "src/resource/";
       String fontPath = path + fontName;
       try {
           // 处理中文乱码(支持Linux系统)
           //FontProgram fontProgram = FontProgramFactory.createFont(fontPath, false);
           PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H);
           return font;
       } catch (IOException e) {
           // 记录日志
           e.printStackTrace();
       }
       return null;
   }
 
     
   PdfFont hfFont;
   {
       try {
           hfFont =PdfFontFactory.createFont(path);// new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
  
   /**
    * 图片水印
    *
    * */
   public static void addImageToPDFDU(String PDFPath){     
        
       try{    
             
 
           // PdfWriter writer = new PdfWriter(new File(PDFPath));            //new FileOutputStream();
            // writer = new PdfWriter(new File(PDFPath));
           // PdfDocument pdfDoc = new PdfDocument(writer);
  
  
  
           // PdfWriter pdfwriter = PdfWriter(new File(PDFPath);
            PdfDocument pdf = new PdfDocument(new PdfWriter(PDFPath));
            PageSize pageSize = new PageSize(PageSize.A4).rotate();
 
            //设置文档属性
            pdf.getDocumentInfo().setAuthor("涂聚文 Geovin Du");
            pdf.getDocumentInfo().setTitle("iText7 java测试PDF");
            pdf.getDocumentInfo().setSubject("涂聚文公司");
            pdf.getDocumentInfo().setMoreInfo("Geovin Du","1201");
            pdf.getDocumentInfo().setCreator("geovindu 涂聚文");
            pdf.getDocumentInfo().setKeywords("iText,涂聚文 塗聚文 Geovin Du");
             
             
            Document document = new Document(pdf,PageSize.A4,true);
 
            TextFooterEventHandler eventHandler = new TextFooterEventHandler(document);
            pdf.addEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
            for (int i = 0; i < 12; i++) {
                document.add(new Paragraph("Test " + i + " 涂聚文 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.").setFont(getDefaultFont()));
            }
 
            // the currently last page is also the last page of the document, so inform the event listener
            eventHandler.lastPage = pdf.getLastPage();
             
             
            PdfPage page1 = pdf.addNewPage();
            PdfCanvas canvas = new PdfCanvas(page1);
  
  
            // creating image data instance by passing the path to image
            ImageData img = ImageDataFactory.create("src/resource/gogo.png");
            canvas.saveState();
            // graphic state
            PdfExtGState state = new PdfExtGState();
            state.setFillOpacity(0.2f);
            canvas.setExtGState(state);
            canvas.addImageAt(img, 20, pageSize.getWidth()/2, false); //650,
            canvas.restoreState();
            document.add(new Paragraph("Adding image to PDF Example,涂聚文").setFont(getDefaultFont()));
             
            pdf.addNewPage();
            
            PdfFont headerFont = PdfFontFactory.createFont(path);
            PdfFont cellFont = PdfFontFactory.createFont(path);
  
      
           Table table = new Table(new float[]{4, 4, 4});
           table.setWidth(UnitValue.createPercentValue(100));
           // adding header
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "姓").setFont(headerFont)));
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "名").setFont(headerFont)));
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "区域名称").setFont(headerFont)));         
             
             
     
           table.addCell(new Paragraph("1.0 geovindu").setFont(cellFont));
           table.addCell(new Paragraph("1.1 涂聚文").setFont(cellFont));
           table.addCell(new Paragraph("1.2 上海市").setFont(cellFont));
            
           table.addCell(new Paragraph("2.1 北京市").setFont(cellFont));
           table.addCell(new Paragraph("2.2 重庆市").setFont(cellFont));
           table.addCell(new Paragraph("2.3 宜春市").setFont(cellFont));
             
           document.add(table);
             
             
             
           document.close();
             
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }

/**  * iText 7.2.1  * bouncycastle 1.7.0  * slf4j-api-1.6.1  * slf4j-log4j12-1.6.1  * log4j-1.2.16  *   * */

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档