Java Tutorial/PDF/Table Row

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Loop through all rows from Table

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);
    
    PdfPRow row;
    for (Iterator i = table.getRows().iterator(); i.hasNext();) {
      row = (PdfPRow) i.next();
      System.out.print(" ");
      System.out.print(row.getMaxHeights());
    }
    
    document.close();
  }
}





Ouput selected rows from a table

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);
    
    System.out.println("Total table height: " + table.getTotalHeight());
    
    document.close();
  }
}





Set table 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 0 - 50"));
    table.writeSelectedRows(0, 50, 150, 820, cb);
   document.close();
  }
}





Split Table Rows

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.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 document1 = new Document(PageSize.A4.rotate());
      PdfWriter.getInstance(document1, new FileOutputStream(
          "2.pdf"));
      document1.open();
      String text = "Quick brown fox jumps over the lazy dog.";
      PdfPTable table = new PdfPTable(2);
      PdfPCell largeCell = new PdfPCell();
      for (int i = 1; i < 13; i++) {
        largeCell.addElement(new Paragraph(String.valueOf(i) + text));
      }
      for (int i = 1; i < 11; i++) {
         table.addCell(largeCell);
      }
      document1.add(table);
      table.setSplitLate(false);
      document1.add(table);
      table.setSplitRows(false);
      document1.add(table);
    document1.close();
  }
}





What if table is larger than the page

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 100 - 150 "));
    table.writeSelectedRows(100, 150, 150, 200, cb);
    document.close();
  }
}





Write Selected Rows in a Table

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 50 - 100"));
    table.writeSelectedRows(50, 100, 150, 820, cb);
    document.close();
  }
}