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的更多相关文章

  1. Servlet输出PDF文档方法

    概述 Java Servlet 编程可以很方便地将 HTML 文件发送到客户端的 Web 浏览器.然而许多站点还允许访问非 HTML 格式的文档,包括 Adobe PDF.Microsoft Word ...

  2. django “如何”系列9:三合一:利用遗留的数据库、输出csv和输出pdf

    如何集成遗留的数据库 django在适合开发新应用的同时,可以可以集成以前遗留的数据库,下面是如何集成一个已经存在的数据库的流程. 给定你的数据库的参数 你需要告诉django你的数据库连接参数以及数 ...

  3. Rmarkdown:输出pdf设置

    输出pdf需要安装Ctex --- title: "first markdown" author: "name" date: "`r format(S ...

  4. emacs在org-mode时输出pdf时,只输出为链接

    这个找到问题了,写错格式了,输出pdf中要内嵌图像,格式要写成[[file:/home/xxx/yyy.jpg]],不能写成file:/home/xxx/yyy.jpg或者[[file:/home/x ...

  5. itextpdf JAVA 输出PDF文档

    使用JAVA生成PDF的时候,还是有些注意事项需要处理的. 第一.中文问题,默认的itext是不支持中文的,想要支持,需要做些处理. 1.直接引用操作系统的中文字体库支持,由于此方案限制性强,又绑定了 ...

  6. c# 中crystal report输出PDF文件

    工程中引入以下crystal report的类库crystaldecisions.crystalreports.enginecrystaldecisions.reportsourcecrystalde ...

  7. C# 输出pdf文件流在页面上显示

    1 不调用itextsharp.dll的操作 /// <summary>        /// 生成pdf流        /// </summary>        /// ...

  8. 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件

    ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...

  9. 【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常

    尼玛,这个问题干扰了我两个星期!! 关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发 在读<<iText in Acti ...

  10. Jupyter Notebook通过latex输出pdf

    主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{artic ...

随机推荐

  1. linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )

    1: 台式机家里面不想再走线了. 于是去某东买了个USB无线网卡.tp的WN725N  USB,非常小, 和罗技的优联接收器差不多大. 2:  驱动能自己识别是不指望了,既然是usb网卡,插入USB后 ...

  2. C++学习(一):现代C++尝试

    C++是一门与时俱进的语言. 早期的C++关注的主要问题是通用性,却没有太多关注易用性的问题,使得C++成为了一门多范式语言,但是使用门槛较高. 从2011开始,C++的标准进行了较大的更新,开始更多 ...

  3. LeetCode OJ--Word Break II ***@

    https://oj.leetcode.com/problems/word-break-ii/ class Solution { public: unordered_set<string> ...

  4. Linux每日一坑001

    centos6,7中网卡/etc/sysconfig/network-scripts/ifcfg-eth0的命名是有要求的,必须是ifcfg-开头.改网卡名的时候掉坑.

  5. (11)centos之vim使用

    ZZ 保存并退出 :x 保存并退出 :q 不保存退出

  6. 最短Hamilton路径

    题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每 ...

  7. SPOJ 20713 DIVCNT2 - Counting Divisors (square)

    DIVCNT2 - Counting Divisors (square) #sub-linear #dirichlet-generating-function Let \sigma_0(n)σ​0​​ ...

  8. MySQL错误:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL easonjim

    错误: Error Code: . You are using safe update mode and you tried to update a table without a WHERE tha ...

  9. 发现一个直播录制工具you-get

    地址:https://github.com/soimort/you-get 截至到今天,支持的平台如下: Site URL Videos? Images? Audios? YouTube https: ...

  10. windows service 调试

    在程序运行入口调试 #if DEBUG Debugger.Launch(); #endif