给表格加背景 java_如何使用Java为PDF中的表设置背景?

本文介绍了如何使用Java iText库为PDF文档中的表格添加背景颜色。通过创建Table对象,设置单元格背景并添加到Document中,实现了自定义背景色的表格。示例代码详细展示了创建PDF表格并设置背景色的过程。
摘要由CSDN通过智能技术生成

如何使用Java为PDF中的表设置背景?

注:iText开发环境设置,下载iText7 jar(社区版:http://github.com/itext/itext7/releases/tag/7.0.4 ) ,创建一个工程:java_itext,并将下载的itext7 jar包和slf4j( http://www.slf4j.org/download.html )工具包添加到构建路径中。项目结构如下图所示 -

5fc47651a73bfcb49b5fa88d19698c1c.png

以下是使用Java为PDF中的表设置背景的程序。

package com.yiibai;

import com.itextpdf.kernel.color.Color;

import com.itextpdf.kernel.font.PdfFont;

import com.itextpdf.kernel.font.PdfFontFactory;

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.border.Border;

import com.itextpdf.layout.element.Cell;

import com.itextpdf.layout.element.Table;

import com.itextpdf.layout.property.TextAlignment;

public class BackgroundToTable {

public static void main(String args[]) throws Exception {

String file = "backgroundToTable.pdf";

// Creating a PdfDocument object

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));

// Creating a Document object

Document doc = new Document(pdfDoc);

// Creating a table

Table table = new Table(2);

// Adding row 1 to the table

Cell c1 = new Cell();

c1.add("姓名");

c1.setBackgroundColor(Color.PINK);

c1.setBorder(Border.NO_BORDER);

c1.setTextAlignment(TextAlignment.CENTER);

table.addCell(c1);

Cell c2 = new Cell();

c2.add("苏小牛");

c2.setBackgroundColor(Color.GRAY);

c2.setBorder(Border.NO_BORDER);

c2.setTextAlignment(TextAlignment.CENTER);

table.addCell(c2);

// Adding row 2 to the table

Cell c3 = new Cell();

c3.add("编号");

c3.setBackgroundColor(Color.WHITE);

c3.setBorder(Border.NO_BORDER);

c3.setTextAlignment(TextAlignment.CENTER);

table.addCell(c3);

Cell c4 = new Cell();

c4.add("1001");

c4.setBackgroundColor(Color.WHITE);

c4.setBorder(Border.NO_BORDER);

c4.setTextAlignment(TextAlignment.CENTER);

table.addCell(c4);

// Adding row 3 to the table

Cell c5 = new Cell();

c5.add("职位");

c5.setBackgroundColor(Color.ORANGE);

c5.setBorder(Border.NO_BORDER);

c5.setTextAlignment(TextAlignment.CENTER);

table.addCell(c5);

Cell c6 = new Cell();

c6.add("Java开发工程师");

c6.setBackgroundColor(Color.CYAN);

c6.setBorder(Border.NO_BORDER);

c6.setTextAlignment(TextAlignment.CENTER);

table.addCell(c6);

// 支持文中字体显示

PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", false);

table.setFont(font);

// Adding Table to document

doc.add(table);

// Closing the document

doc.close();

System.out.println("Background added successfully..");

}

}

执行上面示例代码,得到以下结果 -

Background added successfully..

输出文件内容如下所示 -

a6dc78d13b33e95d44af1d189060a6fe.png

¥ 我要打赏

纠错/补充

收藏

下一篇:哥,这回真没有了

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值