Java Tutorial/PDF/Section

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

Add Paragraph to section

   <source lang="java">

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();
 }

}</source>





Add section to chapter

   <source lang="java">

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();
 }

}</source>





Add text to section

   <source lang="java">

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();
 }

}</source>





Create section from Chapter

   <source lang="java">

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();
 }

}</source>





Set section Indentation Left

   <source lang="java">

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();
 }

}</source>