java 绘制pdf_Java生成pdf文件

packagecom.songyan.pdf;importjava.awt.Color;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.ArrayList;importjava.util.Date;importcom.lowagie.text.Document;importcom.lowagie.text.DocumentException;importcom.lowagie.text.Element;importcom.lowagie.text.Font;importcom.lowagie.text.PageSize;importcom.lowagie.text.Paragraph;importcom.lowagie.text.Phrase;importcom.lowagie.text.pdf.BaseFont;importcom.lowagie.text.pdf.PdfCell;importcom.lowagie.text.pdf.PdfPCell;importcom.lowagie.text.pdf.PdfPRow;importcom.lowagie.text.pdf.PdfPTable;importcom.lowagie.text.pdf.PdfWriter;public classPDFReport{

Document document= new Document();//建立一个Document对象

private static Font headfont ;//设置字体大小

private static Font keyfont;//设置字体大小

private static Font textfont;//设置字体大小

static{

BaseFont bfChinese;try{//bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

headfont= new Font(bfChinese, 10, Font.BOLD);//设置字体大小

keyfont = new Font(bfChinese, 8, Font.BOLD);//设置字体大小

textfont = new Font(bfChinese, 8, Font.NORMAL);//设置字体大小

} catch(Exception e) {

e.printStackTrace();

}

}publicPDFReport(File file) {

document.setPageSize(PageSize.A4);//设置页面大小

try{

PdfWriter.getInstance(document,newFileOutputStream(file));

document.open();

}catch(Exception e) {

e.printStackTrace();

}

}int maxWidth = 520;public PdfPCell createCell(String value,com.lowagie.text.Font font,intalign){

PdfPCell cell= newPdfPCell();

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setHorizontalAlignment(align);

cell.setPhrase(newPhrase(value,font));returncell;

}publicPdfPCell createCell(String value,com.lowagie.text.Font font){

PdfPCell cell= newPdfPCell();

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setPhrase(newPhrase(value,font));returncell;

}public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,intcolspan){

PdfPCell cell= newPdfPCell();

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setHorizontalAlignment(align);

cell.setColspan(colspan);

cell.setPhrase(newPhrase(value,font));returncell;

}public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan,booleanboderFlag){

PdfPCell cell= newPdfPCell();

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setHorizontalAlignment(align);

cell.setColspan(colspan);

cell.setPhrase(newPhrase(value,font));

cell.setPadding(3.0f);if(!boderFlag){

cell.setBorder(0);

cell.setPaddingTop(15.0f);

cell.setPaddingBottom(8.0f);

}returncell;

}public PdfPTable createTable(intcolNumber){

PdfPTable table= newPdfPTable(colNumber);try{

table.setTotalWidth(maxWidth);

table.setLockedWidth(true);

table.setHorizontalAlignment(Element.ALIGN_CENTER);

table.getDefaultCell().setBorder(1);

}catch(Exception e){

e.printStackTrace();

}returntable;

}public PdfPTable createTable(float[] widths){

PdfPTable table= newPdfPTable(widths);try{

table.setTotalWidth(maxWidth);

table.setLockedWidth(true);

table.setHorizontalAlignment(Element.ALIGN_CENTER);

table.getDefaultCell().setBorder(1);

}catch(Exception e){

e.printStackTrace();

}returntable;

}publicPdfPTable createBlankTable(){

PdfPTable table= new PdfPTable(1);

table.getDefaultCell().setBorder(0);

table.addCell(createCell("", keyfont));

table.setSpacingAfter(20.0f);

table.setSpacingBefore(20.0f);returntable;

}public void generatePDF() throwsException{

PdfPTable table= createTable(4);

table.addCell(createCell("学生信息列表:", keyfont,Element.ALIGN_LEFT,4,false));

table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));

table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));

table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));

table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));for(int i=0;i<5;i++){

table.addCell(createCell("姓名"+i, textfont));

table.addCell(createCell(i+15+"", textfont));

table.addCell(createCell((i%2==0)?"男":"女", textfont));

table.addCell(createCell("地址"+i, textfont));

}

document.add(table);

document.close();

}public static void main(String[] args) throwsException {

File file= new File("D:\\text.pdf");

file.createNewFile();newPDFReport(file).generatePDF();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值