我这边使用的jar包:

itext-2.1.7.jar

itextasian-1.5.2.jar

代码,简单的小例子,导出pdf:

PdfService.java:

package com.cy.service;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Service;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter; @Service
public class PdfService { public static void main(String[] args) throws Exception {
//准备的参数
String specName = "基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN基本VLAN";
String matureTitle = "成熟度:";
String matureWord = "成熟特性 受控特性 不可商用";
String descriptionTitle = "描述:";
String descriptionWord = "从便利性、渲染效果综合来看,还是用XmlWorker好些。虽然渲染效果没有达到极致,但十分有利于用户自定义模板";
String remarkTitle = "备注:";
String remarkWord = "对于美制裁中兴一事,商务部新闻发言人高峰19日在回答记者提问时再次强调";
String boardName = "CloudEngine 12800 V200R001C00";
String pathTitle = "路径";
String pathWord = "CloudEngine 12800 V200R001C00>CE12800";
String xiangqTitle = "详情";
String xiangqWord = "美方行径引起了市场对美国贸易和投资环境的普遍担忧,美方的行为表面针对中国,但最终伤害的是美国自身,不仅会使其丧失数以万计的就业机会,还会影响成百上千的美国关联企业";
String beizTitle = "备注";
String beizWord = "将会动摇国际社会对美国投资和营商环境稳定的信心。希望美方不要自作聪明,否则只会自食其果。也希望美方不要低估中方的决心,如果美方坚持通过单边主义的保护政策,不惜伤害中美两国企业利益"; String outPath = "test.pdf";
Rectangle rect = new Rectangle(PageSize.A4);
Document doc=new Document(rect);
//中文字体,解决中文不能显示问题
BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字体样式
Font textFont = new Font(bfChinese,11,Font.NORMAL); //正常
Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗
Font titleFont = new Font(bfChinese,15,Font.BOLD); //二级标题
Color grayColor = new Color(204,204,204); PdfWriter.getInstance(doc, new FileOutputStream(new File(outPath)));
doc.open();
doc.newPage(); //规格名称
// Paragraph p= new Paragraph(specName, titleFont);
// p.setAlignment(Element.ALIGN_CENTER);
// doc.add(p);
PdfPTable head = new PdfPTable(1);
head.setTotalWidth(new float[]{520}); //设置列宽
head.setLockedWidth(true); //锁定列宽
head.setSpacingBefore(10f); // 前间距
head.setSpacingAfter(10f); // 后间距
PdfPCell cell1 = new PdfPCell(new Phrase(specName, titleFont));
cell1.setBorderWidth(0);
cell1.setBackgroundColor(grayColor);
cell1.setMinimumHeight(20); //设置单元格高度
cell1.setHorizontalAlignment(Cell.ALIGN_CENTER); //设置水平居中
cell1.setVerticalAlignment(Cell.ALIGN_MIDDLE); //设置垂直居中
head.addCell(cell1);
doc.add(head); //成熟度、描述、备注
Paragraph p = new Paragraph();
Phrase ph = new Phrase();
Chunk c1 = new Chunk(matureTitle, boldFont) ;
Chunk c2 = new Chunk(matureWord, textFont) ;
ph.add(c1);
ph.add(c2);
p.add(ph);
p.setLeading(20);
doc.add(p); p = new Paragraph();
ph = new Phrase();
c1 = new Chunk(descriptionTitle, boldFont) ;
c2 = new Chunk(descriptionWord, textFont) ;
ph.add(c1);
ph.add(c2);
p.add(ph);
p.setLeading(20);
doc.add(p); p = new Paragraph();
ph = new Phrase();
c1 = new Chunk(remarkTitle, boldFont) ;
c2 = new Chunk(remarkWord, textFont) ;
ph.add(c1);
ph.add(c2);
p.add(ph);
p.setLeading(20);
doc.add(p); //创建2列的详情表格
PdfPTable table = new PdfPTable(2);
table.setSpacingBefore(20f); //table前间距
table.setTotalWidth(new float[]{ 100, 420}); //设置列宽
table.setLockedWidth(true); //锁定列宽
PdfPCell cell;
//第一行 实例名
cell = new PdfPCell(new Phrase(" ", textFont));
cell.setMinimumHeight(30); //设置单元格高度
cell.setBackgroundColor(grayColor);
table.addCell(cell);
cell = new PdfPCell(new Phrase(boardName, textFont));
cell.setBackgroundColor(grayColor);
cell.setUseAscender(true); //设置可以居中
cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //设置水平居中
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //设置垂直居中
table.addCell(cell);
//第二行 路径
cell = new PdfPCell(new Phrase(pathTitle, textFont));
cell.setMinimumHeight(40);
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(pathWord, textFont));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell);
//第三行 详情
cell = new PdfPCell(new Phrase(xiangqTitle, textFont));
cell.setMinimumHeight(80);
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(xiangqWord, textFont));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell);
//第四行 备注
cell = new PdfPCell(new Phrase(beizTitle, textFont));
cell.setMinimumHeight(120);
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(beizWord, textFont));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(cell); doc.add(table);
doc.close(); System.out.println("------------->>写出完毕");
} }

运行之后导出的效果:

利用itext导出PDF的小例子的更多相关文章

  1. java利用itext导出pdf

    项目中有一功能是导出历史记录,可以导出pdf和excel,这里先说导出pdf.在网上查可以用那些方式导出pdf,用itext比较多广泛. 导出pdf可以使用两种方式,一是可以根据已有的pdf模板,进行 ...

  2. Java利用IText导出PDF(更新)

    我很久以前写的还是上大学的时候写的:https://www.cnblogs.com/LUA123/p/5108007.html ,今天心血来潮决定更新一波. 看了下官网(https://itextpd ...

  3. 利用itext生成pdf的简单例子

    一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  4. Itext导出PDF,word,图片案例

    iText导出pdf.word.图片 一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生 ...

  5. 利用FR导出PDF汉字乱码的处理

    利用FR导出pdf,然后在unigui中显示,发现汉字乱码,改成gb2312,不乱码,但不自动折行,最后是改成DefaultCharSet搞定.FR版本:5.4.6 后记:有的浏览器中还是乱码,把字体 ...

  6. 新知识:Java 利用itext填写pdf模板并导出(昨天奋战到深夜四点,知道今天两点终于弄懂)

    废话少说,不懂itext干啥用的直接去百度吧. ***************制作模板******************* 1.先用word做出界面 2.再转换成pdf格式 3.用Adobe Acr ...

  7. iText导出pdf、word、图片

    一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或 ...

  8. (转)关于使用iText导出pdf

    一.iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  9. iText导出PDF(图片,水印,页眉,页脚)

    项目需要导出PDF,导出的内容包含图片和文本,而且图片的数量不确定,在网上百度发现大家都在用iText,在官网发现可以把html转换为PDF,但是需要收费,那就只能自己写了. 在开始之前先在网上百度了 ...

随机推荐

  1. ZOJ4062 Plants vs. Zombies(二分+贪心)

    题目链接:传送门 题目大意: 有n棵植物依次放在1-n,机器人从0出发浇水,每棵植物被浇水时di += ai,求浇m次水后min{di|1 ≤ i ≤ n}的最大值.(浇水时必须往左或往右走一步,落脚 ...

  2. MyBatis like函数使用注意事项

    百分号后面必须要加上空格,不然会将后面的字符串全部都黏在一起,导致sql语句运行报错

  3. npm常规命令行集合

    最近在摸索vue-cli脚手架的安装,中间用到了一些node的npm命令行,进行了一些整理,并且这个会一直搜集整理更新! 1,常规文件操作命令 cd..                   返回当前文 ...

  4. 2017.5.10 MapReduce内部逻辑

    MapReduce内部逻辑 Split:HDFS 中的数据以 Split 方式作为 MapReduce 的输入 Block 是 HDFS 术语,Split 是 MapReduce 术语 通常1个 Sp ...

  5. hdu4705 Y 树形DP

    给出一颗数,求没有一条路径穿过的节点三元集合个数. 这样的三元集合呈现Y字形,求出反面情况,三点为子节点和两个祖先节点,或一个祖先节点与它子树中非父子关系的节点.可由树形DP求得. #pragma c ...

  6. 【BZOJ3240】【UOJ#124】【NOI2013】矩阵游戏

    终于看懂一道题QAQ然而NOI都是这种难度题怎么玩QAQ 原题: 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若 ...

  7. elasticsearch技术解析与实战(一) 入门和索引

    GET _cat/nodes GET _cat/health GET _cat/shards GET http://10.37.84.124:9200/secisland?pretty { " ...

  8. sqler sql 转rest api 的工具试用

    sqler 从开源很快就获取了1k的star,使用起来很方便,而且也很灵活,支持的数据库也比较多. 支持的功能 无需依赖,可独立使用: 支持多种数据可类型,包括:SQL Server, MYSQL, ...

  9. Singer 学习九 运行&&开发taps、targets (四 开发target)

    singer 的target 需要从stdin 的行数据,同时处理schema.record.state 消息 指南 schema 需要进行关联stream records 数据的校验 一旦Targe ...

  10. Bubble Sort冒泡排序

    冒泡排序是一种简单的排序算法. 它每次重复的访问过要排序的数列, 一次比较两个元素, 如果他们的顺错误, 就把他们交换过来. 下面这种图很清晰的解释了什么是冒泡算法. 具体算法描述如下: 1. 比较相 ...