Java Tutorial/PDF/Column

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

Add SimpleColumn to MultiColumnText

import java.io.FileOutputStream;
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.pdf.BaseFont;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    MultiColumnText mct = new MultiColumnText();
    mct.addSimpleColumn(100, 280);
    mct.addSimpleColumn(300, 480);
    for (int i = 0; i < 30; i++) {
      mct.addElement(new Paragraph(String.valueOf(i + 1)));
      mct.addElement(newParagraph("noun", Element.ALIGN_CENTER, Font.BOLDITALIC));
      for (int j = 0; j < 4; j++) {
        mct.addElement(newParagraph("Line", Element.ALIGN_LEFT, Font.NORMAL));
      }
      mct.addElement(newParagraph("adverb", Element.ALIGN_LEFT, Font.NORMAL));
      mct.addElement(newParagraph("\n\n", Element.ALIGN_LEFT, Font.NORMAL));
    }
    document.add(mct);
    document.close();
    document.close();
  }
  private static Element newParagraph(String text, int alignment, int type) {
    Font font = FontFactory.getFont(BaseFont.HELVETICA, 10, type);
    Paragraph p = new Paragraph(text, font);
    p.setAlignment(alignment);
    p.setLeading(font.size() * 1.2f);
    return p;
  }
}





Column Properties

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    ct.setAlignment(Element.ALIGN_JUSTIFIED);
    ct.setExtraParagraphSpace(12);
    ct.setFollowingIndent(18);
    ct.setLeading(0, 1.2f);
    ct.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
    ct.setUseAscender(true);
    Phrase p;
    ct.setSimpleColumn(36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36);
    for (int i = 0; i < 10; i++) {
      p = new Phrase(i + "\n");
      ct.addText(p);
    }
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
      status = ct.go();
      ct.setYLine(PageSize.A4.height() - 36);
      document.newPage();
    }
    document.close();
  }
}





Columns Right To Left

import java.io.FileOutputStream;
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.pdf.BaseFont;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    MultiColumnText mct = new MultiColumnText();
    mct.setColumnsRightToLeft(true);
    mct.addRegularColumns(document.left(), document.right(), 10f, 3);
    for (int i = 0; i < 30; i++) {
      mct.addElement(new Paragraph(String.valueOf(i + 1)));
      mct.addElement(newParagraph("noun", Element.ALIGN_CENTER, Font.BOLDITALIC));
      for (int j = 0; j < 4; j++) {
        mct.addElement(newParagraph("Line", Element.ALIGN_LEFT, Font.NORMAL));
      }
      mct.addElement(newParagraph("adverb", Element.ALIGN_LEFT, Font.NORMAL));
      mct.addElement(newParagraph("\n\n", Element.ALIGN_LEFT, Font.NORMAL));
    }
    document.add(mct);
    document.close();
    document.close();
  }
  private static Element newParagraph(String text, int alignment, int type) {
    Font font = FontFactory.getFont(BaseFont.HELVETICA, 10, type);
    Paragraph p = new Paragraph(text, font);
    p.setAlignment(alignment);
    p.setLeading(font.size() * 1.2f);
    return p;
  }
}





ColumnText.hasMoreText

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    ct.setAlignment(Element.ALIGN_JUSTIFIED);
    ct.setText(new Phrase("string"));
    float[] left = { 36, (PageSize.A4.width() / 2) + 18 };
    float[] right = { (PageSize.A4.width() / 2) - 18, PageSize.A4.width() - 36 };
    int status = ColumnText.START_COLUMN;
    int column = 0;
    while (ColumnText.hasMoreText(status)) {
      ct.setSimpleColumn(left[column], 36, right[column], PageSize.A4.height() - 36);
      status = ct.go();
      column++;
      if (column > 1) {
        column = 0;
        document.newPage();
      }
    }
    document.close();
  }
}





ColumnText.NO_MORE_COLUMN

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36, 18,
        Element.ALIGN_JUSTIFIED);
    ct.setText(new Phrase("String"));
    int status = ColumnText.NO_MORE_COLUMN;
    while ((status & ColumnText.NO_MORE_TEXT) == 0) {
      status = ct.go();
      ct.setYLine(PageSize.A4.height() - 36);
      document.newPage();
    }
    document.close();
  }
}





Create Column With SimpleColumn

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    StringBuffer sb = new StringBuffer(1024);
    BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
    int c;
    while ((c = reader.read()) > -1) {
      sb.append((char) c);
    }
    reader.close();
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(new Phrase(sb.toString()), 36, 36, PageSize.A4.width() - 36, PageSize.A4
        .height() - 36, 18, Element.ALIGN_JUSTIFIED);
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
      status = ct.go();
      ct.setYLine(PageSize.A4.height() - 36);
      document.newPage();
    }
    document.close();
  }
}





Regular Columns

import java.io.FileOutputStream;
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.pdf.BaseFont;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    MultiColumnText mct = new MultiColumnText();
    mct.addRegularColumns(document.left(), document.right(), 10f, 3);
    for (int i = 0; i < 30; i++) {
      mct.addElement(new Paragraph(String.valueOf(i + 1)));
      mct.addElement(newParagraph("noun", Element.ALIGN_CENTER, Font.BOLDITALIC));
      for (int j = 0; j < 4; j++) {
        mct.addElement(newParagraph("Line", Element.ALIGN_LEFT, Font.NORMAL));
      }
      mct.addElement(newParagraph("adverb", Element.ALIGN_LEFT, Font.NORMAL));
      mct.addElement(newParagraph("\n\n", Element.ALIGN_LEFT, Font.NORMAL));
    }
    document.add(mct);
    document.close();
    document.close();
  }
  private static Element newParagraph(String text, int alignment, int type) {
    Font font = FontFactory.getFont(BaseFont.HELVETICA, 10, type);
    Paragraph p = new Paragraph(text, font);
    p.setAlignment(alignment);
    p.setLeading(font.size() * 1.2f);
    return p;
  }
}





Set Column text

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    int status = ColumnText.START_COLUMN;
    ct.setSimpleColumn(36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36, 18,
        Element.ALIGN_JUSTIFIED);
    Phrase p = new Phrase("line");
    ct.addText(p);
    float pos = ct.getYLine();
    status = ct.go(true);
    if (!ColumnText.hasMoreText(status)) {
      ct.addText(p);
      ct.setYLine(pos);
      ct.go(false);
    } else {
      document.newPage();
      ct.setText(p);
      ct.setYLine(PageSize.A4.height() - 36);
      ct.go();
    }
    document.close();
  }
}





Using ColumnText

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
    String line;
    while ((line = reader.readLine()) != null) {
      ct.addText(new Phrase(line + "\n"));
    }
    reader.close();
    ct.setSimpleColumn(36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36, 18,
        Element.ALIGN_JUSTIFIED);
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
      status = ct.go();
      ct.setYLine(PageSize.A4.height() - 36);
      document.newPage();
    }
    document.close();
  }
}