Java/PDF RTF/Table Nested

Материал из Java эксперт
Перейти к: навигация, поиск

Nested Table Cell

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class NestedTableCellPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NestedTableCellPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfPTable pageTot = new PdfPTable(1);
      pageTot.getDefaultCell().setPadding(0f);
      pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER);
      pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
      pageTot.setWidthPercentage(100f);
      PdfPTable cell = new PdfPTable(1);
      cell.getDefaultCell().setBorder(Rectangle.NO_BORDER);
      cell.getDefaultCell().setPadding(0f);
      PdfPCell info = new PdfPCell(new Phrase("Outside"));
      info.setBorder(Rectangle.NO_BORDER);
      pageTot.addCell(info);
      PdfPCell shipment = new PdfPCell(new Phrase(new Chunk("Cell")));
      shipment.setFixedHeight(100);
      shipment.setPaddingTop(5f);
      shipment.setPaddingBottom(10f);
      shipment.setBorder(Rectangle.BOX);
      shipment.setVerticalAlignment(Element.ALIGN_TOP);
      shipment.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.addCell(shipment);
      pageTot.addCell(cell);
      document.add(pageTot);
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}





Nested Tables Demo

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 NestedTablesPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NestedTablesPDF.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("2.1");
      nested2.addCell("2.2");
      table.addCell(nested1);
      table.addCell("cell ");
      table.addCell("cell ");
      table.addCell("cell ");
      table.addCell("cell ");
      table.addCell("cell ");
      
      table.addCell(nested2);
      table.addCell("cell ");
      table.addCell("cell ");
      table.addCell("cell ");
      table.addCell("cell ");
      document.add(table);
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Nested Tables without Point Position

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
public class NestedTablesWithoutPointPositionPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("NestedTablesWithoutPointPositionPDF.pdf"));
      document.open();
      Table aTable = new Table(2, 2); // 4 rows, 4 columns
      aTable.setAutoFillEmptyCells(true);
      aTable.addCell("2.2");
      aTable.addCell("2.1");
      Table secondTable = new Table(2);
      secondTable.addCell("0.0");
      secondTable.addCell("0.1");
      aTable.insertTable(secondTable);
      aTable.addCell("2.2");
      
      document.add(aTable);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Two Nested Tables

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.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
public class TwoNestedTablesPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("TwoNestedTablesPDF.pdf"));
      document.open();
      Table secondTable = new Table(2);
      secondTable.addCell("0.0");
      secondTable.addCell("0.1");
      secondTable.addCell("1.0");
      secondTable.addCell("1.1");
      Table aTable = new Table(4, 4); // 4 rows, 4 columns
      aTable.setAutoFillEmptyCells(true);
      aTable.addCell("2.2", new Point(2, 2));
      aTable.insertTable(secondTable, new Point(3, 3));
      aTable.addCell("2.1", new Point(2, 1));
      aTable.insertTable(secondTable, new Point(1, 3));
      document.add(aTable);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}