Java/PDF RTF/Table Cell Border
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Cell Border Colors
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class CellBorderColorsPDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellBorderColorsPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Paragraph("test colors:"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("red"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(Color.red);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("green"));
cell.setBorder(Rectangle.BOTTOM);
cell.setBorderColorBottom(Color.magenta);
cell.setBorderWidthBottom(10f);
cell.setBackgroundColor(Color.green);
table.addCell(cell);
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
Cell Border Colors With Background
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class CellBorderColorsWidthBackgroundPDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellBorderColorsWidthBackgroundPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Paragraph("blue"));
cell.setBorder(Rectangle.TOP);
cell.setUseBorderPadding(true);
cell.setBorderWidthTop(5f);
cell.setBorderColorTop(Color.cyan);
cell.setBackgroundColor(Color.blue);
table.addCell(cell);
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
Table Cell Border Color
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TableCellBorderColorPDF {
public static void main(String[] args) {
Document.rupress = false;
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("TableCellBorderColorPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell();
cell.addElement(new Chunk("cell "));
cell.setBorderColor(new Color(0xFF, 0x00, 0x00));
table.addCell("a cell");
table.addCell(cell);
table.addCell("a cell");
document.add(table);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Table Cell Border Width
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TableCellBorderWidthPDF {
public static void main(String[] args) {
Document.rupress = false;
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("TableCellBorderWidthPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell();
cell.addElement(new Chunk("cell "));
cell.setBorderWidth(10);
table.addCell("a cell");
table.addCell(cell);
table.addCell("a cell");
document.add(table);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Table Cell Format: NoBorder
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TableCellFormatNoBorderPDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableCellFormatNoBorderPDF.pdf"));
document.open();
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setFixedHeight(70);
table.addCell("!");
table.addCell("Text Text Text Text Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text Text Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text \nText \nText ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
table.addCell("Text ");
table.addCell(new Phrase(new Chunk("cell")));
document.add(table);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}