Java IText在pdf中居中显示标题
我正在尝试使用下面的程序创建报告。一切正常,但我无法将标题“截至2014年5月11日的车辆可用性列表(VAL)”居中 我想让标题居中,同时也希望标题周围有间距……理想情况下,标题应该跨越5列,居中且粗体Java IText在pdf中居中显示标题,java,itext,Java,Itext,我正在尝试使用下面的程序创建报告。一切正常,但我无法将标题“截至2014年5月11日的车辆可用性列表(VAL)”居中 我想让标题居中,同时也希望标题周围有间距……理想情况下,标题应该跨越5列,居中且粗体 import java.io.FileOutputStream; import java.io.IOException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayLis
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorld implements PdfPCellEvent, PdfPTableEvent{
/** Path to the resulting PDF file. */
public static final String RESULT
= "c:/itext/hello.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new HelloWorld().createPdf(RESULT);
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(getheaderTable(writer));
document.add(getTable());
// step 5
document.close();
}
public PdfPTable getheaderTable(PdfWriter writer) throws DocumentException, IOException{
HelloWorld gp = new HelloWorld();
PdfPTable headertable = new PdfPTable(new float[] { 5 });
headertable.setTableEvent(gp);
headertable.setWidthPercentage(100f);
headertable.getDefaultCell().setCellEvent(new HelloWorld());
PdfPCell cell = new PdfPCell(new Phrase("VEHICLE AVAILABILITY LISTING (VAL) AS OF 11/05/2014"));
cell.setColspan(5);
cell.setRowspan(5);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
headertable.addCell(cell);
return headertable;
}
public PdfPTable getTable() throws DocumentException, IOException {
HelloWorld gp = new HelloWorld();
PdfPTable table = new PdfPTable(new float[] { 5, 1, 1, 1});
table.setTableEvent(gp);
table.setWidthPercentage(100f);
table.getDefaultCell().setPadding(5);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
table.getDefaultCell().setCellEvent(gp);
for (int i = 0; i < 1; i++) {
table.addCell("CARS");
table.addCell("MODEL");
table.addCell("OPENDATE");
table.addCell("CLOSEOUT DATE");
}
table.getDefaultCell().setBackgroundColor(null);
//table.setHeaderRows(1);
//table.setFooterRows(1);
List<Car> cars = new ArrayList<Car>();
java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));
Car c = new Car("SUV 4 * 4", "FORD ENDEAVOR",new Timestamp(date.getTime()),new Timestamp(date.getTime()));
System.out.println(c);
cars.add(c);
cars.add(new Car("TRUCK 4 * 4", "GM CHEVY",new Timestamp(date.getTime()),new Timestamp(date.getTime())));
for (Car car : cars) {
table.addCell(car.getItemDesc());
table.addCell(car.getModel());
table.addCell(new SimpleDateFormat("MM/dd/yyyy").format(car.getOpen_Date()));
table.addCell(new SimpleDateFormat("MM/dd/yyyy").format(car.getCloseout_Date()));
}
return table;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
float x1 = position.getLeft() + 2;
float x2 = position.getRight() - 2;
float y1 = position.getTop() - 2;
float y2 = position.getBottom() + 2;
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
canvas.stroke();
}
public void tableLayout(PdfPTable table, float[][] width, float[] height,
int headerRows, int rowStart, PdfContentByte[] canvas) {
float widths[] = width[0];
float x1 = widths[0];
float x2 = widths[widths.length - 1];
float y1 = height[0];
float y2 = height[height.length - 1];
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.rectangle(x1, y1, x2 - x1, y2 - y1);
cb.stroke();
cb.resetRGBColorStroke();
}
}
class Car{
protected String ItemDesc;
protected String Model;
protected Timestamp Open_Date;
protected Timestamp Closeout_Date;
public Car(String ItemDesc, String Model,Timestamp Open_Date, Timestamp Closeout_Date){
this.ItemDesc = ItemDesc;
this.Model = Model;
this.Open_Date = Open_Date;
this.Closeout_Date = Closeout_Date;
}
public String getItemDesc() {
return ItemDesc;
}
public void setItemDesc(String itemDesc) {
ItemDesc = itemDesc;
}
public String getModel() {
return Model;
}
public void setModel(String model) {
Model = model;
}
public Timestamp getOpen_Date() {
return Open_Date;
}
public void setOpen_Date(Timestamp open_Date) {
Open_Date = open_Date;
}
public Timestamp getCloseout_Date() {
return Closeout_Date;
}
public void setCloseout_Date(Timestamp closeout_Date) {
Closeout_Date = closeout_Date;
}
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入java.sql.Timestamp;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.List;
导入com.itextpdf.text.Document;
导入com.itextpdf.text.DocumentException;
导入com.itextpdf.text.Element;
导入com.itextpdf.text.paragration;
导入com.itextpdf.text.Phrase;
导入com.itextpdf.text.Rectangle;
导入com.itextpdf.text.pdf.PdfContentByte;
导入com.itextpdf.text.pdf.PdfPCell;
导入com.itextpdf.text.pdf.PdfPCellEvent;
导入com.itextpdf.text.pdf.PdfPTable;
导入com.itextpdf.text.pdf.PdfPTableEvent;
导入com.itextpdf.text.pdf.PdfWriter;
/**
*第一个iText示例:Hello World。
*/
公共类HelloWorld实现PdfPCellEvent、PdfPTableEvent{
/**生成的PDF文件的路径*/
公共静态最终字符串结果
=“c:/itext/hello.pdf”;
/**
*创建一个PDF文件:hello.PDF
*@param args不需要参数
*/
公共静态void main(字符串[]args)
抛出DocumentException,IOException{
新建HelloWorld().createPdf(结果);
}
/**
*创建PDF文档。
*@param filename新PDF文档的路径
*@DocumentException
*@抛出异常
*/
public void createPdf(字符串文件名)
抛出DocumentException,IOException{
//第一步
文档=新文档();
//步骤2
getInstance(文档,新文件输出流(文件名));
PdfWriter writer=PdfWriter.getInstance(文档,新文件输出流(文件名));
//步骤3
document.open();
//步骤4
document.add(getheaderTable(writer));
add(getTable());
//步骤5
document.close();
}
公共PdfPTable getheaderTable(PdfWriter writer)引发DocumentException、IOException{
HelloWorld gp=新HelloWorld();
PdfPTable headertable=新PdfPTable(新浮点[]{5});
headertable.setTableEvent(gp);
头表。设置宽度百分比(100f);
headertable.getDefaultCell().setCellEvent(新HelloWorld());
PdfPCell cell=新的PdfPCell(新短语(“截至2014年5月11日的车辆可用性清单”);
细胞集落数(5);
cell.setRowspan(5);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
headertable.addCell(cell);
返回标题表;
}
public PdfPTable getTable()引发DocumentException,IOException{
HelloWorld gp=新HelloWorld();
PdfPTable table=新PdfPTable(新浮点[]{5,1,1,1});
表.可设置事件(gp);
表1.设置宽度百分比(100f);
table.getDefaultCell().setPadding(5);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
table.getDefaultCell().setCellEvent(gp);
对于(int i=0;i<1;i++){
表2.addCell(“CARS”);
表2.addCell(“模型”);
表1.addCell(“开放日期”);
表1.addCell(“收尾日期”);
}
table.getDefaultCell().setBackgroundColor(null);
//表2.setHeaderRows(1);
//表3.1行(1);
List cars=new ArrayList();
java.util.Date Date=新建java.util.Date();
System.out.println(新的时间戳(date.getTime());
Car c=新车(“SUV 4*4”,“福特奋进”,新时间戳(date.getTime()),新时间戳(date.getTime());
系统输出打印ln(c);
加上(c);
添加(新车(“卡车4*4”,“通用雪佛兰”,新时间戳(date.getTime()),新时间戳(date.getTime()));
用于(汽车:汽车){
table.addCell(car.getItemDesc());
table.addCell(car.getModel());
table.addCell(新的SimpleDataFormat(“MM/dd/yyyy”).format(car.getOpen_Date());
table.addCell(新的SimpleDataFormat(“MM/dd/yyyy”).format(car.getCloseout_Date());
}
返回表;
}
公共空心单元布局(PdfPCell单元,矩形位置,
PdfContentByte[]画布){
float x1=位置.getLeft()+2;
float x2=position.getRight()-2;
float y1=position.getTop()-2;
float y2=position.getBottom()+2;
PdfContentByte canvas=画布[PdfPTable.LINECANVAS];
矩形(x1,y1,x2-x1,y2-y1);
canvas.stroke();
}
公共空白表格布局(PdfPTable表格,浮动[]宽度,浮动[]高度,
int headerRows、int rowStart、PdfContentByte[]画布){
浮动宽度[]=宽度[0];
浮动x1=宽度[0];
浮动x2=宽度[宽度.长度-1];
浮动y1=高度[0];
浮动y2=高度[高度.长度-1];
PdfContentByte cb=canvas[PdfPTable.LINECANVAS];
cb.矩形(x1,y1,x2-x1,y2-y1);
cb.stroke();
cb.resetRGBColorStroke();
}
}
班车{
受保护字符串ItemDesc;
保护串模型;
受保护的时间戳打开日期;
受保护的时间戳收尾日期;
公共汽车(字符串ItemDesc、字符串模型、时间戳打开日期、时间戳关闭日期){
this.ItemDesc=ItemDesc;
这个模型=模型;
this.Open_Date=Open_Date;
this.Closeout\u Date=收尾日期;
}
公共字符串getItemDesc(){
返回ItemDesc;
}
公共void setItemDesc(字符串itemDesc){
ItemDesc=ItemDesc;
}
公共字符串getModel(){
收益模型;
}
公共void集合模型(字符串模型){
模型=模型;
}
公共时间戳getOpen_Date(){
返回打开日期;
}
公共作废设置打开日期(时间戳打开日期){
打开日期=打开日期;
}
公众的
cell.setHorizontalAlignment( Element.ALIGN_CENTER);
Phrase phrase = new Phrase("VEHICLE AVAILABILITY LISTING (VAL) AS OF 11/05/2014");
Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Phrase phrase = new Phrase("VEHICLE AVAILABILITY LISTING (VAL) AS OF 11/05/2014", bold);
cell.setRowspan(5);
cell.setMinimumHeight(100);