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 / ...
随机推荐
- J S 脚本语言 if() { if { } else { } } var a =100; switch { case ( ) break ; } 基础详解 , 最下面有例子
注释语法 注释语法// 多行注释/ JS输出语句 JS样式尽量靠最下面写 <script type="text/javascript">//嵌入JS开始代码 //ale ...
- 洛谷P1433 吃奶酪【dfs】【剪枝】
题目:https://www.luogu.org/problemnew/show/P1433 题意: 给定n个坐标,要求从(0,0)开始走遍所有点,最少经过的路程. 思路: 刚开始想像数字三角形一样适 ...
- CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...
- POJ 1854 - Evil Straw Warts Live
Description A palindrome is a string of symbols that is equal to itself when reversed. Given an inpu ...
- MVC模型和MVT模型
MVC 大部分开发平台都需要搭建的后台框架,Java和PHP最为喜爱 M:model. 模型. 主要用于对数据库层的封装 V:view. 视图. 用于对用户展 ...
- CSS:盒模型和position定位
盒模型 页面上显示的每个元素(包括内联元素)都可以看作一个盒子,即盒模型( box model ).请看Chrome DevTools 里的截图: 可以显而易见的看出盒模型由 4 部分组成.从内到外分 ...
- 使用babel与@babel/node
安装 yarn add -D @babel/cli @babel/node 编译entry-server.js yarn babel ./src/ssr/entry-server.js --prese ...
- express 写一个简单的web app
之前写过一个简单的web app, 能够完成注册登录,展示列表,CURD 但是版本好像旧了,今天想写一个简单的API 供移动端调用 1.下载最新的node https://nodejs.org/zh- ...
- linux状态及原理全剖析
Table of Contents 1 linux 1.1 proc filesystem 1.1.1 /proc 1.1.1.1 /proc/meminfo 1.1.1.2 /proc/stat 1 ...
- linux添加新硬盘
1.添加新磁盘 2.fdisk -l查看磁盘被识别的名称 3.如果输入fdisk -l命令没有找到新的磁盘,按下面步骤操作 1)进入到cd /sys/class/scsi_host/ 2)echo & ...