- iText 教程
- iText - 首页
- iText 画布
- iText - 绘制弧线
- iText - 绘制直线
- iText - 绘制圆形
- iText 有用资源
- iText - 快速指南
- iText - 有用资源
- iText - 讨论
iText - 单元格边框的格式化
本章我们将学习如何使用 iText 库格式化表格中单元格的边框。
单元格边框的格式化
您可以通过实例化Document类来创建一个空的 PDF 文档。实例化此类时,需要将PdfDocument对象作为参数传递给其构造函数。
然后,要向文档添加表格,需要实例化Table类并将此对象使用add()方法添加到文档中。
您可以使用Cell类的setBorder()方法添加各种类型的边框,例如DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder等,并使用各种颜色。
以下是格式化表格中单元格边框的步骤。
步骤 1:创建 PdfWriter 对象
PdfWriter类表示 PDF 的 DocWriter。此类属于com.itextpdf.kernel.pdf包。此类的构造函数接受一个字符串,表示要创建 PDF 文件的路径。
通过向其构造函数传递一个字符串值(表示您需要创建 PDF 的路径)来实例化 PdfWriter 类,如下所示。
// Creating a PdfWriter String dest = "C:/itextExamples/coloredBorders.pdf"; PdfWriter writer = new PdfWriter(dest);
当此类型的对象传递给 PdfDocument(类)时,添加到此文档的每个元素都将写入指定的文件。
步骤 2:创建 PdfDocument 对象
PdfDocument类是表示 iText 中 PDF 文档的类。此类属于com.itextpdf.kernel.pdf包。要实例化此类(以写入模式),需要将PdfWriter类的对象传递给其构造函数。
通过将上面创建的PdfWriter对象传递给其构造函数来实例化PdfDocument类,如下所示。
// Creating a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
创建 PdfDocument 对象后,您可以使用其类提供的相应方法添加各种元素,例如页面、字体、文件附件和事件处理程序。
步骤 3:创建 Document 对象
com.itextpdf.layout包中的Document类是创建自包含 PDF 时的根元素。此类的一个构造函数接受PdfDocument类的对象。
通过传递在前面步骤中创建的PdfDocument类的对象来实例化Document类,如下所示。
// Creating a Document Document document = new Document(pdfDoc);
步骤 4:创建 Table 对象
Table类表示一个二维网格,其中填充了按行和列排序的单元格。它属于com.itextpdf.layout.element包。
如下所示实例化Table类。
// Creating a table
float [] pointColumnWidths = {200F, 200F};
Table table = new Table(pointColumnWidths);
步骤 5:创建单元格
通过实例化com.itextpdf.layout.element包中的Cell类来创建一个单元格对象,并使用Cell类的add()方法添加单元格的内容,如下所示。
// Adding cell 1 to the table
Cell cell1 = new Cell(); // Creating a cell
cell1.add("Name"); // Adding content to the cell
步骤 6:格式化单元格的边框
iText 库提供了表示边框的各种类,例如DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder等。
这些类的构造函数接受两个参数:一个表示边框颜色的color对象和一个表示边框宽度的整数。
选择其中一种边框类型,并通过传递color对象和表示宽度的整数来实例化相应的边框,如下所示。
Border b1 = new DashedBorder(Color.RED, 3);
现在,使用cell类的setBorder()方法设置单元格的边框。此方法接受Border类型的对象作为参数。
通过将上面创建的Border对象作为参数传递给setBorder()方法来设置单元格的边框,如下所示。
c1.setBorder(b1)
最后,要将此单元格添加到表格中,请调用Table类的addCell()方法并将cell对象作为参数传递给此方法,如下所示。
table.addCell(c1);
步骤 7:将表格添加到文档
使用Document类的add()方法添加上一步中创建的table对象,如下所示。
// Adding list to the document document.add(table);
步骤 8:关闭文档
使用Document类的close()方法关闭文档,如下所示。
// Closing the document document.close();
示例
下面的 Java 程序演示了如何使用 iText 库格式化表格中单元格的边框。它创建一个名为coloredBorders.pdf的 PDF 文档,向其中添加一个表格,格式化其单元格的内容,并将其保存在C:/itextExamples/路径中。
将此代码保存在名为FormatedBorders.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.border.DashedBorder;
import com.itextpdf.layout.border.DottedBorder;
import com.itextpdf.layout.border.DoubleBorder;
import com.itextpdf.layout.border.RoundDotsBorder;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
public class FormatedBorders {
public static void main(String args[]) throws Exception {
// Creating a PdfWriter object
String dest = "C:/itextExamples/coloredBorders.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);
// Adding row 1 to the table
Cell c1 = new Cell();
// Adding the contents of the cell
c1.add("Name");
// Setting the back ground color of the cell
c1.setBackgroundColor(Color.DARK_GRAY);
// Instantiating the Border class
Border b1 = new DashedBorder(Color.RED, 3);
// Setting the border of the cell
c1.setBorder(b1);
// Setting the text alignment
c1.setTextAlignment(TextAlignment.CENTER);
// Adding the cell to the table
table.addCell(c1);
Cell c2 = new Cell();
c2.add("Raju");
c1.setBorder(new SolidBorder(Color.RED, 3));
c2.setTextAlignment(TextAlignment.CENTER);
table.addCell(c2);
// Adding row 2 to the table
Cell c3 = new Cell();
c3.add("Id");
c3.setBorder(new DottedBorder(Color.DARK_GRAY, 3));
c3.setTextAlignment(TextAlignment.CENTER);
table.addCell(c3);
Cell c4 = new Cell();
c4.add("001");
c4.setBorder(new DoubleBorder(Color.DARK_GRAY, 3));
c4.setTextAlignment(TextAlignment.CENTER);
table.addCell(c4);
// Adding row 3 to the table
Cell c5 = new Cell();
c5.add("Designation");
c5.setBorder(new RoundDotsBorder(Color.RED, 3));
c5.setTextAlignment(TextAlignment.CENTER);
table.addCell(c5);
Cell c6 = new Cell();
c6.add("Programmer");
c6.setBorder(new RoundDotsBorder(Color.RED, 3));
c6.setTextAlignment(TextAlignment.CENTER);
table.addCell(c6);
// Adding Table to document
doc.add(table);
// Closing the document
doc.close();
System.out.println("Borders added successfully..");
}
}
使用以下命令从命令提示符编译并执行保存的 Java 文件:
javac FormatedBorders.java java FormatedBorders
执行后,上述程序将创建一个 PDF 文档,显示以下消息。
Borders added successfully
如果您验证指定的路径,则可以找到已创建的 PDF 文档,如下所示。