Java Tutorial/PDF/Chapter

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

Create Chapter and add chapter 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 CutPDFFileIntoPages {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "HelloWorldRead.pdf"));
    document.open();
    Paragraph hello = new Paragraph("hello, ");
    Chapter universe = new Chapter("A", 1);
    Section section;
    section = universe.addSection("B");
    section.add(hello);
    document.add(universe);
    Chapter people = new Chapter("C", 2);
    section = people.addSection("D");
    section.add(hello);
    document.add(people);
    document.setPageSize(PageSize.A4.rotate());
    Chapter animals = new Chapter("E", 3);
    section = animals.addSection("F");
    section.add(hello);
    document.add(animals);
    document.close();
  }
}





Use ChapterAutoNumber

import java.io.FileOutputStream;
import com.lowagie.text.ChapterAutoNumber;
import com.lowagie.text.Document;
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 writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    document.open();
    Phrase text = new Phrase("test. ");
    ChapterAutoNumber chapter1 = new ChapterAutoNumber("This is a sample sentence:");
    chapter1.setBookmarkTitle("The fox");
    chapter1.setBookmarkOpen(false);
    Section section1 = chapter1.addSection("Quick");
    section1.add(text);
    document.add(chapter1);
    ChapterAutoNumber chapter2 = new ChapterAutoNumber("Jumps");
    Section section = chapter2.addSection("Over");
    section.add(text);
    document.add(chapter2);
    document.close();
  }
}