我試圖創建使用下面的程序報告的標題。事情工作正常,但我無法居中標題「車輛可用性清單(瓦爾)截至2014年11月5日」iText的中心PDF
我想中心標題以及希望它周圍的空間......理想情況下,它應該跨度5列,以及居中並加粗..
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;
}
}
Thanks..1問題是固定的,但行跨度不起作用 – user1050619 2014-11-05 20:48:54