当前位置: 首页 > news >正文

java生成PDF格式文档

需要的两个包及下载地址:

(1)iText.jar:http://download.csdn.net/source/296416

(2)iTextAsian.jar(用来进行中文的转换):http://download.csdn.net/source/172399

 

下面是代码示例:

 

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class ITextDemo {
    public boolean iTextTest() {
        try {
            /** 实例化文档对象 */
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            /** 创建 PdfWriter 对象 */
            PdfWriter.getInstance(document,// 文档对象的引用
                    new FileOutputStream("d://ITextTest.pdf"));//文件的输出路径+文件的实际名称
            document.open();// 打开文档

            /** pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 */
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document:

            /** 向文档中添加内容,创建段落对象 */
            document.add(new Paragraph("First page of the document."));// Paragraph添加文本
            document.add(new Paragraph("我们是害虫", FontChinese)); //FontChinese:为中文字体的设置

            /** 创建章节对象 */
            Paragraph title1 = new Paragraph("第一章", FontChinese);
            Chapter chapter1 = new Chapter(title1, 1);
            chapter1.setNumberDepth(0);
            /** 创建章节中的小节 */
            Paragraph title11 = new Paragraph("表格的添加", FontChinese);
            Section section1 = chapter1.addSection(title11);
            /** 创建段落并添加到小节中 */
            Paragraph someSectionText = new Paragraph("下面展示的为3 X 2 表格.",
                    FontChinese);
            section1.add(someSectionText);

            /** 创建表格对象(包含行列矩阵的表格) */
            Table t = new Table(3, 2);// 2行3列
            t.setBorderColor(new Color(220, 255, 100));
            t.setPadding(5);
            t.setSpacing(5);
            t.setBorderWidth(1);
            Cell c1 = new Cell(new Paragraph("第一格", FontChinese));
            t.addCell(c1);
            c1 = new Cell("Header2");
            t.addCell(c1);
            c1 = new Cell("Header3");
            t.addCell(c1);
            // 第二行开始不需要new Cell()
            t.addCell("1.1");
            t.addCell("1.2");
            t.addCell("1.3");
            section1.add(t);

            /** 创建章节中的小节 */
            Paragraph title13 = new Paragraph("列表的添加", FontChinese);
            Section section3 = chapter1.addSection(title13);
            /** 创建段落并添加到小节中 */
            Paragraph someSectionText3 = new Paragraph("下面展示的为列表.", FontChinese);
            section3.add(someSectionText3);
            /** 创建列表并添加到pdf文档中 */
            List l = new List(true, true, 10);// 第一个参数为true,则创建一个要自行编号的列表,
            // 如果为false则不进行自行编号
            l.add(new ListItem("First item of list"));
            l.add(new ListItem("第二个列表", FontChinese));
            section3.add(l);
            document.add(chapter1);

            /** 创建章节对象 */
            Paragraph title2 = new Paragraph("第二章", FontChinese);
            Chapter chapter2 = new Chapter(title2, 1);
            chapter2.setNumberDepth(0);
            /** 创建章节中的小节 */
            Paragraph title12 = new Paragraph("png图片添加", FontChinese);
            Section section2 = chapter2.addSection(title12);

            /** 添加图片 */
            section2.add(new Paragraph("图片添加: 饼图", FontChinese));
            Image png = Image.getInstance("D:/pie.png");//图片的地址
            section2.add(png);

            document.add(chapter2);
            document.close();
            return true;
        } catch (Exception e2) {
            System.out.println(e2.getMessage());
        }
        return false;
    }

    public static void main(String args[]) {
        System.out.println(new ITextDemo().iTextTest());
    }
}

 

 

注释:如果发现中文无法显示的时候,可以在你需要显示中文的地方先new Paragraph("",FontChinese);

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • VRRP笔记三:配置keepalived为实现haproxy高可用的双主模型配置文件示例:
  • 记一次Arch的安装经历
  • windows xp常用命令集
  • eclipseJVM terminated. Exit code=-1
  • java图片验证码
  • copyEvens
  • 字符设备驱动程序之异步通知
  • HTTP 错误 403.9 - 禁止访问:连接的用户过多
  • 如何使用flex皮肤
  • 查看进程占用的文件和文件数目
  • 动态创建DataGrid 列
  • Vue.js基础入门
  • 在 Tree 中查找节点
  • jQery使网页在显示器上居中显示适用于任何分辨率
  • 动态显示/隐藏 DataGrid 的列
  • Debian下无root权限使用Python访问Oracle
  • fetch 从初识到应用
  • httpie使用详解
  • java小心机(3)| 浅析finalize()
  • Python打包系统简单入门
  • vue--为什么data属性必须是一个函数
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 从零开始的无人驾驶 1
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 基于游标的分页接口实现
  • 记录一下第一次使用npm
  • 浏览器缓存机制分析
  • 每天10道Java面试题,跟我走,offer有!
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​业务双活的数据切换思路设计(下)
  • #laravel 通过手动安装依赖PHPExcel#
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • (3)nginx 配置(nginx.conf)
  • (二)fiber的基本认识
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (三)Kafka离线安装 - ZooKeeper开机自启
  • (十八)SpringBoot之发送QQ邮件
  • (学习日记)2024.02.29:UCOSIII第二节
  • (译)2019年前端性能优化清单 — 下篇
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)IOS中获取各种文件的目录路径的方法
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .libPaths()设置包加载目录
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • .NET项目中存在多个web.config文件时的加载顺序
  • .NET周刊【7月第4期 2024-07-28】
  • @Async注解的坑,小心
  • @NotNull、@NotEmpty 和 @NotBlank 区别
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [ solr入门 ] - 利用solrJ进行检索
  • [17]JAVAEE-HTTP协议