Java Tutorial/PDF/Section

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

Add Paragraph to section

import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
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("1.pdf"));
    document.open();
    document.add(new Paragraph("this"));
    document.newPage();
    Paragraph hello = new Paragraph("paragraph");
    Chapter universe = new Chapter("To the Universe:", 1);
    Section section;
    section = universe.addSection("World:");
    section.add(hello);
    section = universe.addSection("Sun:");
    section.add(hello);
    section = universe.addSection("Moon:");
    section.add(hello);
    section = universe.addSection("Stars:");
    section.add(hello);
    document.add(universe);
    document.close();
  }
}





Add section to chapter

import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
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("1.pdf"));
    document.open();
    document.add(new Paragraph("this"));
    document.newPage();
    Paragraph hello = new Paragraph("paragraph");
    Chapter universe = new Chapter("To the Universe:", 1);
    Section section;
    section = universe.addSection("World:");
    section.add(hello);
    section = universe.addSection("Sun:");
    section.add(hello);
    section = universe.addSection("Moon:");
    section.add(hello);
    section = universe.addSection("Stars:");
    section.add(hello);
    document.add(universe);
    document.close();
  }
}





Add text to section

import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Phrase text = new Phrase("this is a test. ");
    Font font = new Font(Font.HELVETICA, 14, Font.BOLD);
    Chapter chapter1 = new Chapter(new Paragraph("This is the title.", font), 1);
    chapter1.add(text);
    Section section1 = chapter1.addSection("Quick", 0);
    section1.add(text);
    section1.add(text);
    section1.add(text);
    document.add(chapter1);
    document.close();
  }
}





Create section from Chapter

import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Phrase text = new Phrase("this is a test. ");
    Font font = new Font(Font.HELVETICA, 14, Font.BOLD);
    Chapter chapter1 = new Chapter(new Paragraph("This is the title.", font), 1);
    chapter1.add(text);
    Section section1 = chapter1.addSection("Quick", 0);
    section1.add(text);
    section1.add(text);
    section1.add(text);
    document.add(chapter1);
    document.close();
  }
}





Set section Indentation Left

import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Phrase text = new Phrase("this is a test. ");
    Font font = new Font(Font.HELVETICA, 14, Font.BOLD);
    Chapter chapter1 = new Chapter(new Paragraph("This is the title.", font), 1);
    chapter1.add(text);
    Section section1 = chapter1.addSection("Quick", 0);
    section1.add(text);
    section1.add(text);
    section1.add(text);
    Section subsection1 = section1.addSection("Lazy", 2);
    subsection1.setIndentationLeft(30);
    subsection1.add(text);
    subsection1.add(text);
    subsection1.add(text);
    subsection1.add(text);
    subsection1.add(text);
    document.add(chapter1);
    document.close();
  }
}