Itext5 rowspan 可以跨行

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

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

img_fd61c533127466a5ca6fe9dfe67ca674.jpg


img_21577b492f3c968ac5b2359a39c075ef.jpg


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 中文字体

目录
相关文章
|
Java API Android开发
【POI框架实战】——POI设置Excel单元格格式
 “这个excel中的数据怎么不能做加法啊?”、“标头这一行的字体怎么这么小啊,我都看不清”、“这一列能不能换个颜色,明显一些”、“你把这一列的数据给我留两个小数位。”、“这些数据能不能以货币的类型展示啊,就每个三位一个小逗号那种……”
【POI框架实战】——POI设置Excel单元格格式
|
5月前
poi-tl循环表格列和行
poi-tl循环表格列和行
270 0
|
4月前
POI(excel)中ROW应用实践应用总结
POI(excel)中ROW应用实践应用总结
57 2
|
5月前
table合并单元格 colspan(跨列)和rowspan(跨行)
table合并单元格 colspan(跨列)和rowspan(跨行)
element UI table合并行合并列(整理)
element UI table合并行合并列(整理)
|
8月前
|
前端开发
ant design Row col样式修改
ant design Row col样式修改
48 0
ant design Row col样式修改
|
10月前
|
Java BI
iText导出PDF多表格
iText导出PDF多表格
151 0
poi 生成word 表格,并向表格单元格中插入多个图片
poi 生成word 表格,并向表格单元格中插入多个图片
311 0
poi 生成word 表格,并向表格单元格中插入多个图片
【Excel自动化办公Part5】:设置行高和列宽、合并单元格、取消合并单元格
【Excel自动化办公Part5】:设置行高和列宽、合并单元格、取消合并单元格
168 0
【Excel自动化办公Part5】:设置行高和列宽、合并单元格、取消合并单元格