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 / ...
随机推荐
- js fetch api
MDN fatch 返回的对象 type string url string redirected boolean status number ok boolean statusText string ...
- WIN7/XP用注册表关联指定后缀名和打开程序(手动【图文】和C编程两种实现)
前言: 本文是基本原理介绍和手动的操作.程序实现该功能在http://blog.csdn.net/arvon2012/article/details/7839556,同时里面有完整代码的下载. 今天在 ...
- myEclipse中项目无法部署到tomcat
问题现象: 从svn上新下载了项目到win环境上. 部署项目的时候,在servers视图里,Add Deployment,如下图: 发现只有一个项目可以加载,另外的项目看不到:可是明明我并没有部署过啊 ...
- 洛谷P1216 数字三角形【dp】
题目:https://www.luogu.org/problemnew/show/P1216 题意: 给定一个三角形.从顶走到底,问路径上的数字之和最大是多少. 走的时候可以往左下(实际上纵坐标不变) ...
- {django模型层(二)多表操作}一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询、分组查询、F查询和Q查询
Django基础五之django模型层(二)多表操作 本节目录 一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询.分组查询.F查询和Q查询 六 xxx 七 ...
- [No0000107]C#中 Excel列字母与数字的转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 会话(Session)与cookies
由于http是无状态的,向服务器发送请求后,服务器解析请求然后返回对应的响应,服务器负责完成这个过程是完全独立的,不会记录前后状态的变化,因此缺少状态记录. 我们分别需要会话和Cookies的技术来保 ...
- 使用XPath对象解析xml文件
使用XPath对象解析xml文件 1.DocumentBuilderFactory类 工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器 其构造方法受保护,用newInstance() ...
- python的数据库链接
https://blog.csdn.net/canofy/article/details/83294330#-*-coding:utf-8-*-import MySQLdb #yum update p ...
- charles4.2下载与破解方法以及配置https
Charles的使用方法 Charles下载地址 地址:https://www.charlesproxy.com/latest-release/download.do 2. Charles破解 破解地 ...