import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
DecimalFormat;
NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import Document;
import DocumentException;
import Element;
import Font;
import PageSize;
import Paragraph;
import Phrase;
import pdf.BaseFont;
import pdf.PdfCell;
import pdf.PdfPCell;
import pdf.PdfPRow;
import pdf.PdfPTable;
import pdf.PdfWriter;
import com.sun.java_cup.internal.internal_error;
public class PDFReport{
Document document = new Document();// 建立一个Document对象
private static Font headfont ;// 设置字体大小
private static Font keyfont;// 设置字体大小
private static Font textfont;// 设置字体大小
static{
BaseFont bfChinese;
try {
//bfChinese = ateFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
bfChinese = ateFont("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();
}
}
public PDFReport(File file) {
document.setPageSize(PageSize.A4);// 设置页面大小
try {
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
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);
}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);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable(){
PdfPTable table = new PdfPTable(1);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void generatePDF() throws Exception{
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) throws Exception {
text align centerFile file = new File("D:\\text.pdf");
new PDFReport(file).generatePDF();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论