2014-11-05 141 views
0

我試圖創建使用下面的程序報告的標題。事情工作正常,但我無法居中標題「車輛可用性清單(瓦爾)截至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; 
    } 

} 

回答

0

問題1:要居中的在細胞內水平文本(使用文本模式)。

即使用做:

cell.setHorizontalAlignment(Element.ALIGN_CENTER); 

注意,這家酒店(水平對齊)將被忽略,你使用compostie模式細胞的時刻。在複合模式,在小區的級別上定義的水平取向,而傾向於使用對準的在添加到細胞中的各個元素的水平忽略。

問題2:您希望文本在大膽。

目前,正在創建一個Phrase這樣的:

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); 

問題3:需要特定的細胞有一個最小高度。

目前,正在創建具有5列的表。第一行包含一個帶有colspan 5的單元。因此,這個單元橫跨整行。你爲這個單元格定義一個5的rowspan。這是荒謬的。試試這在HTML中,看看會發生什麼。行的高度不受影響(原因很明顯)。請從您的代碼中刪除以下行(因爲它沒有意義):

cell.setRowspan(5); 

假設你正在使用大小爲12的字體,則該行的高度將是18默認。你想約5線的高度,所以你需要90,讓我們添加一些填充,並用100的高度。在這種情況下工作,你應該添加以下行:

cell.setMinimumHeight(100); 

獎金問題:

您可能會遇到文本沒有完全垂直居中。這是因爲iText做出的一些假設。如果您告訴單元格考慮某些特定字體指標(例如字體的上升和下降),有時這會有所幫助:cell.setUseAscender(true); 單元格。setUseDescender(真);

1

看起來像你缺少cell.setHorizontalAlignment(Element.ALIGN_CENTER);

呼叫您也可以使用cell.setColspan(5);

+0

Thanks..1問題是固定的,但行跨度不起作用 – user1050619 2014-11-05 20:48:54