输出 pdf
jar 包 :core-renderer.jar iText-2.0.8.jar iTextAsian.jar
方式1:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.pdf.BaseFont; public class pdf {
public static void main(String[] args) throws FileNotFoundException {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("test");
}
String outputFile = "e:/firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
StringBuffer html = new StringBuffer();
//组装成符合W3C标准的html文件,否则不能正确解析
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">")
.append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: Arial Unicode MS;}</style>")
.append("<style type=\"text/css\">img {width: 500px;}</style>")
.append("<style type=\"text/css\">table {font-size:13px;}</style>")
.append("</head>")
.append("<body>");
html.append("<center>");
html.append("<div>报表测试</div>");
for(int i=0;i<list.size();i++) {
html.append("<div>" + list.get(i) + "</div>");
}
html.append("</center>");
html.append("</body></html>");
try {
renderer.setDocumentFromString(html.toString());
// ITextFontResolver fontResolver = renderer.getFontResolver();
// fontResolver.addFont("e:/fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解决图片的相对路径问题,图片路径必须以file开头
// renderer.getSharedContext().setBaseURL("file:/" + rootpath);
renderer.layout();
renderer.createPDF(os);
os.close();
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
}
方式2
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter; public class pdf2 {
public static void main(String[] args) throws FileNotFoundException, Exception {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("test");
}
String outputFile = "e:/firstdoc.pdf";
OutputStream ops = new FileOutputStream(outputFile);
Document document = new Document();
PdfWriter.getInstance(document, ops);
document.open();
// set chinese font
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font f2 = new Font(bfChinese, 2, Font.NORMAL);
Font f6 = new Font(bfChinese, 6, Font.NORMAL);
Font f8 = new Font(bfChinese, 8, Font.NORMAL);
Font f10 = new Font(bfChinese, 10, Font.NORMAL);
Font f12 = new Font(bfChinese, 12, Font.BOLD);
// int rowNumer = 0;
// PdfPTable table = new PdfPTable(2);
// table.setWidthPercentage(80);
// table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
// PdfPCell titleCell = new PdfPCell();
// titleCell.setBackgroundColor(new Color(213, 141, 69));
// titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// titleCell.setPhrase(new Paragraph("人口指标", f8));
// table.addCell(titleCell);
// titleCell.setPhrase(new Paragraph("人口指标2", f8));
// table.addCell(titleCell);
// document.add(table);
PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setColspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2"); document.add(table);
document.close();
ops.close();
System.out.println("ok");
}
}
输出 pdf的更多相关文章
- Servlet输出PDF文档方法
概述 Java Servlet 编程可以很方便地将 HTML 文件发送到客户端的 Web 浏览器.然而许多站点还允许访问非 HTML 格式的文档,包括 Adobe PDF.Microsoft Word ...
- django “如何”系列9:三合一:利用遗留的数据库、输出csv和输出pdf
如何集成遗留的数据库 django在适合开发新应用的同时,可以可以集成以前遗留的数据库,下面是如何集成一个已经存在的数据库的流程. 给定你的数据库的参数 你需要告诉django你的数据库连接参数以及数 ...
- Rmarkdown:输出pdf设置
输出pdf需要安装Ctex --- title: "first markdown" author: "name" date: "`r format(S ...
- emacs在org-mode时输出pdf时,只输出为链接
这个找到问题了,写错格式了,输出pdf中要内嵌图像,格式要写成[[file:/home/xxx/yyy.jpg]],不能写成file:/home/xxx/yyy.jpg或者[[file:/home/x ...
- itextpdf JAVA 输出PDF文档
使用JAVA生成PDF的时候,还是有些注意事项需要处理的. 第一.中文问题,默认的itext是不支持中文的,想要支持,需要做些处理. 1.直接引用操作系统的中文字体库支持,由于此方案限制性强,又绑定了 ...
- c# 中crystal report输出PDF文件
工程中引入以下crystal report的类库crystaldecisions.crystalreports.enginecrystaldecisions.reportsourcecrystalde ...
- C# 输出pdf文件流在页面上显示
1 不调用itextsharp.dll的操作 /// <summary> /// 生成pdf流 /// </summary> /// ...
- 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件
ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...
- 【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常
尼玛,这个问题干扰了我两个星期!! 关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发 在读<<iText in Acti ...
- Jupyter Notebook通过latex输出pdf
主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{artic ...
随机推荐
- linux 下 多进程与多线程
[Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...
- C语言集锦(三)Direct3D和GDI+的例子
0.前言 有些时候你可能想了解,如何用纯C语言来写Direct3D和GDI+的Demo.注意,下面的Direct3D例子不适用于TCC编译器,GDI+的例子是可以的. 1.Direct3D C语言的例 ...
- SecureCRT鼠标快速复制粘贴
- React 入门之路
React React简介 是由Facebook公司推广的一套框架,已经应用instagram等产品 React就是为了提供应用程序性能而设计的一套框架 在angular中,对dom提供了一些指令,让 ...
- Docker 容器网络
默认网络 当安装docker时,它会自动创建3个网络.可以使用docker network ls 来查看. zane@zane-V:~$ docker network ls NETWORK ID ...
- iOS 5的文件存储策略应对
苹果在iOS 5系统时,对app的文件存储提出了新的要求.从它的guildline来看,是推荐开发者尽量把app生成的文件放在Caches目录下的.原文如下: Only user-generated ...
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 1>
这段时间忙于看文献,没用过VS了. 今天用着用着就报错了: LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 1> 问度娘,大神给出了解决方法 ...
- 【GLSL教程】(六)逐顶点的光照 【转】
引言 在OpenGL中有三种类型的光:方向光(directional).点光(point).聚光(spotlight).本教程将从方向光讲起,首先我们将使用GLSL来模仿OpenGL中的光. 我们将向 ...
- Web编程前端之7:web.config详解 【转】
http://www.cnblogs.com/alvinyue/archive/2013/05/06/3063008.html 声明:这篇文章是摘抄周公(周金桥)的<asp.net夜话> ...
- 时间迭代和BigDecimal操作
常规小操作的代码: import java.math.BigDecimal; import java.sql.Timestamp; import java.text.SimpleDateFormat; ...