
正文
生成pdfjava代码 java pdf模版生成pdf
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java怎么输出pdf格式的文件
java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:
public class createPdf {
//自己做的一个简单例子,中间有图片之类的
//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document
Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
public void getPDFdemo() throws DocumentException, IOException{
//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:
//首先
//字体的定义:这里用的是自带的jar里面的字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
// 当然你也可以用你电脑里面带的字体库
//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//定义字体 注意在最新的包里面 颜色是封装的
Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
//生成pdf的第一个步骤:
//保存本地指定路径
saveLocal();
document.open();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
// PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//获取此编译的文件路径
String path = this.getClass().getClassLoader().getResource("").getPath();
//获取根路径
String filePath = path.substring(1, path.length()-15);
//获取图片路径 找到你需要往pdf上生成的图片
//这里根据自己的获取的路径写 只要找到图片位置就可以
String picPath = filePath +"\\WebContent" +"\\images\\";
//往PDF中添加段落
Paragraph pHeader = new Paragraph();
pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );
document.add(pHeader);//在文档中加入你写的内容
//获取图片
Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");
//定义图片在文档中显示的绝对位置
img2.scaleAbsolute(137.0F, 140.0F);
img2.setAbsolutePosition(330.0F, 37.0F);
//将图片添加到文档中
document.add(img2);
//关闭文档
document.close();
/*//设置文档保存的文件名
response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");
//设置类型
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();*/
}
public static void main(String[]args) throws DocumentException, IOException{
createPdf pdf= new createPdf();
pdf.getPDFdemo();
}
//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf
public void saveLocal() throws IOException, DocumentException{
//直接生成PDF 制定生成到D盘test.pdf
File file = new File("D:\\text2.pdf");
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));
}
}
相关问答
Q1: 如何用纯java代码实现word转pdf
几种方案:
方法一:用apache pio 读取doc文件生成pdfjava代码,然后转成html文件用Jsoup格式化html文件生成pdfjava代码,最后用itext将html文件转成pdf。
方法2:使用jdoctopdf来实现,这是一个封装好生成pdfjava代码的包,可以把doc转换成pdf,html,xml等格式,调用很方便
地址:
需要注意中文字体的写入问题。
方法3:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。
jodconverter的下载地址:
首先要安装openOffice,下载地址:
安装完后要启动openOffice的服务,具体启动方法请自行google
方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)
Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件
SaveAsPDFandXPS下载地址:
jacob 包下载地址:
Q2: 如何运用Java组件itext生成pdf
首先从iText的官网下载这个开源的小组件。
iText官方网站
Java版iText组件
Java版工具包
C#版iText组件
C#版工具包
这里笔者使用的是Java版itext-5.2.1。
将itext-5.2.1.zip压缩包解压缩后得到7个文件:itextpdf-5.2.1.jar(核心组件)、itextpdf-5.2.1-javadoc.jar(API文档)、itextpdf-5.2.1-sources.jar(源代码)、itext-xtra-5.2.1.jar、itext-xtra-5.2.1-javadoc.jar、itext-xtra-5.2.1-sources.jar
使用5步即可生成一个简单的PDF文档。
复制代码
1 // 1.创建 Document 对象
2 Document _document = new Document();
3 // 2.创建书写器,通过书写器将文档写入磁盘
4 PdfWriter _pdfWriter = PdfWriter.getInstance(_document, new FileOutputStream("生成文件的路径"));
5 // 3.打开文档
6 _document.open();
7 // 4.向文档中添加内容
8 _document.add(new Paragraph("Hi"));
9 // 5.关闭文档
10 _document.close();
复制代码
OK,搞定,不出问题的话就会在你指定的路径中生成一个PDF文档,内容是纯文本的“Hi”。
可是这样并不能完全满足我们的需求,因为通常我们要生成的PDF文件不一定是纯文本格式的,比如我现在要实现打印销售单的功能,那么最起码需要绘制表格才行,怎么办呢?且跟笔者继续向下研究。
在iText中,有专门的表格类,即PdfPTable类。笔者做了一个简单的表格示例,请先看代码:
复制代码
1 OutTradeList _otl = this.getOtlBiz().findOutTradeListById(this.getOtlid());
2 String _fileName = _otl.getOtlId() + ".pdf";
3
4 // iText 处理中文
5 BaseFont _baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true);
6 // 1.创建 Document 对象
7 Document _document = new Document(PageSize.A4);
8
9 HttpServletResponse response = ServletActionContext.getResponse();
10 response.setContentType("application/pdf; charset=ISO-8859-1");
11 response.setHeader("Content-Disposition", "inline; filename=" + new String(_fileName.getBytes(), "iso8859-1"));
12
13 // 2.创建书写器,通过书写器将文档写入磁盘
14 PdfWriter _pdfWriter = null;
15 try {
16 _pdfWriter = PdfWriter.getInstance(_document, response.getOutputStream());
17 } catch (Exception e) {
18 this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
19 e.printStackTrace();
20 System.out.println("2.挂了");
21 // return INPUT;
22 return null;
23 }
24 if(_pdfWriter == null) {
25 this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
26 System.out.println("3.挂了");
27 // return INPUT;
28 return null;
29 }
30
31 // 3.打开文档
32 _document.open();
33
34 // 4.创建需要填入文档的元素
35 PdfPTable _table = new PdfPTable(4);
36 PdfPCell _cell = null;
37
38 _table.addCell(new Paragraph("单据号", new Font(_baseFont)));
39 _cell = new PdfPCell(new Paragraph(_otl.getOtlId()));
40 _cell.setColspan(3);
41 _table.addCell(_cell);
42
43 _table.addCell(new Paragraph("客户名称", new Font(_baseFont)));
44 _cell = new PdfPCell(new Paragraph(_otl.getClients().getName(), new Font(_baseFont)));
45 _cell.setColspan(3);
46 _table.addCell(_cell);
47
48 _table.addCell(new Paragraph("销售日期", new Font(_baseFont)));
49 _cell = new PdfPCell(new Paragraph(_otl.getOutDate().toString()));
50 _cell.setColspan(3);
51 _table.addCell(_cell);
52
53 _cell = new PdfPCell();
54 _cell.setColspan(4);
55 PdfPTable _tabGoods = new PdfPTable(7);
56 // 添加标题行
57 _tabGoods.setHeaderRows(1);
58 _tabGoods.addCell(new Paragraph("序号", new Font(_baseFont)));
59 _tabGoods.addCell(new Paragraph("商品名称", new Font(_baseFont)));
60 _tabGoods.addCell(new Paragraph("自定义码", new Font(_baseFont)));
61 _tabGoods.addCell(new Paragraph("规格", new Font(_baseFont)));
62 _tabGoods.addCell(new Paragraph("数量", new Font(_baseFont)));
63 _tabGoods.addCell(new Paragraph("单价", new Font(_baseFont)));
64 _tabGoods.addCell(new Paragraph("小计", new Font(_baseFont)));
65 Object[] _outTrades = _otl.getOutTrades().toArray();
66 // 将商品销售详细信息加入表格
67 for(int i = 0; i _outTrades.length;) {
68 if((_outTrades[i] != null) (_outTrades[i] instanceof OutTrade)) {
69 OutTrade _ot = (OutTrade) _outTrades[i];
70 Goods _goods = _ot.getGoods();
71 _tabGoods.addCell(String.valueOf((++i)));
72 _tabGoods.addCell(new Paragraph(_goods.getName(), new Font(_baseFont)));
73 _tabGoods.addCell(_goods.getUserCode());
74 _tabGoods.addCell(_goods.getEtalon());
75 _tabGoods.addCell(String.valueOf(_ot.getNum()));
76 _tabGoods.addCell(String.valueOf(_ot.getPrice()));
77 _tabGoods.addCell(String.valueOf((_ot.getNum() * _ot.getPrice())));
78 }
79 }
80 _cell.addElement(_tabGoods);
81 _table.addCell(_cell);
82
83 _table.addCell(new Paragraph("总计", new Font(_baseFont)));
84 _cell = new PdfPCell(new Paragraph(_otl.getAllPrice().toString()));
85 _cell.setColspan(3);
86 _table.addCell(_cell);
87
88 _table.addCell(new Paragraph("操作员", new Font(_baseFont)));
89 _cell = new PdfPCell(new Paragraph(_otl.getProcure()));
90 _cell.setColspan(3);
91 _table.addCell(_cell);
92
93 // 5.向文档中添加内容,将表格加入文档中
94 _document.add(_table);
95
96 // 6.关闭文档
97 _document.close();
98 System.out.println(_fileName);
99 this.setPdfFilePath(_fileName);
100 System.out.println("3.搞定");
101 // return SUCCESS;
102 return null;
复制代码
以上代码是写在 Struts2 的 Action 中的,当用户发送了请求之后直接将生成的PDF文件用输出流写入到客户端,浏览器收到服务器的响应之后就会询问用户打开方式。
当然,我们也可以将文件写入磁盘等等。
Q3: java中怎么利用poi和itext生成pdf文档
生成PDF文档代码如下:
package poi.itext;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.BaseFont;
/**
* 创建Pdf文档
* @author Administrator
*
*/
public class HelloPdf
{
public static void main(String[] args)throws Exception
{
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
// 第一步,创建document对象
Rectangle rectPageSize = new Rectangle(PageSize.A4);
//下面代码设置页面横置
//rectPageSize = rectPageSize.rotate();
//创建document对象并指定边距
Document doc = new Document(rectPageSize,50,50,50,50);
Document document = new Document();
try
{
// 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起
//从而完成向Document写,即写入PDF文档
PdfWriter.getInstance(document,new FileOutputStream("src/poi/itext/HelloWorld.pdf"));
//第3步,打开文档
document.open();
//第3步,向文档添加文字. 文档由段组成
document.add(new Paragraph("Hello World"));
Paragraph par = new Paragraph("世界你好",FontChinese);
document.add(par);
PdfPTable table = new PdfPTable(3);
for(int i=0;i12;i++)
{
if (i == 0)
{
PdfPCell cell = new PdfPCell();
cell.setColspan(3);
cell.setBackgroundColor(new Color(180,180,180));
cell.addElement(new Paragraph("表格头" , FontChinese));
table.addCell(cell);
}
else
{
PdfPCell cell = new PdfPCell();
cell.addElement(new Paragraph("表格内容" , FontChinese));
table.addCell(cell);
}
}
document.add(table);
}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
//关闭document
document.close();
System.out.println("生成HelloPdf成功!");
}
}
希望对你有帮助。
生成pdfjava代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java pdf模版生成pdf、生成pdfjava代码的信息别忘了在本站进行查找喔。





