Package projetV1

Source Code of projetV1.ExportPdf

package projetV1;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.util.ArrayList;;

public class ExportPdf {
  ArrayList<ArrayList> a;
  int i;

  private static Font catFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
  private static Font redFont = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.RED);
  private static Font subFont = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
  private static Font smallBold = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);

  public void export(ArrayList<ArrayList> a, String path) {
    this.a=a;
    try {
      File dd = new File(path);
      dd.createNewFile();
     
      FileOutputStream f=new FileOutputStream(dd);
      Document document = new Document();
      PdfWriter.getInstance(document, f);
      document.open();
      addTitlePage(document);
      addContent(document);
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void addTitlePage(Document document)
      throws DocumentException {
    Paragraph preface = new Paragraph();
    addEmptyLine(preface, 1);
    preface.add(new Paragraph("WikiDico V 1.0", catFont));
    addEmptyLine(preface, 1);
    preface.add(new Paragraph("Le dictionnaire libre", smallBold));
    addEmptyLine(preface, 3);
    addEmptyLine(preface, 8);
    document.add(preface);
    document.newPage();
  }

  private void addContent(Document document) throws DocumentException {
    Chapter catPart=null;
    int k=1;
    for(int j=0; j<a.size(); j++){
      if (!a.get(j).equals(new ArrayList<String>())){
    Anchor anchor = new Anchor(a.get(j).get(0).toString(), catFont);
    anchor.setName(a.get(j).get(0).toString());
    catPart = new Chapter(new Paragraph(anchor), k);

    Paragraph subPara = new Paragraph("Prononciation: ", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph(a.get(j).get(1).toString()));

    subPara = new Paragraph("Nature: ", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph(a.get(j).get(2).toString()));
 
    subPara = new Paragraph("D�finition: ", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph(a.get(j).get(3).toString()));
    document.add(catPart);

      k++;
      }
    }

    document.add(catPart);

  }

  private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
    }
  }

}
TOP

Related Classes of projetV1.ExportPdf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.