近来项目完成自己学些itext打印pdf,主要是因为普通打印不能满足要求,比如有的用户要求我要打印查询出来所有的记录,而用普通的页面打印方式又只能打印当前页面,如果数据是四五个页面的大小普通打印就不能满足要求了,以前也用过免费的web打印,但是还是会有些问题不能解决,更为要命的是别人的东西拿来就用,能满足要求固然是好事,否则你的麻烦就要来了。一旦出现错误就没有办法补救了,所以还是得自己想办法用自己会的东西才是最安全的。
itext 做pdf其实也比较简单,最重要的就是个Document对象,把他的生成过程了解清楚就可以自己动手写东西。
下面是我的代码:
package com.xuning.pdf.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
/**
* itext 实现pdf打印功能,这里我没有写成action或是servlet类
* 如果要设置成可以在页面上进行下载的就要在action 或是servlet中进行设置
* 设置成可下载的就可以了。还有一种方式也是比较好的,就是加载xml文件的形式进行
* 生成pdf文件,不过这种方式你要写各种类来解释xml各种元素。
* @author 徐宁 luoyuexiaoxiao@yahoo.com.cn
*/
public class PdfExport extends PdfPageEventHelper {
public PdfTemplate tpl;
public BaseFont bf;
private static BaseFont bfChinese;
public static BaseFont arial;
private static Font fontCN;
private static Font fontCN1;
private static Font font;
private static Font font1;
//类执行时就初始化这个块
static {
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
fontCN = new com.lowagie.text.Font(bfChinese, 14,com.lowagie.text.Font.BOLD);
fontCN1 = new com.lowagie.text.Font(bfChinese, 8,com.lowagie.text.Font.NORMAL);
font = new Font(bfChinese, 12f, Font.BOLD);
font1 = new Font(bfChinese, 10f, Font.NORMAL);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
PdfExport pe = new PdfExport();
Document document = new Document(PageSize.A4);
try {
FileOutputStream os = new FileOutputStream("批量查询结果列表.pdf");
PdfWriter writer = PdfWriter.getInstance(document, os);
writer.setPageEvent(new PdfExport());
writer.setViewerPreferences(PdfWriter.HideMenubar);
document.open();
PdfPTable tab1 = pe.setTableBody(new ArrayList(0));
document.add(tab1);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (Exception de) {
de.printStackTrace();
} finally {
if (document != null) {
document.close();
}
}
}
/**
* 设置标题方法,实际应用中通过传入参数进行设置
* @return
*/
private PdfPTable setTiteAndDate(String titleName){
PdfPTable titleTable = new PdfPTable(1);
int table[] = { 100 };
try {
titleTable.setWidths(table);
titleTable.setWidthPercentage(100);
titleTable.setSpacingBefore(5f);// 设置标题和第一个表格间的距离.不然会粘在一起
PdfPCell pdfcell = new PdfPCell(new Phrase(titleName, fontCN));
pdfcell.setBorder(0);
pdfcell.setHorizontalAlignment(Element.ALIGN_CENTER);
titleTable.addCell(pdfcell);
PdfPCell pdfcell1 = new PdfPCell(new Phrase("日期:"+ getDifferentTime(null), fontCN1));
pdfcell1.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfcell1.setBorder(0);
titleTable.addCell(pdfcell1);
} catch (Exception e) {
e.printStackTrace();
}
return titleTable;
}
itext实现pdf打印之一
最新推荐文章于 2023-02-15 21:21:28 发布