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

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

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

Cell Alignment Baseline

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 CellAlignmentBaselinePDF {
  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("CellAlignmentBaselinePDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell;
      table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BASELINE);
      Paragraph p = new Paragraph("Text Text\nText Text Text\nText\nText\nText\nText\nText\nText\nText Text Text\nText Text\nText ");
      table.addCell("default alignment");
      cell = new PdfPCell(p);
      
      table.addCell(cell);
      document.add(table);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Cell Alignment Center

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 CellAlignmentCenterPDF {
  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("CellAlignmentCenterPDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell;
      Paragraph p = new Paragraph("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ");
      table.addCell("default alignment");
      cell = new PdfPCell(p);
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell(cell);
      table.addCell("right alignment");
      document.add(table);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Cell Alignment Justified

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 CellAlignmentJustifiedPDF {
  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("CellAlignmentJustifiedPDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell;
      Paragraph p = new Paragraph("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ");
      table.addCell("default alignment");
      cell = new PdfPCell(p);
      cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
      table.addCell(cell);
      document.add(table);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Cell Alignment Middle

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 CellAlignmentMiddlePDF {
  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("CellAlignmentMiddlePDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell;
      table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
      Paragraph p = new Paragraph("Text Text\nText Text Text\nText\nText\nText\nText\nText\nText\nText Text Text\nText Text\nText ");
      table.addCell("default alignment");
      cell = new PdfPCell(p);
      
      table.addCell(cell);
      document.add(table);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Table Cell Alignment: Center, Middle

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 TableCellAlignmentCenterMiddlePDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableCellAlignmentCenterMiddlePDF.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();
  }
}





Table Cell Alignment Demo

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TableCellAlignmentPDF {
  public static void main(String[] args) {
    Document.rupress = false;
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("TableCellAlignmentPDF.pdf"));
      document.open();
      Image img = Image.getInstance("logo.png");
      img.scalePercent(10);
      
      PdfPTable table = new PdfPTable(3);
      PdfPCell cell = new PdfPCell();
      cell.addElement(new Chunk("cell "));
      cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell("a cell");
      table.addCell(cell);
      table.addCell("a cell");
      document.add(table);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Table Cell Default Alignment

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 TableCellDefaultAlignmentPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream("TableCellDefaultAlignmentPDF.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);
      
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}