繁体   English   中英

IText Center pdf中的标题

[英]IText Center the heading in pdf

我正在尝试使用以下程序创建报告。 一切正常,但我无法将标题“ 2014年11月5日的车辆可用性列表(VAL)”居中

我想使标题居中,也要在其周围留出间隔...理想情况下,它应该跨越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;
    }

}

好像您缺少对cell.setHorizontalAlignment( Element.ALIGN_CENTER);的调用cell.setHorizontalAlignment( Element.ALIGN_CENTER);

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

问题1:您想将文本水平居中(使用文本模式 )的单元格居中。

使用以下命令完成此操作:

cell.setHorizontalAlignment( Element.ALIGN_CENTER);

请注意,当您在compostie模式下使用单元时,此属性(水平对齐)将被忽略。 复合模式下 ,忽略在单元格级别定义的水平对齐方式 ,而倾向于在添加到该单元格的各个元素的级别进行对齐。

问题2:您希望文本以粗体显示。

当前,您正在创建这样的Phrase

Phrase phrase = new Phrase("VEHICLE AVAILABILITY LISTING (VAL) AS OF 11/05/2014");

这将创建一个字体为Helvetica(常规)的文本元素。 要使用粗体字体,您需要:

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。 那太荒谬了。 用HTML尝试一下,看看会发生什么。 行的高度不受影响(出于明显的原因)。 请从代码中删除以下行(因为它没有意义):

cell.setRowspan(5);

假设您使用的字体大小为12,则默认情况下该行的高度为18。 您需要大约5行的高度,因此需要90行,让我们添加一些填充并以100行的高度进行工作。在这种情况下,您应该添加以下行:

cell.setMinimumHeight(100);

奖金问题:

您可能会遇到文本在垂直方向上未完全居中的情况。 这是因为iText做出了一些假设。 有时,如果您告诉单元格考虑某些特定的字体指标(例如字体的Ascent和Descent),这会有所帮助:

cell.setUseAscender(真); cell.setUseDescender(真);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM