java cellformat,如何使用Java格式化表中单元格的内容。(How to format the content of a cell in a table using Java.)...

如何使用Java格式化表中单元格的内容。(How to format the content of a cell in a table using Java.)

问题描述 (Problem Description)

如何使用Java格式化表中单元格的内容。

解决方案 (Solution)

以下是使用Java格式化表中单元格内容的程序。import com.itextpdf.kernel.color.Color;

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 {

//Creating a PdfWriter object

String dest = "C:/itextExamples/addingBackground.pdf";

PdfWriter writer = new PdfWriter(dest);

//Creating a PdfDocument object

PdfDocument pdfDoc = new PdfDocument(writer);

//Creating a Document object

Document doc = new Document(pdfDoc);

//Creating a table

float [] pointColumnWidths = {200F, 200F};

Table table = new Table(pointColumnWidths);

//Populating row 1 and adding it to the table

Cell c1 = new Cell(); //Creating cell1

c1.add("Name"); //Adding name to cell1

c1.setBackgroundColor(Color.DARK_GRAY); //Setting background color

c1.setBorder(Border.NO_BORDER); //Setting border

c1.setTextAlignment(TextAlignment.CENTER); //Setting text alignment

table.addCell(c1); //Adding cell1 to the table

Cell c2 = new Cell();

c2.add("Raju");

c2.setBackgroundColor(Color.GRAY);

c2.setBorder(Border.NO_BORDER);

c2.setTextAlignment(TextAlignment.CENTER);

table.addCell(c2);

//Populating row 2 and adding it to the table

Cell c3 = new Cell();

c3.add("Id");

c3.setBackgroundColor(Color.WHITE);

c3.setBorder(Border.NO_BORDER);

c3.setTextAlignment(TextAlignment.CENTER);

table.addCell(c3);

Cell c4 = new Cell();

c4.add("001");

c4.setBackgroundColor(Color.WHITE);

c4.setBorder(Border.NO_BORDER);

c4.setTextAlignment(TextAlignment.CENTER);

table.addCell(c4);

//Populating row 3 and adding it to the table

Cell c5 = new Cell();

c5.add("Designation");

c5.setBackgroundColor(Color.DARK_GRAY);

c5.setBorder(Border.NO_BORDER);

c5.setTextAlignment(TextAlignment.CENTER);

table.addCell(c5);

Cell c6 = new Cell();

c6.add("Programmer");

c6.setBackgroundColor(Color.GRAY);

c6.setBorder(Border.NO_BORDER);

c6.setTextAlignment(TextAlignment.CENTER);

table.addCell(c6);

//Adding Table to document

doc.add(table);

//Closing the document

doc.close();

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

}

}

输出 (Output)

a0fb272991136ffaaff486a7d7bd31f1.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值