Java PDF: Configurer l’affichage des Signets

Author:

Java PDF:  Configurer l'affichage des Signets
{filelink=10347}

/***** Code de MesExemples.com *******/
/**
 * @(#)ExempleChapitre.java
 *
 *
 * @author sakoba(mesexemples.com)
 *sakoba(java.mesexemples.com) @version 1.00 2013/4/19
 */



import java.awt.Color;
import java.io.FileOutputStream;

import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;

public class ExempleChapitrePDF
 {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter pdf_writer = PdfWriter.getInstance(document,
       new FileOutputStream("c:/ChapitrePDF.pdf"));
       // Définir la version du PDF
       pdf_writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
       // Définir le type d'affichage
      pdf_writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      // Définir le style de la section des chapitres
      Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24);
      Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA);
      Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA);
      
      // Créer deux paragraphee
      Paragraph paragraphe1 = new Paragraph("texte 1");
      Paragraph paragraphe2 = new Paragraph("texte 2");
      
      Paragraph cTitle = new Paragraph("Ceci est un chapitre ", chapterFont);
      Chapter chapitre = new Chapter(cTitle, 1);

      paragraphe2.setAlignment(Element.ALIGN_CENTER);
      paragraphe1.setAlignment(Element.ALIGN_RIGHT);
      chapitre.add(paragraphe1);
      
      // Ajouter une section dans le paragraphe 1
      Paragraph sTitle = new Paragraph("Première section du paragraphe 1" , 
    sectionFont);
      Section section = chapitre.addSection(sTitle, 1);
      section.add(paragraphe1);
      section.setBookmarkOpen(false); // Modifier l'ouverture
      
      // Créer une sous section
      Paragraph subTitle = new Paragraph("sous-section de section1", 
    subsectionFont);
      Section subsection = section.addSection(subTitle, 3);
      
      document.add(chapitre);
    } catch (Exception err) {
      err.printStackTrace();
    }
    document.close();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *