Java/PDF RTF/Phrase

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

Adding Chunk to a Phrase

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class PhraseInaChunkPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("PhraseInaChunkPDF.pdf"));
      document.open();
      Phrase phrase = new Phrase(new Chunk("this is a phrase in a chunk\n"));
      document.add(phrase);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Phrase with Color red and Normal Font

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class PhraseWithColorRedAndNormalFontPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("PhraseWithColorRedAndNormalFontPDF.pdf"));
      document.open();
      Phrase phrase = new Phrase("(3) this is a phrase with a red, normal font Courier, size 20.\n",
                                 FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL,
                                 new Color(255, 0, 0)));
      document.add(phrase);
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Phrase with Leading Line

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class PhraseWithLeadingLinePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("PhraseWithLeadingLinePDF.pdf"));
      document.open();
      Phrase phrase0 = new Phrase();
      Phrase phrase1 = new Phrase("this is a phrase\n");
      Phrase phrase2 = new Phrase(24,"this is a phrase with leading 24. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text \n");
      document.add(phrase0);
      document.add(phrase1);
      document.add(phrase2);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Phrase with Several Chunks and Different Fonts

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class PhraseWithSeveralChunksAndDifferentFontsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("PhraseWithSeveralChunksAndDifferentFontsPDF.pdf"));
      document.open();
      Phrase phrase6 = new Phrase("I have several Chunks.");
      Chunk chunk = new Chunk("Font: ");
      phrase6.add(chunk);
      phrase6.add(new Chunk("Helvetica", FontFactory.getFont(FontFactory.HELVETICA, 12)));
      phrase6.add(chunk);
      phrase6.add(new Chunk("Times New Roman", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12)));
      phrase6.add(chunk);
      phrase6.add(new Chunk("Courier", FontFactory.getFont(FontFactory.COURIER, 12)));
      phrase6.add(chunk);
      phrase6.add(new Chunk("Symbol", FontFactory.getFont(FontFactory.SYMBOL, 12)));
      phrase6.add(chunk);
      document.add(Chunk.NEWLINE);
      phrase6.add(new Chunk("ZapfDingBats", FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12)));
      document.add(phrase6);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}