// 1.新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);

// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\ITextTest.pdf"));

// 3.打开文档
document.open();
// 4.向文档中添加内容
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);

// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落

document.add(new Paragraph("First page of the document."));
document.add(new Paragraph(value,fontChinese));
// 5.关闭文档
document.close();

/**
  * 添加含有章节的pdf文件
  */
 public static void writeCharpter() throws Exception {

// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));

// 打开文件
  document.open();

// 标题
  document.addTitle("Hello mingri example");

// 作者
  document.addAuthor("wolf");

// 主题
  document.addSubject("This example explains how to add metadata.");
  document.addKeywords("iText, Hello mingri");
  document.addCreator("My program using iText");

// document.newPage();
  // 向文档中添加内容
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
  Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));

// 新建章节
  Chapter chapter1 = new Chapter(title1, 1);
  chapter1.setNumberDepth(0);
  Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
  Section section1 = chapter1.addSection(title11);
  Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
  section1.add(someSectionText);
  someSectionText = new Paragraph("Following is a 3 X 2 table.");
  section1.add(someSectionText);
  document.add(chapter1);

// 关闭文档
  document.close();
 }

摘自:http://www.jb51.net/article/36626.htm

转载于:https://www.cnblogs.com/xinshou-2017/p/7306688.html

java导出pdf格式文档相关推荐

  1. Java DOC 转换给 PDF 格式文档的代码

    工作过程,把写代码过程经常用的代码片段备份一次,下面的代码段是关于Java DOC 转换给 PDF 格式文档的代码,应该对码农们有所用. import java.io.File; import jav ...

  2. php使用pdf2htmlex,转换 HTML 与 PDF 格式文档的神器

    企业 Web 项目开发中经常会有生产 PDF 格式文档的需求,例如 PDF 账单下载,月末生成各种统计报表等等.我们要帮助企业实现自动化,也就是说无需人工干预,程序能够按需从 DB 中拿数据自主生成. ...

  3. 如何批量提取 PDF 文档内容,将 PDF 格式文档转为 Txt 文本格式

    概要:PDF 文档常常用来阅读.预览或者存档一些资料,PDF 支持的内容也是比较丰富的.可以支持图片.文本等多种类型的元素.那有时候我们就需要将一些纯文本的 PDF 文档中的文字提取出来,转为一个 T ...

  4. java导出生成word文档_java使用freemarker 生成word文档

    最近需要做一个导出word的功能, 在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的样式也不美观,于是选择了另一种方式--- ...

  5. java 把pdf图片文档和文章文档转成文字的方法(此方法不好用,转换成文字可以,转成pdf不行)

    java 提供了一些库和工具可以用来把 PDF 文档和图片文档转成文本. Apache PDFBox:这是一个开源的 PDF 库,可以用来提取 PDF 文件中的文本内容. iText:这是一个用于创建 ...

  6. 简要介绍word文档转换为pdf格式文档的工具

    找了很多工具,其实都是乱七八糟的,没几个好用的,最好还是用Adobe Acrobat Pro吧,这个就很方便了,而且转换的也不错. ABC Amber PDF Converter ABC Amber ...

  7. java导出html word文档_java生成带html样式的word文件

    最近在项目中需要将通过富文本编辑器处理过的文字转换为Word,查了很久,大家通常的解决办法是使用Jacob或POI等组件直接生成Word,但是都无法将富文本编辑器处理过的文字保留样式并保存为Word, ...

  8. 用JPG转PDF转换器把图片制作成PDF格式文档

    我们都有这样一个想法,如果能将姥爷姥姥那一代的相册与爸爸妈妈以及我们合在一起,制成一个4680影集该是多么开心的事.要实现这个想法并不困难,21世纪信息化的时代给我们一个明确的答案, JPG转换成PD ...

  9. PPT如何转成PDF格式文档

    最近不少同事要求我们帮忙修改一些来自客户的ppt文件,其实ppt文件修改还是比较简单的,重要的是要将ppt转化成pdf文档,需要一款专业的pdf转换工具.对于这些对ppt文件有更高要求的用户,推荐使用 ...

最新文章

  1. zip/unzip 命令
  2. js、jquery、h5、css等简记
  3. android webview 多文件上传,Android中的webview支持页面中的文件上传实例代码
  4. boost::mpl模块实现replace相关的测试程序
  5. nll_loss 和 cross_entropy
  6. 蓝桥杯 ADV-189 算法提高 连接乘积
  7. HTTP协议基本原理简介(一)
  8. 这就是为什么IT人没有女朋友的原因!!
  9. tomcat设置编码格式utf8
  10. 如何在浏览器中下载网站的https证书
  11. 模拟电路全系列复习题 恶补知识点
  12. xp激活30天到期_我的XP系统还剩30天激活怎么办
  13. RGB565常见颜色对照表
  14. java转盘_Java 实现大转盘抽奖
  15. 频域滤波—方波变换中的沃尔什变换与哈达玛变换
  16. 电子负载的 Von/Voff 设置
  17. Intriguing Properties of Contrastive Losses
  18. mine makefile studing processing
  19. ScriptManager脚本管理器
  20. compareTo的用法

热门文章

  1. 【PostMan使用】PostMan的简单使用教程
  2. Python 中MNE库去伪迹(ICA)案例的逐句解析
  3. 10组团队项目-中期总结
  4. JSP中获取项目根目录
  5. 【C++】入门第一课
  6. SpringMVC(JSON数据转换,乱码问题)
  7. Android App开发之位图加工Bitmap中转换位图的像素色彩、裁剪内部区域、利用矩阵变换位图的讲解及实战(附源码和演示)
  8. php在fpm运行模式下实现服务之间的服务熔断、服务监控、调用日志
  9. 奔跑吧!Android虚拟机!Genymotion的安装和配置
  10. 关于 MATLAB 你首先要会的基础(权且可当做期末复习备考)