使用itextpdf操作pdf的实例讲解

作者:悠闲咖啡007 时间:2022-11-16 00:22:43 

使用maven引入jar


<dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itextpdf</artifactId>
 <version>5.2.0</version>
</dependency>
<dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itext-asian</artifactId>
 <version>5.2.0</version>
</dependency>

创建exportToPDF工具类


package com.os.core.util.file.exportPdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.FontSelector;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* Created by PengSongHe on 2016/10/20 0020.
*/
public class exportToPDF {
 public static void main(String[] args) {
   try {
     Document document = new Document();//默认A4
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d:/HelloWorld.pdf"));
     writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
     document.addCreationDate();
     document.addCreator("飞翔家族");
     document.addTitle("export pdf");
     document.addKeywords("export");
     document.addSubject("飞翔家族 export pdf");
     document.open();
//处理中文,需要itextasian.jar支持
     FontSelector selector = new FontSelector();
     selector.addFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 12));
     selector.addFont(FontFactory.getFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
     Phrase phrase = selector.process("HelloWord!你好。\u4fdd\u5b58");
     document.add(new Paragraph(phrase));
     //添加新页
     //document.newPage();
     //writer.setPageEmpty(false);
     //document.add(new Paragraph("New page"));
     document.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (DocumentException e) {
     e.printStackTrace();
   }
 }
}

来源:http://blog.csdn.net/psh18513234633/article/details/78893818

标签:itextpdf,操作,pdf
0
投稿

猜你喜欢

  • RabbitMQ延迟队列及消息延迟推送实现详解

    2023-11-15 15:31:31
  • netty pipeline中的inbound和outbound事件传播分析

    2023-08-27 06:57:00
  • java.lang.NoClassDefFoundError错误解决办法

    2021-12-29 03:52:27
  • Java通过PropertyDescriptor反射调用set和get方法

    2023-10-11 19:34:17
  • java字符串相似度算法

    2023-11-26 12:33:25
  • SpringBoot整合screw实现数据库文档自动生成的示例代码

    2023-11-29 05:30:15
  • Spring Boot thymeleaf模板引擎的使用详解

    2022-07-27 07:42:54
  • java+jsp+struts2实现发送邮件功能

    2023-08-28 18:25:27
  • java生成jar包并且单进程运行的实例

    2021-08-30 00:40:55
  • 微服务通过Feign调用进行密码安全认证操作

    2023-07-30 02:43:38
  • SpringBoot2零基础到精通之映射与常用注解请求处理

    2022-06-11 15:41:51
  • SSM Mapper文件查询出返回数据查不到个别字段的问题

    2023-10-17 01:02:08
  • java高并发ScheduledThreadPoolExecutor与Timer区别

    2023-08-11 03:08:29
  • Java中synchronized正确使用方法解析

    2021-12-20 13:00:39
  • 基于RxJava实现酷炫启动页

    2023-09-26 21:50:47
  • MybatisPlus如何自定义TypeHandler映射JSON类型为List

    2023-08-08 14:05:38
  • 浅谈spring的重试机制无效@Retryable@EnableRetry

    2021-12-12 10:42:57
  • Mybatis是这样防止sql注入的

    2022-05-30 02:05:16
  • Mybatis如何通过接口实现sql执行原理解析

    2022-11-30 11:31:26
  • java获取包下被指定注解的类过程解析

    2023-08-08 11:12:27
  • asp之家 软件编程 m.aspxhome.com