Java/PDF RTF/Paragraph

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

Adding Chunk and Phrase to Paragraph

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.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class ParagraphsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ParagraphsPDF.pdf"));
      document.open();
      Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. ", FontFactory.getFont(FontFactory.HELVETICA, 10)));
      p1.add("The default leading is 1.5 times the fontsize. ");
      p1.add(new Chunk("new chunks "));
      p1.add(new Phrase("new phrases. "));
      document.add(p1);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Adding Chunk to Paragraph

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class AddingChunkToParagraphPDF {
  public static void main(String[] args) {
    try {
      Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddingChunkToParagraphPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph();
      p.add(new Chunk("Font.TIMES_ROMAN", new Font(Font.TIMES_ROMAN, 12)));
      document.add(new Paragraph(p));
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Adding List to Paragraph

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
public class PDFListsAtoE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ListsAtoE.pdf"));
      document.open();
      Paragraph paragraph = new Paragraph("A to E:");
      List list = new List(false, 10);
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      list.add("E");
      paragraph.add(list);
      document.add(paragraph);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Adding Paragraph to a Page

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.PdfWriter;
public class AddingParagraphToAPagePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("AddingParagraphToAPagePDF.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A3);
      document.newPage();
      document.add(new Paragraph("This PageSize is A3."));
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Generates a simple "Hello World" PDF file

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.pdf.PdfWriter;
public class HelloWorldPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("HelloWorldPDF.pdf"));
      document.open();
      document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Long Paragraph in PDF

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class LongParagraphPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("LongParagraphPDF.pdf"));
      document.open();
      Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. " +
          "This is my first paragraph. This is my first paragraph. This is my " +
          "first paragraph. This is my first paragraph. This is my first paragraph. " +
          "This is my first paragraph. "));
      document.add(p1);
      
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Three Paragraphs

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.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class ThreeParagraphsPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ThreeParagraphsPDF.pdf"));
      document.open();
      Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. ", FontFactory.getFont(FontFactory.HELVETICA, 10)));
      document.add(p1);
      
      Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
      p2.add("a new line.");
      document.add(p2);
      Paragraph p3 = new Paragraph("This is my third paragraph.",FontFactory.getFont(FontFactory.HELVETICA, 12));
      document.add(p3);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}