Java Tutorial/PDF/Table Column — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:21, 1 июня 2010
Содержание
Create Table and set total width
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfPTable table = new PdfPTable(2);
float[] rows = { 50f, 250f };
table.setTotalWidth(rows);
for (int k = 0; k < 200; ++k) {
table.addCell("row " + k);
table.addCell("blah " + k);
}
document.add(new Paragraph("row 150 - 200"));
table.writeSelectedRows(150, -1, 150, 820, cb);
document.close();
}
}
Delete columns
import java.awt.Point;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Table table = new Table(2, 2);
table.setAlignment(Element.ALIGN_LEFT);
table.setAutoFillEmptyCells(true);
table.addCell("0.0");
table.addColumns(2);
float[] f = { 2f, 1f, 1f, 1f };
table.setWidths(f);
table.addCell("2.2", new Point(2, 2));
table.deleteColumn(2);
table.setConvert2pdfptable(true);
document.add(table);
document.close();
}
}
Set Column span
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
document.add(table);
document.close();
}
}
Table Column Widths
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
float[] widths1 = { 1f, 1f, 2f };
PdfPTable table = new PdfPTable(widths1);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
document.add(table);
float[] widths2 = { 2f, 1f, 1f };
table.setWidths(widths2);
document.add(table);
document.close();
}
}
Table header with column span
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 MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4, 36, 36, 36, 36);
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("");
table.addCell("");
float[] widths = { 72f, 72f, 144f };
Rectangle r = new Rectangle(PageSize.A4.right(72), PageSize.A4.top(72));
table.setWidthPercentage(widths, r);
document.add(table);
document.close();
}
}
Table with Absolute Columns Width
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4, 36, 36, 36, 36);
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
float[] widths = { 72f, 72f, 144f };
table.setTotalWidth(widths);
table.setLockedWidth(true);
document.add(table);
document.close();
}
}