2014-11-05 99 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