itext2.1.7操作word,A4大小中   4个表格,怎么上面2个表格 下面2个表格。谢谢了worditextjava web

解决方案 »

  1.   


    import java.awt.Color;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;import com.lowagie.text.BadElementException;
    import com.lowagie.text.Cell;
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Element;
    import com.lowagie.text.Font;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Phrase;
    import com.lowagie.text.Table;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.rtf.RtfWriter2;public class DoubleTable {
    private Document document;
    private BaseFont bfChinese;
    public BaseFont getBfChinese() {
    return bfChinese;
    } public void setBfChinese(BaseFont bfChinese) {
    this.bfChinese = bfChinese;
    } public Document getDocument() {
    return document;
    } public void setDocument(Document document) {
    this.document = document;
    } public DoubleTable() {
    this.document = new Document(PageSize.A4); }
    /**
     * @param filePath
     *            建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
     * @throws DocumentException
     * @throws IOException
     */
    public void openDocument(OutputStream out) throws DocumentException,
    IOException {
    // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
    RtfWriter2.getInstance(this.document, out);
    this.document.open();
    // 设置中文字体
    this.bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
    BaseFont.NOT_EMBEDDED);
    }
    /**
     * 关闭word文档
     * @throws DocumentException
     * @throws IOException
     */
    public void closeDocument() throws DocumentException {
    this.document.close();

    }
    public Table subTable(){
    //创建一个页面
    Table aTable=null;
    try {
    aTable = new Table(2, 6);
    //设置行样式
    // int width1[] = {100 };
    int width1[] = {40,60 };
    aTable.setWidths(width1);// 设置每列所占比例
    aTable.setWidth(100); // 占页面宽度 90%
    aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
    aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
    aTable.setAutoFillEmptyCells(true); // 自动填满
    aTable.setBorderWidth(1); // 边框宽度
    //aTable.setSpacing(0);

    Font fontChinese = new Font(bfChinese, 10, Font.BOLD);
    String[] titles={"序号","单位","姓名","申报级别","申报系列","备注"};
    for(int i=0;i<titles.length;i++){
    Cell cell = new Cell(new Phrase(titles[i], fontChinese));
    cell.setVerticalAlignment(Element.ALIGN_CENTER);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorderColor(new Color(0, 0, 0));
    aTable.addCell(cell);

    Cell cell2 = new Cell(new Phrase(""+i, fontChinese));
    cell2.setVerticalAlignment(Element.ALIGN_CENTER);
    cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell2.setBorderColor(new Color(0, 0, 0));
    aTable.addCell(cell2);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return aTable;
    }
    public void createTables(){
    try {
    //创建一个页面
    Table aTable = new Table(2, 2);
    //设置行样式
    int width1[] = { 50, 50 };
    aTable.setWidths(width1);// 设置每列所占比例
    aTable.setWidth(100); // 占页面宽度 
    aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
    aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
    aTable.setAutoFillEmptyCells(true); // 自动填满
    aTable.setBorderWidth(0); // 边框宽度
    //aTable.setSpaceInsideCell(10);
    aTable.setPadding(5);// aTable.setSpacing(10);
    for(int i=0;i<4;i++){
    Table sub=subTable();
    Cell cell = new Cell(sub);
    cell.setVerticalAlignment(Element.ALIGN_CENTER);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setUseBorderPadding(true);
    aTable.addCell(cell);
    }
    this.document.add(aTable);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String args[]){
    DoubleTable wt = new DoubleTable();
    try {
    wt.openDocument(new FileOutputStream("d:\\doubletable.doc"));
    wt.createTables();
    wt.closeDocument();
    } catch (DocumentException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }生成的word这个样子怎么解决边上外边距有角的问题,是itext的bug还是要弄什么?