Java/PDF RTF/Table Cell Size
Содержание
Cell Fixed Height
<source lang="java">
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 CellFixedHeightPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellFixedHeightPDF.pdf")); document.open(); PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Paragraph("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ")); table.addCell("wrap"); cell.setNoWrap(false); table.addCell(cell); table.addCell("no wrap"); cell.setFixedHeight(50f); cell.setNoWrap(true); table.addCell(cell); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Cell Heights Warp And NoWrap
<source lang="java">
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 CellHeightsWarpAndNoWrapPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellHeightsWarpAndNoWrapPDF.pdf")); document.open(); PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Paragraph("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ")); table.addCell("wrap"); cell.setNoWrap(false); table.addCell(cell); table.addCell("no wrap"); cell.setNoWrap(true); table.addCell(cell); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Cell Minimum Height
<source lang="java">
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 CellMinimumHeightPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellMinimumHeightPDF.pdf")); document.open(); PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Paragraph("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ")); table.addCell("wrap"); cell.setNoWrap(false); table.addCell(cell); table.addCell("no wrap"); cell.setMinimumHeight(50f); cell.setNoWrap(true); table.addCell(cell); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Cells Fit Page
<source lang="java">
import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfWriter; public class CellsFitPagePDF {
public static void main(String[] args) { Document document = new Document(PageSize.A6); try { PdfWriter.getInstance(document, new FileOutputStream("CellsFitPagePDF.pdf")); document.open(); Table table = new Table(2); table.setCellsFitPage(true); table.addCell("text text text text text text text text text text text text "); table.addCell("text"); table.addCell("text"); table.addCell("text"); document.add(table); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
}
</source>
Changing Cell Widths
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class ChangingCellWidthsPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4, 36, 36, 36, 36); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ChangingCellWidthsPDF.pdf")); document.open(); float[] widths = {0.1f, 0.1f, 0.05f, 0.75f}; PdfPTable table = new PdfPTable(widths); table.addCell("10%"); table.addCell("10%"); table.addCell("5%"); table.addCell("75%"); table.addCell("111111111111111111111111111"); table.addCell("111111111111111"); table.addCell("11111"); table.addCell("11"); document.add(table); widths[0] = 40f; widths[1] = 40f; widths[2] = 10f; widths[3] = 10f; table.setWidths(widths); table.addCell("111111111111111111111111111"); table.addCell("111111111111111"); table.addCell("11111"); table.addCell("11"); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Locking table Cell Widths
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class LockingCellWidthsPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4, 36, 36, 36, 36); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LockingCellWidthsPDF.pdf")); document.open(); float[] widths = {0.1f, 0.1f, 0.05f, 0.75f}; PdfPTable table = new PdfPTable(widths); table.addCell("10%"); table.addCell("10%"); table.addCell("5%"); table.addCell("75%"); table.addCell("111111111111111111111111111"); table.addCell("111111111111111"); table.addCell("11111"); table.addCell("11"); table.setTotalWidth(300); table.setLockedWidth(true); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Setting Cell Widths
<source lang="java">
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.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class CellWidthsPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4, 36, 36, 36, 36); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CellWidthsPDF.pdf")); document.open(); float[] widths = {0.1f, 0.1f, 0.05f, 0.75f}; PdfPTable table = new PdfPTable(widths); table.addCell("10%"); table.addCell("10%"); table.addCell("5%"); table.addCell("75%"); table.addCell("111111111111111111111111111"); table.addCell("111111111111111"); table.addCell("11111"); table.addCell("11"); document.add(table); } catch (Exception de) { de.printStackTrace(); } document.close(); }
}
</source>
Specific Cell Position
<source lang="java">
import java.awt.Point; 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.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfWriter; public class SpecificCellPositionPDF {
public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("SpecificCellPositionPDF.pdf")); document.open(); Table aTable; aTable = new Table(4, 4); // 4 rows, 4 columns aTable.setAlignment(Element.ALIGN_RIGHT); aTable.addCell("2.2", new Point(2, 2)); aTable.addCell("3.3", new Point(3, 3)); aTable.addCell("2.1", new Point(2, 1)); aTable.addCell("1.3", new Point(1, 3)); document.add(aTable); aTable.setConvert2pdfptable(true); document.add(aTable); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
}
</source>
Specific Cell with Different Width
<source lang="java">
import java.awt.Point; 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.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfWriter; public class SpecificCellWithDifferentWidthPDF {
public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("SpecificCellWithDifferentWidthPDF.pdf")); document.open(); Table aTable; aTable = new Table(4, 4); // 4 rows, 4 columns aTable.setWidths(new float[] {2f, 1f, 1f, 1f}); aTable.setAlignment(Element.ALIGN_RIGHT); aTable.addCell("2.2", new Point(2, 2)); aTable.addCell("3.3", new Point(3, 3)); aTable.addCell("2.1", new Point(2, 1)); aTable.addCell("1.3", new Point(1, 3)); document.add(aTable); aTable.setConvert2pdfptable(true); document.add(aTable); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
}
</source>
Table Cell Fitting Page
<source lang="java">
import java.awt.Color; import java.io.FileOutputStream; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfWriter; public class TableCellFitPagePDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); try { PdfWriter.getInstance(document, new FileOutputStream("TableCellFitPagePDF.pdf")); document.open(); Table datatable = new Table(3); int headerwidths[] = {10, 24, 12}; datatable.setWidths(headerwidths); datatable.setWidth(46); datatable.setPadding(3); Cell cell = new Cell(new Phrase("title", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setLeading(30); cell.setColspan(3); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); datatable.addCell(cell); datatable.setDefaultCellBorderWidth(2); datatable.setDefaultHorizontalAlignment(1); datatable.addCell("Header 1"); datatable.addCell("Header \n 2"); datatable.addCell("Header 3"); datatable.endHeaders(); datatable.setDefaultCellBorderWidth(1); for(int i=0;i<200;i++){ datatable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT); datatable.addCell("1"); datatable.addCell("2"); datatable.addCell("3"); } datatable.setCellsFitPage(true); document.add(datatable); } catch (Exception e) { e.printStackTrace(); } document.close(); }
}
</source>
Table Cell Height
<source lang="java">
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 TableCellHeightPDF {
public static void main(String[] args) { Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableCellHeightPDF.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(); }
}
</source>