需用的Jar包
https://download.csdn.net/download/GXSeveryday/12380111
PDFReport.java
package test;
import java.io.File;
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.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFReport {
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);
headfont = new Font(bfChinese, 20, Font.BOLD);
keyfont = new Font(bfChinese, 12, Font.BOLD);
textfont = new Font(bfChinese, 12, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public Document PDFReport(Document document,File file) {
document.setPageSize(PageSize.A4);
try {
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
int maxWidth = 520;
public PdfPCell createCell(String value,Font font,int align){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,Font font){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,Font font,int align,int colspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,Font font,int align,int colspan,boolean boderFlag){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,font));
cell.setPadding(3.0f);
if(!boderFlag){
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
public PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths){
PdfPTable table = new PdfPTable(widths);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable(){
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void generatePDF(File file) throws Exception{
Document document = new Document();
document = PDFReport(document,file);
PdfPTable table = createTable(4);
table.addCell(createCell("学生信息列表:", keyfont,Element.ALIGN_CENTER,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, keyfont,Element.ALIGN_LEFT,4,true));
}
document.add(table);
document.close();
}
public void generateOfflinePDF(File file) throws Exception{
Document document = new Document();
document = PDFReport(document,file);
PdfPTable table = createTable(4);
table.addCell(createCell("项目缴款通知书", headfont,Element.ALIGN_CENTER,4,false));
table.addCell(createCell("缴款人姓名:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("测试专用操作", textfont, Element.ALIGN_LEFT));
table.addCell(createCell("缴款人证件号码:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("510522", textfont, Element.ALIGN_LEFT));
table.addCell(createCell("凭证时间:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("2016-02-03", textfont, Element.ALIGN_LEFT));
table.addCell(createCell("凭证编号:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("112233", textfont, Element.ALIGN_LEFT));
table.addCell(createCell("项目名称:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("大周皇朝项目", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("标的名称:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("标的", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("应缴金额:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("15元", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("户名:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("王超", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("开户行:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("中国银行", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("账号:", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("622", textfont, Element.ALIGN_LEFT,3,true));
table.addCell(createCell("特别提醒:", keyfont, Element.ALIGN_LEFT,4,false));
String tj = " 您的报名信息已与以下账户信息绑定,请选择其中之一进行缴款,不能使用现金缴款。请选择以下注册账户之一进行缴款,若"
+ "使用以下账户之外的其他账户进行缴款,或选择两个以上注册账户进行组合缴款的,缴款人将无法取得竞买资格,"
+ "说缴款项将作为误进款于项目交易结束后三个工作日内原路径退回。";
table.addCell(createCell(tj, textfont, Element.ALIGN_LEFT,4,false));
String jzsj = "缴款截止日期:"+"2016-02-01";
table.addCell(createCell(jzsj, keyfont, Element.ALIGN_LEFT,4,false));
table.addCell(createCell("序号", textfont, Element.ALIGN_CENTER));
table.addCell(createCell("银行", textfont, Element.ALIGN_CENTER));
table.addCell(createCell("账号", textfont, Element.ALIGN_CENTER,2,true));
table.addCell(createCell("1", textfont, Element.ALIGN_CENTER));
table.addCell(createCell("中国银行", textfont, Element.ALIGN_CENTER));
table.addCell(createCell("510522", textfont, Element.ALIGN_CENTER,2,true));
table.addCell(createCell("收款账户确认:", keyfont, Element.ALIGN_LEFT,4,false));
String tx = " 项目经理已告知中国建设银行、中国工商银行、重庆农村商业银行可选择为收款银行。本人选择"+"中国银行"+",特此确定。";
table.addCell(createCell(tx, textfont, Element.ALIGN_LEFT,4,false));
table.addCell(createCell("缴款人经办人签字:", keyfont, Element.ALIGN_LEFT,4,false));
document.add(table);
document.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\text.pdf");
file.createNewFile();
new PDFReport().generateOfflinePDF(file);
}
}
更多学习资讯,学习源码,请在耶宇星球领取