Java Tutorial/PDF/Table
Содержание
- 1 Add table cell to table
- 2 Generates a PDF file with a table
- 3 Nested Pdf Table
- 4 Pdf Table Split Vertically
- 5 Pdf Table with Absolute Width
- 6 Pdf Table Without Borders
- 7 Set Spacing Before/After a table
- 8 Skip header
- 9 Table Aligned
- 10 Use Table to create Report header
- 11 We need 4 cells with rowspan 2
Add table cell to table
<source lang="java">
import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; 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.rotate()); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); PdfPTable datatable = new PdfPTable(10); int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(5); datatable.setHeaderRows(2); datatable.getDefaultCell().setBorderWidth(1); for (int i = 1; i < 30; i++) { datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); datatable.addCell("myUserId"); datatable.addCell("long long name"); datatable.addCell("No Name Company"); datatable.addCell("D" + i); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (int j = 0; j < 6; j++) datatable.addCell(Math.random() > .5 ? "Yes" : "No"); } datatable.setSplitLate(false); document.add(datatable); document.close(); }
}</source>
Generates a PDF file with a table
<source lang="java">
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")); 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(); }
}</source>
Nested Pdf Table
<source lang="java">
import java.io.FileOutputStream; 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 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(4); PdfPTable nested1 = new PdfPTable(2); nested1.addCell("1.1"); nested1.addCell("1.2"); PdfPTable nested2 = new PdfPTable(1); nested2.addCell("20.1"); nested2.addCell("20.2"); for (int k = 0; k < 24; ++k) { if (k == 1) { table.addCell(nested1); } else if (k == 20) { table.addCell(new PdfPCell(nested2)); } else { table.addCell("cell " + k); } } document.add(table); document.close(); }
}</source>
Pdf Table Split Vertically
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; 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(); PdfPTable table = new PdfPTable(10); for (int k = 1; k <= 100; ++k) { table.addCell("number " + k); } table.setTotalWidth(800); table.writeSelectedRows(0, 5, 0, -1, 50, 650, writer.getDirectContent()); document.newPage(); table.writeSelectedRows(5, -1, 0, -1, 50, 650, writer.getDirectContent()); document.close(); }
}</source>
Pdf Table with Absolute Width
<source lang="java">
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); table.setTotalWidth(216f); table.setLockedWidth(true); 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); document.close(); }
}</source>
Pdf Table Without Borders
<source lang="java">
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); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); 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); document.close(); }
}//</source>
Set Spacing Before/After a table
<source lang="java">
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 with colspan 3")); cell.setColspan(3); table.addCell(cell); table.addCell("2"); table.setSpacingBefore(15f); document.add(table); table.setSpacingAfter(10f); document.add(table); document.close(); }
}</source>
Skip header
<source lang="java">
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(2); table.setWidthPercentage(100); table.setHeaderRows(1); PdfPCell h1 = new PdfPCell(new Paragraph("Header 1")); h1.setGrayFill(0.7f); table.addCell(h1); PdfPCell h2 = new PdfPCell(new Paragraph("Header 2")); h2.setGrayFill(0.7f); table.addCell(h2); PdfPCell cell; for (int row = 1; row <= 2000; row++) { document.add(table); table.deleteBodyRows(); table.setSkipFirstHeader(true); cell = new PdfPCell(new Paragraph(String.valueOf(row))); table.addCell(cell); cell = new PdfPCell(new Paragraph("Quick brown fox jumps over the lazy dog.")); table.addCell(cell); } document.add(table); document.close(); }
}</source>
Table Aligned
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.Element; 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 with colspan 3")); cell.setColspan(3); table.addCell(cell); table.addCell("1"); table.addCell("2"); table.addCell("3"); table.setWidthPercentage(100); document.add(table); table.setWidthPercentage(50); table.setHorizontalAlignment(Element.ALIGN_RIGHT); document.add(table); table.setHorizontalAlignment(Element.ALIGN_LEFT); document.add(table); document.close(); }
}</source>
Use Table to create Report header
<source lang="java">
import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; 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.rotate()); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); PdfPTable datatable = new PdfPTable(10); int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(5); PdfPCell cell = new PdfPCell(new Phrase("Report", FontFactory .getFont(FontFactory.HELVETICA, 24, Font.BOLD))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2); cell.setColspan(10); cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); cell.setUseDescender(true); datatable.addCell(cell); document.add(datatable); document.close(); }
}</source>
We need 4 cells with rowspan 2
<source lang="java">
import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; 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.rotate()); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); PdfPTable datatable = new PdfPTable(10); int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(5);
datatable.getDefaultCell().setBorderWidth(2); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); datatable.addCell("User Id"); datatable.addCell("Name\nAddress"); datatable.addCell("Company"); datatable.addCell("Department");
PdfPTable permissions = new PdfPTable(6); permissions.getDefaultCell().setBorderWidth(2); permissions.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); permissions.getDefaultCell().setColspan(6); permissions.addCell("Permissions"); permissions.getDefaultCell().setColspan(1); permissions.addCell("Admin"); permissions.addCell("Data"); permissions.addCell("Expl"); permissions.addCell("Prod"); permissions.addCell("Proj"); permissions.addCell("Online"); PdfPCell permission = new PdfPCell(permissions); permission.setColspan(6); datatable.addCell(permission); document.add(datatable); document.close(); }
}</source>