Itext5 rowspan 可以跨行

一直在网上查的都是说Itext5的 rowspan 不能跨行,只能colspan跨列,也就信了,一直相信网上的东西,因为说的人多了,可是!!今天我读到Itext in action 第二版  第四章的第二小节的时候,发现,rowspan是可以跨行的并且还能居中对齐!!擦,代码奉上。



package com.rs10.textPdf;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Font;

import com.itextpdf.text.Phrase;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfPCell;

import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;

import com.lowagie.text.Element;

public classTestPdf {

    public static void main(String[] args) throws IOException, DocumentException{

        String fileDesc = "";

        FileOutputStream outr = null;// 创建输出流

       // 生成随机数

        Random random = newRandom();

        intx = random.nextInt();

        fileDesc = "c:\\pdftest\\"+ "_"+ x + ".pdf";//路径下一定要有此文件

        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        Font fontChinese =  new Font(baseFontChinese , 12 , Font.NORMAL);

        // step 1

        Document document = newDocument();

        // step 2

        try{

            outr= newFileOutputStream(fileDesc);

            PdfWriter.getInstance(document, outr);

           // step 3

            document.open();

            // step 4

            PdfPTable table = newPdfPTable(3);

            // the cell object

            PdfPCell cell;

            // we add a cell with colspan3

            cell = newPdfPCell(newPhrase("Cell with colspan 3"));

            cell.setColspan(3);

            table.addCell(cell);

            // now we add a cell with rowspan2

            cell = newPdfPCell(newPhrase("Cell with rowspan 2 跨行",fontChinese));

            cell.setRowspan(2);

            //cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

            table.addCell(cell);

            // we add the four remaining cells with addCell()

            table.addCell("row 1-1; cell 1");

            table.addCell("row 1-2; cell 2");

            table.addCell("row 2-1; cell 1");

            table.addCell("row 2-2; cell 2");

            document.add(table);

            document.close();

       } catch(DocumentException e1) {

            e1.printStackTrace();

       } finally{

            if(outr != null) {

                outr.close();

            }

            String cmd = "\"C:\\Program Files\\Foxit Software\\FoxitReader\\Foxit Reader.exe\" ";//这个是命令行打开福昕阅读器,我的福昕安装路径,你可以仿造我的写一个。

            System.out.println(cmd + "\""+ fileDesc + "\""+ " -n");

            Runtime.getRuntime().exec(cmd+ "\""+ fileDesc + "\""+ " -n");

            //打开pdf

       }

    }

}

-------------------------------

需要的jar包 : 

itext-asian.jar 主要jar包

itext-xtra-5.2.1.jar 扩展jar包

itext-asian.jar 中文字体

原文地址:https://www.cnblogs.com/ae6623/p/4416642.html