1、引入maven

     <dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>

2、大致流程

//创建一条文本对象(A4纸张)
Document document = new Document(PageSize.A4); //创建一个DOC文档(根据指定的路径)
RtfWriter2.getInstance(document, new FileOutputStream(file_url)); //打开文本对象
document.open(); //添加一个简单的段落
document.add(new Paragraph("Hello World!")); //结束编写
document.close();

3、字体

//设置基础字体 -- 宋体
BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //设置字体 (含有大小、颜色……)
Font titleFont = new Font(bfChinese, 14.0F, 1); //字体:宋体 大小:14.0F 加粗
Font contextFont = new Font(bfChinese, 14.0F, 0); //字体:宋体 大小:14.0F 不加粗
Font contextFont_red = new Font(bfChinese, 14.0F, 0, Color.red); //字体:宋体 大小:14.0F 不加粗 字体颜色:红色
Font contextFont_black = new Font(bfChinese, 14.0F, 0, Color.black);//字体:宋体 大小:14.0F 不加粗 字体颜色:黑色
Font headerFooterFont = new Font(bfChinese, 9.0F, 0);//字体:宋体 大小:9.0F 不加粗

4、设置头 和 页码数

全局变量

private static String TECH_INFO = "这里是顶部";  //头部标语
private static String FOOT_INFO = "这里是底部"; //底部标语

头部

Table header = new Table(2);
header.setBorder(0);
header.setWidth(100.0F); Paragraph address = new Paragraph(TECH_INFO);
address.setFont(headerFooterFont);
Cell cell01 = new Cell(address);
cell01.setBorder(0);
header.addCell(cell01); Paragraph date = new Paragraph("生成日期: " +
new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
date.setAlignment(2);
date.setFont(headerFooterFont);
cell01 = new Cell(date);
cell01.setBorder(0);
header.addCell(cell01);
document.setHeader(new RtfHeaderFooter(header));

底部(页码数)

Table footer = new Table(2);
footer.setBorder(0);
footer.setWidth(100.0F); Paragraph company = new Paragraph(FOOT_INFO);
company.setFont(headerFooterFont);
Cell cell02 = new Cell(company);
cell02.setBorder(0);
footer.addCell(cell02); Paragraph pageNumber = new Paragraph("第 ");
pageNumber.add(new RtfPageNumber());
pageNumber.add(new Chunk(" 页"));
pageNumber.setAlignment(2);
pageNumber.setFont(headerFooterFont);
cell02 = new Cell(pageNumber);
cell02.setBorder(0);
footer.addCell(cell02); document.setFooter(new RtfHeaderFooter(footer));

5、表格

Table table1 = new Table(4);//创建表格

int width[] = {18,32,18,32};//设置每列宽度比例

table1.setWidths(width);//设置每列宽度比例

table1.setWidth(95);//占页面宽度比例

//第一行 第一个
Cell cell11 = new Cell(new Paragraph("11",contextFont));
cell11.setVerticalAlignment(Element.ALIGN_TOP); //垂直 居上
cell11.setHorizontalAlignment(Element.ALIGN_RIGHT); //水平 居右
cell11.setBorder(0); //第一行 第二个
Cell cell12 = new Cell("12");
cell12.setVerticalAlignment(Element.ALIGN_BOTTOM); //垂直 居下
cell12.setHorizontalAlignment(Element.ALIGN_LEFT); //水平 居左
cell12.setBorder(0); //第一行 第三个
Cell cell13 = new Cell(new Paragraph("13",contextFont));
cell13.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直 居中
cell13.setHorizontalAlignment(Element.ALIGN_RIGHT); //水平 居右
cell13.setBorder(0); //第一行 第四个
Cell cell14 = new Cell(new Paragraph("14",contextFont));
cell14.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直 居中
cell14.setHorizontalAlignment(Element.ALIGN_LEFT); //水平 居左
cell14.setBorder(0); table1.addCell(cell11);
table1.addCell(cell12);
table1.addCell(cell13);
table1.addCell(cell14); document.add(table1);

6、插入图片

//图片路径
String URL = "http://www.baidu.com/img/bd_logo1.png";
//创建图片
Image img = null;
try {
img = Image.getInstance(URL);
img.scalePercent(10);
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//内容
Paragraph context = new Paragraph();
context.setAlignment(1);
context.setFont(contextFont);
if(img == null){
context.add("没有图片");
}else{
context.add(img);
}
document.add(context);

itext 生成doc文档 小结(自己备忘)的更多相关文章

  1. itext 生成pdf文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf& ...

  2. IText 中文字体解决方案 生成doc文档

    IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...

  3. java基础---->使用Itext生成数据库文档

    这里简单的介绍一下使用Itext生成数据库表的文档.于是我们领教了世界是何等凶顽,同时又得知世界也可以变得温存和美好. 生成数据库的文档 一.maven项目需要引入的jar依赖 <depende ...

  4. java标识符,关键字,注释及生成Doc文档

    # java语法基础 ## 标识符,关键字与注释 ### 标识符 1.类名,变量名,方法名都称为标识符. 2.命名规则:(1):所有的标识符都应该以字母(AZ,或者az)美元符($)或者下划线(_)开 ...

  5. Java eclipse生成doc文档

    这里讲解下eclipse成为doc文档,首先代码: /** * @author szy * @version 1.0 */ package com.founder.sun; class Cat{ pu ...

  6. Java多种方式动态生成doc文档

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5280272.html 本来是要在Android端生成doc的(这需求...),最后方法没有好的方法能够在An ...

  7. 【PDF】java使用Itext生成pdf文档--详解

    [API接口]  一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...

  8. IDEA生成doc文档-生成chm文档

    首先,打开IDEA,并找到Tools -> Generate JavaDoc- 可供查询的chm比那些HTML页面好看多了. 如果您用过JDK API的chm文档,那么您一定不会拒绝接受其它第三 ...

  9. 如何在使用itext生成pdf文档时给文档添加背景图片

    这个问题我在网上搜了很久,没有找到什么解决方案,需求其实很简单,就是添加背景图片.在解决这个问题之前,我们需要了解什么是背景图片?背景图片就是位于文档最底层的图片,文字和其他内容可以浮在它的上面.这又 ...

随机推荐

  1. Web 常用

    System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();

  2. Linux内核中_IO,_IOR,_IOW,_IOWR宏的用法与解析

    ref from : http://blog.csdn.net/zhuxiaoping54532/article/details/49680537 main 在驱动程序里, ioctl() 函数上传送 ...

  3. POJ 1088 滑雪(简单的记忆化dp)

    题目 又一道可以称之为dp的题目,虽然看了别人的代码,但是我的代码写的还是很挫,,,,,, //看了题解做的简单的记忆化dp #include<stdio.h> #include<a ...

  4. Exception总结

    NO.1 java.lang.NullPointerException 程序遇上了空指针 NO.2 java.lang.ClassNotFoundException 指定的类不存在 NO.3 java ...

  5. 弹层蒙版(mask),ios滚动穿透,我们项目的解决方案

    问题描述 项目开发遇到一个ios独有的问题,在wkwebview中稳定复现 问题: 弹出一个蒙版,当在蒙版上面滑动的时候蒙版后面的内容滚动了 这当然是ios的bug,但是经过我们测试iphone7也会 ...

  6. 磁盘阵列 RAID 技术原理详解

    RAID一页通整理所有RAID技术.原理并配合相应RAID图解,给所有存储新人提供一个迅速学习.理解RAID技术的网上资源库,本文将持续更新,欢迎大家补充及投稿.中国存储网一如既往为广大存储界朋友提供 ...

  7. 调度器Quartz的配置文件中的线程池设置

    在使用调度器Quartz来进行数据归档的时候,当我们开的定时任务很多的时候,就会出现一些定时任务不会被触发的现象,这就是线程阻塞.那到底什么叫线程阻塞呢? 线程阻塞,顾名思义就是说线程被阻塞了,没有按 ...

  8. [NOIP2016day2T1] 組合數問題(problem)

    题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们 ...

  9. 如何拿CSDN博客上的原图

    比如带水印的地址: http://img.blog.csdn.net/20140408122234546?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdT ...

  10. 目标跟踪学习系列十:Struck:Structured Output Tracking with Kernels 代码调试

    本来想看完代码再具体的写的.可是有人问了就先贴出来吧! 代码调试中会遇到的一些的问题. 首先,你没有代码的话能够在这里下载:http://download.csdn.net/detail/u01219 ...