iText实现导出pdf文件java代码实现例子
///////////////////////////////////主类//////////////////////////////////////////
package com.iText;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;
public class test2 {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
try {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese , 16 , Font.BOLD);
Paragraph title = new Paragraph("包间设备使用情况统计报表",fontChinese);
Paragraph title2 = new Paragraph(" ");
//设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/a.pdf"));
document.open();
BaseFont _baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
PdfPTable table = new PdfPTable(7);
table.setHeaderRows(1);
PdfPTableEvent event = new AlternatingBackground();
table.setTableEvent(event);
table.getDefaultCell().setMinimumHeight(30);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
/*int[] a = {18,18,58,68,18,18,18};
_tabGoods.setWidths(a);*/
table.setWidthPercentage(100);
// 添加标题行
//_tabGoods.setHeaderRows(2);
table.addCell(new Paragraph("序号", new Font(_baseFont)));
table.addCell(new Paragraph("商品名称", new Font(_baseFont)));
table.addCell(new Paragraph("自定义码", new Font(_baseFont)));
table.addCell(new Paragraph("规格", new Font(_baseFont)));
table.addCell(new Paragraph("数量", new Font(_baseFont)));
table.addCell(new Paragraph("单价", new Font(_baseFont)));
table.addCell(new Paragraph("小计", new Font(_baseFont)));
String[] _outTrades = {"序号1","商品名称2","自定义码355555","规格4","数量5","单价6","小计7"};
// 将商品销售详细信息加入表格
for(int i = 0; i < 30;) {
table.addCell(String.valueOf((++i)));
table.addCell(new Paragraph(_outTrades[1]+i, new Font(_baseFont)));
table.addCell(new Paragraph(_outTrades[2]+i, new Font(_baseFont)));
table.addCell(new Paragraph(_outTrades[3]+i, new Font(_baseFont)));
table.addCell(new Paragraph(_outTrades[4]+i, new Font(_baseFont)));
table.addCell(new Paragraph(_outTrades[5]+i, new Font(_baseFont)));
table.addCell(new Paragraph(_outTrades[6]+i, new Font(_baseFont)));
}
document.add(title);
document.add(title2);
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//////////////////////////////////////////////////////////////////
///////////////////////////相间背景色的事件监听//////////////////////////
package com.iText;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
public class AlternatingBackground implements PdfPTableEvent {
public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
int columns;
Rectangle rect;
//合适的颜色:(235,235,235)
int footer = widths.length - table.getFooterRows();
int header = table.getHeaderRows() - table.getFooterRows();
for (int row = header; row < footer; row += 2) {
columns = widths[row].length - 1;
rect = new Rectangle(widths[row][0], heights[row], widths[row][columns], heights[row + 1]);
rect.setBackgroundColor(new BaseColor(225,225,225));
rect.setBorder(Rectangle.NO_BORDER);
canvases[PdfPTable.BASECANVAS].rectangle(rect);
}
}
}
iText实现导出pdf文件java代码实现例子的更多相关文章
- Java导出pdf文件数据
提示:导出pdf文件,需要3个jar包iText-2.1.5.jar,iTextAsian.jar,iText-rtf-2.1.4.jar. public boolean outputPdfJhsy( ...
- 史上最全的springboot导出pdf文件
最近项目有一个导出报表文件的需求,我脑中闪过第一念头就是导出pdf(产品经理没有硬性规定导出excel还是pdf文件),于是赶紧上网查看相关的资料,直到踩了无数的坑把功能做出来了才知道其实导出exce ...
- 使用iText库创建PDF文件
前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...
- asp.net2.0导出pdf文件完美解决方案【转载】
asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...
- .Net导出pdf文件,C#实现pdf导出
最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...
- .Net导出pdf文件,C#实现pdf导出 转载 http://www.cnblogs.com/hmYao/p/5842958.html
导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本,之前下了个5.4的似乎不好用. 下载之后直接添加引用. <%@ Page Lan ...
- 利用ITextSharp导出PDF文件
最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- 纯前端导出pdf文件
纯前端js导出pdf,已经用于生产环境. 工具: 1.html2canvas,一种让html转换为图片的工具. 2.pdfmake或者jspdf ,一种生成.编辑pdf,并且导出pdf的工具. pdf ...
- C# 利用ITextSharp导出PDF文件
最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
随机推荐
- windows下模拟网络延时、丢包、抖动
1.Fiddler 免费软件 模拟网速功能比较单一(Rules --> Performance --> Simulate Modem speed),选项较少,Fiddler仅是减缓带宽并未 ...
- Flask web开发之路十二
ge请求和post请求 ### get请求和post请求:1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放 ...
- tomcat启动项目报错:The specified JRE installation does not exist
在Build Path里设置好jre和各Library的顺序,代码无报错,启动时弹框,里面的信息是:The specified JRE installation does not exist. 后来想 ...
- css学习_写法规范、选择器
1.css(层叠样式表)样式主要目的(让页面更好看些) css尽量不要写内联样式,保证结构和样式分离原则: html专门负责结构,css专门负责样式. 2.css写法规范 选择器 { 属性 :值 : ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- [No0000129]WPF(1/7)开始教程[译]
概要 在我使用了半年多 WPF 后,是时候写点关于 WPF 基础方面的东西了.我发表了一系列针对具体问题的文章.现在是到了让大家明白为什么说WPF是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...
- [No0000104]JavaScript-基础课程4
要说 JavaScript 和其他较为常用的语言最大的不同是什么,那无疑就是 JavaScript 是函数式的语言,函数式语言的特点如下: 函数为第一等的元素,即人们常说的一等公民.就是说,在函数式编 ...
- nginx的proxy_pass路径转发规则浅析(末尾/问题)
源地址 : https://www.zifangsky.cn/917.html 一 location匹配路径末尾没有 / 此时proxy_pass后面的路径必须拼接location的路径: 1 2 ...
- DRBD数据镜像与搭建
一.数据安全工具DRDB 1. 数据镜像软件DRDB介绍 分布式块设备复制,是基于软件.基于网络的块复制存储解决方案 作用:用于服务器之间的磁盘.分区.逻辑卷等进行数据镜像. 例如:当用户将数据写入本 ...
- JavaScript面向对象之创建类和方法
一,js使用函数来定义类而不是像别的编程语言一样通过关键字class来定义,通过类本身(this)和原型(prototype)来完成面对对象编程! 示例1, //创建ElectronicSignatu ...