如何在ITEXT中动态增加ColumnTexts的高度?

时间:2014-12-28 06:39:37

标签: java height itext

我正在尝试在文档中添加2个columnTexts,例如ct& CT1。

如果ct高度自动增加,ct1应根据高度进行相应调整。

我尝试过这种方式,

public static void main(String[] args) throws DocumentException, IOException {
        ItextHieghtTest i = new ItextHieghtTest();
        i.setTableHght();
    }

    void setTableHght() throws DocumentException, IOException
    {
        Rectangle pageSize = PageSize.LETTER.rotate();
        float text1_Hieght = 0;

        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("NextLineTextPDF.pdf"));
        doc.setPageSize(pageSize);
        doc.open();

        PdfContentByte cb = writer.getDirectContent();

        float x = 6.47f * 72f;
        float y = pageSize.getHeight() - (1.13f * 72f);

        Font blue = new Font(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED),10f,0,Color.BLUE);
        Chunk greenText = new Chunk("22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_2222222222222222222222222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222Text_22222222222222222222", blue);

        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(x, y, 750, 50);
        Paragraph p2 = new Paragraph();
        p2.add(greenText);
        ct.addElement(p2);
        text1_Hieght = ColumnText.getWidth(p2);
        ct.go();

        pageSize = PageSize.LETTER.rotate();
        float y1  = pageSize.getHeight() - (text1_Hieght/100+ 2.000f * 72f);
        if(text1_Hieght >750 && text1_Hieght <1450)
            y1 = pageSize.getHeight() - (text1_Hieght/100+ 2.222f * 72f);
        if(text1_Hieght >1450 && text1_Hieght <1750)
            y1 = pageSize.getHeight() - (text1_Hieght/100 + 2.444f * 72f);
        if(text1_Hieght >1750 && text1_Hieght <2050)
            y1 = pageSize.getHeight() - (text1_Hieght/100 + 2.666f * 72f);
        if(text1_Hieght >2050 && text1_Hieght <2350)
            y1 = pageSize.getHeight() - (text1_Hieght/100 + 2.888f * 72f);
        if(text1_Hieght >2350 && text1_Hieght <2550)
            y1 = pageSize.getHeight() - (text1_Hieght/100 + 3.000f * 72f);
        System.out.println("text1_Hieght " + text1_Hieght + "&" + y1) ;

        PdfContentByte cb1 = writer.getDirectContent();
        ColumnText ct1 = new ColumnText(cb1);
        ct1.setAlignment(Element.ALIGN_JUSTIFIED);
        ct1.setSimpleColumn(x, 1f * 7, 10.5f * 72f, y1);
        ct1.setYLine(y1);
        ct1.addElement(new Chunk("text_3333333333333333333333333333333"));
        ct1.go();

        doc.close();
    }

但问题是,如果text1_Hieght> 2350,我的ct1高度不是自动调整,而是由ct覆盖了。

如果ct高度增加,有人可以自动动态调整ct1吗?

由于

0 个答案:

没有答案