Java/PDF RTF/Table — различия между версиями

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

Текущая версия на 05:59, 1 июня 2010

Convert2pdfptable

import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
public class Convert2pdfptable {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("Convert2pdfptable.pdf"));
      document.open();
      Table table = new Table(2);
      table.setBorderWidth(1);
      table.setPadding(10);
      Cell cell = new Cell("header");
      cell.setHeader(true);
      cell.setColspan(2);
      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");
      table.setConvert2pdfptable(true);
      document.add(table);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Extending PdfPTable LastRow

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 ExtendingLastRowPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream("ExtendingLastRowPDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      table.setExtendLastRow(true);
      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();
  }
}





PdfPTable: WriteSelectedRows

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 WriteSelectedRowsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 2, 2, 2, 2);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "WriteSelectedRowsPDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(4);
      table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT);
      
      table.addCell("a");
      table.addCell("a");
      table.addCell("a");
      table.addCell("a");
      
      table.setTotalWidth(300f);
      table.writeSelectedRows(0, -1, 100, 600, writer.getDirectContent());
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Table Column, Row Count

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 TableColumnRowCountPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document,  new FileOutputStream("TableColumnRowCountPDF.pdf"));
      document.open();
      Table table = new Table(2, 2); // 2 rows, 2 columns
      table.addCell("0.0");
      table.addCell("0.1");
      table.addCell("1.0");
      table.addCell("1.1");
      document.add(table);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Table Width Percentage

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
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 TableWidthPercentagePDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream("TableWidthPercentagePDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(3);
      PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
      cell.setColspan(3);
      table.addCell(cell);
      document.add(table);
      
      table.setWidthPercentage(50);
      document.add(table);
      
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}