Example usage for com.itextpdf.text Chapter Chapter

List of usage examples for com.itextpdf.text Chapter Chapter

Introduction

In this page you can find the example usage for com.itextpdf.text Chapter Chapter.

Prototype

public Chapter(String title, int number) 

Source Link

Document

Constructs a new Chapter.

Usage

From source file:com.leenmeij.app.utils.CreatePdf.java

/**
 * Create the content page of the PDF file
 * @param document/*from w w  w.  j  a va 2 s .  co  m*/
 * @param invoice
 * @throws DocumentException
 */
private static void contentPage(Document document, Invoice invoice) throws DocumentException {
    // Get the customer information
    User user = new User();
    user = user.getById(invoice.getUser_id());

    // Create a chapter
    Chapter catPart = new Chapter(new Paragraph("LeenMeij Factuur gegevens", catFont), 1);

    // Add a sub paragraph
    Paragraph subParagraph = new Paragraph("Klantgegevens", subFont);
    Section subCatPart = catPart.addSection(subParagraph);

    // Set the user information
    subCatPart.add(new Paragraph("Naam: " + user.getFirstName() + " " + user.getLastName()));
    subCatPart.add(new Paragraph("Adres: " + user.getAddressLineOne() + " " + user.getAddressLineTwo()));
    subCatPart.add(new Paragraph("Woonplaats:" + user.getCity()));
    subCatPart.add(new Paragraph("Land: " + user.getCountry()));

    // Add another subparagraph
    subParagraph = new Paragraph("Huurgegevens:", subFont);
    subCatPart = catPart.addSection(subParagraph);
    // Format the date for showing purposes
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/YYYY");
    // Add the dates
    subCatPart.add(new Paragraph("Startdatum: " + format.format(invoice.getStartdate())));
    subCatPart.add(new Paragraph("Einddatum: " + format.format(invoice.getEnddate())));

    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // add a table
    createTable(subCatPart, invoice);

    // now add all this to the document
    document.add(catPart);
}

From source file:com.pdg.ticket.utils.FirstPdf.java

private static void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("First Chapter", catFont);
    anchor.setName("First Chapter");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategory 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragraph 1"));
    subCatPart.add(new Paragraph("Paragraph 2"));
    subCatPart.add(new Paragraph("Paragraph 3"));

    // Add a list
    //createList(subCatPart);
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);//from  w  w  w . j av  a2s  .co m
    subCatPart.add(paragraph);

    // Add a table
    createTable(subCatPart);

    // Now add all this to the document
    document.add(catPart);

    //      // Next section
    //      anchor = new Anchor("Second Chapter", catFont);
    //      anchor.setName("Second Chapter");
    //
    //      // Second parameter is the number of the chapter
    //      catPart = new Chapter(new Paragraph(anchor), 1);
    //
    //      subPara = new Paragraph("Subcategory", subFont);
    //      subCatPart = catPart.addSection(subPara);
    //      subCatPart.add(new Paragraph("This is a very important message"));
    //
    //      // Now add all this to the document
    //      document.add(catPart);

}

From source file:com.shashi.itext.write.FirstPdf.java

private static void addContent(Document document) throws DocumentException {

    Anchor anchor = new Anchor("First Chapter", catFont);
    anchor.setName("First Chapter");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategory 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragraph 1"));
    subCatPart.add(new Paragraph("Paragraph 2"));
    subCatPart.add(new Paragraph("Paragraph 3"));

    // Add a list
    createList(subCatPart);/*from   ww  w .  java2  s.c o  m*/
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // Add a table
    createTable(subCatPart);

    // Now add all this to the document
    document.add(catPart);

    // Next section
    anchor = new Anchor("Second Chapter", catFont);
    anchor.setName("Second Chapter");

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategory", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    // Now add all this to the document
    document.add(catPart);

}

From source file:com.util.Imprimir.java

public static void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("Capitulo Primeiro", catFont);
    anchor.setName("Capitulo Primeiro");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategoria 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategoria 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragrafo 1"));
    subCatPart.add(new Paragraph("Paragrafo 2"));
    subCatPart.add(new Paragraph("Paragrafo 3"));

    // add a list
    createList(subCatPart);//ww  w.j a v a  2  s  . c o  m
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // add a table
    createTable(subCatPart);

    // now add all this to the document
    document.add(catPart);

    // Next section
    anchor = new Anchor("Capitulo Segundo", catFont);
    anchor.setName("Capitulo Segundo");

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategoria", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    // now add all this to the document
    document.add(catPart);

}

From source file:com.vectorprint.report.itext.DefaultElementProducer.java

License:Open Source License

/**
 * create the Section, style the title, style the section and return the styled section.
 *
 * @param title/*from  ww  w  .  ja  va 2  s  .  co  m*/
 * @param nesting
 * @param stylers
 * @return
 * @throws VectorPrintException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
@Override
public Section getIndex(String title, int nesting, List<? extends BaseStyler> stylers)
        throws VectorPrintException, InstantiationException, IllegalAccessException {
    if (nesting < 1) {
        throw new VectorPrintException("chapter numbering starts with 1, wrong number: " + nesting);
    }
    if (sections.get(nesting) == null) {
        sections.put(nesting, new ArrayList<>(10));
    }
    Section current;
    if (nesting == 1) {
        List<Section> chapters = sections.get(1);
        current = new Chapter(createElement(title, Paragraph.class, stylers), chapters.size() + 1);
        chapters.add(current);
    } else {
        List<Section> parents = sections.get(nesting - 1);
        Section parent = parents.get(parents.size() - 1);
        current = parent.addSection(createParagraph(title, stylers));
        sections.get(nesting).add(current);
    }
    return styleHelper.style(current, null, stylers);
}

From source file:com.workhub.utils.PDFUtils.java

private static void addContent(Document document, List<ElementModel> models) throws DocumentException {
    for (int i = 0; i < models.size(); i++) {
        ElementModel elementModel = models.get(i);
        Anchor anchor = new Anchor(elementModel.getTitle(), catFont);
        anchor.setName(elementModel.getTitle());
        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), i + 1);

        Paragraph content = new Paragraph();
        catPart.add(content);//from ww  w  .j a  v  a2s  .  c  o m
        addEmptyLine(content, 2);

        switch (elementModel.getType()) {
        case Constants.TYPE_ELEMENT_TEXT:
            content.add(new Paragraph(((TextElementModel) elementModel).getContent(), smallBold));

            break;
        case Constants.TYPE_ELEMENT_LINK:
            content.add(new Paragraph(((LinkElementModel) elementModel).getContent(), smallBold));

            break;
        case Constants.TYPE_ELEMENT_PICTURE:
            Image image1;
            try {
                if (((PictureElementModel) elementModel).getContent() != null) {
                    image1 = Image.getInstance(((PictureElementModel) elementModel).getContent());

                    content.add(image1);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case Constants.TYPE_ELEMENT_FILE:
            content.add(new Paragraph(((FileElementModel) elementModel).getTitle(), smallBold));
            break;

        }

        document.add(catPart);
    }

}

From source file:com.zaptech.pdfdemo.MainActivity.java

private void addContent(Document document) {
    try {//from w  w  w .j a v  a 2  s.  c o  m
        Anchor anchor = new Anchor("First Chapter", catFont);
        anchor.setName("First Chapter");
        Chapter chapter = new Chapter(new Paragraph(anchor), 1);
        Paragraph paragraph = new Paragraph("Subcategory 1", subFont);
        Section section = chapter.addSection(paragraph);
        section.add(new Paragraph("Hello"));
        paragraph = new Paragraph("Subcategory 2", subFont);
        section = chapter.addSection(paragraph);
        section.add(new Paragraph("Paragraph 1"));
        section.add(new Paragraph("Paragraph 2"));
        section.add(new Paragraph("Paragraph 3"));
        createList(section);
        Paragraph paragraph1 = new Paragraph();
        addEmptyLine(paragraph1, 5);
        section.add(paragraph1);
        document.add(chapter);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Controller.CrearPDF.java

/**
 * We create a PDF document with iText using different elements to learn 
 * to use this library.// w  w w .ja v  a2  s  . c  om
 * Creamos un documento PDF con iText usando diferentes elementos para aprender 
 * a usar esta librera.
 * @param pdfNewFile  <code>String</code> 
 *      pdf File we are going to write. 
 *      Fichero pdf en el que vamos a escribir. 
 */
//    public void createPDF(File pdfNewFile, ReparacionEntity reparacion) throws Exception {
public void createPDF(File pdfNewFile) throws Exception {
    // We create the document and set the file name.        
    // Creamos el documento e indicamos el nombre del fichero.
    try {
        //            ClienteController cc = new ClienteController();
        //            Cliente cliente = cc.buscarPorId(reparacion.getCliente());
        Document document = new Document();
        try {

            PdfWriter.getInstance(document, new FileOutputStream(pdfNewFile));

        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println("No such file was found to generate the PDF "
                    + "(No se encontr el fichero para generar el pdf)" + fileNotFoundException);
        }
        document.open();
        // We add metadata to PDF
        // Aadimos los metadatos del PDF
        document.addTitle("Table export to PDF (Exportamos la tabla a PDF)");
        document.addSubject("Using iText (usando iText)");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Cdigo Xules");
        document.addCreator("Cdigo Xules");

        // First page
        // Primera pgina 
        Chunk chunk = new Chunk("RDEN DE TRABAJO", categoryFont);
        Chunk c2 = new Chunk("\n\n\nCentro de Formacin SATCAR6\n" + "Calle Artes Grficas n1 Nave 12 A\n"
                + "Pinto (28320), Madrid", smallFont);

        Chunk c3 = new Chunk("Datos del Cliente", smallBold);
        //            Chunk c4 = new Chunk("Nombre: "+cliente.getRazonSocial(),smallBold);
        //            Chunk c5 = new Chunk("Poblacin: "+cliente.getPoblacion(),smallBold);
        //            Chunk c6 = new Chunk("Provincia: "+cliente.getProvincia(),smallBold);
        //            Chunk c7 = new Chunk("Cdigo Postal: "+cliente.getCp(),smallBold);
        //            Chunk c8 = new Chunk("Num Telfono: Pepito"+cliente.getTlf1(),smallBold);
        Chunk c4 = new Chunk("Nombre: Jesus Eduardo Garcia Toril", smallBold);
        Chunk c5 = new Chunk("Poblacin: Pinto", smallBold);
        Chunk c6 = new Chunk("Provincia: Madrid", smallBold);
        Chunk c7 = new Chunk("Cdigo Postal: 28320", smallBold);
        Chunk c8 = new Chunk("Num Telfono: 659408182", smallBold);

        Paragraph parrafo = new Paragraph(chunk);
        Paragraph p2 = new Paragraph(c2);
        Paragraph p3 = new Paragraph(c3);
        Phrase ph1 = new Phrase(c4);
        Phrase ph2 = new Phrase(c5);
        Phrase ph3 = new Phrase(c6);
        Phrase ph4 = new Phrase(c7);
        Phrase ph5 = new Phrase(c8);

        // Let's create de first Chapter (Creemos el primer captulo)

        // We add an image (Aadimos una imagen)
        Image image;
        try {
            parrafo.setAlignment(Element.ALIGN_CENTER);
            image = Image.getInstance(iTextExampleImage);
            image.setAbsolutePosition(0, 750);
            p2.setAlignment(Element.ALIGN_LEFT);
            document.add(parrafo);
            document.add(image);
            document.add(p2);
            document.add(p3);
            document.add(ph1);
            document.add(ph2);
            document.add(ph3);
            document.add(ph4);
            document.add(ph5);

        } catch (BadElementException ex) {
            System.out.println("Image BadElementException" + ex);
        }

        // Second page - some elements
        // Segunda pgina - Algunos elementos

        // List by iText (listas por iText)
        String text = "test 1 2 3 ";
        for (int i = 0; i < 5; i++) {
            text = text + text;
        }
        List list = new List(List.UNORDERED);
        ListItem item = new ListItem(text);
        item.setAlignment(Element.ALIGN_JUSTIFIED);
        list.add(item);
        text = "a b c align ";
        for (int i = 0; i < 5; i++) {
            text = text + text;
        }
        item = new ListItem(text);
        item.setAlignment(Element.ALIGN_JUSTIFIED);
        list.add(item);
        text = "supercalifragilisticexpialidocious ";
        for (int i = 0; i < 3; i++) {
            text = text + text;
        }
        item = new ListItem(text);
        item.setAlignment(Element.ALIGN_JUSTIFIED);
        list.add(item);

        // How to use PdfPTable
        // Utilizacin de PdfPTable

        // We use various elements to add title and subtitle
        // Usamos varios elementos para aadir ttulo y subttulo
        Anchor anchor = new Anchor("Table export to PDF (Exportamos la tabla a PDF)", categoryFont);
        anchor.setName("Table export to PDF (Exportamos la tabla a PDF)");
        Chapter chapTitle = new Chapter(new Paragraph(anchor), 1);
        Paragraph paragraph = new Paragraph("Do it by Xules (Realizado por Xules)", subcategoryFont);
        Section paragraphMore = chapTitle.addSection(paragraph);
        paragraphMore.add(new Paragraph("This is a simple example (Este es un ejemplo sencillo)"));
        Integer numColumns = 6;
        Integer numRows = 120;
        // We create the table (Creamos la tabla).
        PdfPTable table = new PdfPTable(numColumns);
        // Now we fill the PDF table 
        // Ahora llenamos la tabla del PDF
        PdfPCell columnHeader;
        // Fill table rows (rellenamos las filas de la tabla).                
        for (int column = 0; column < numColumns; column++) {
            columnHeader = new PdfPCell(new Phrase("COL " + column));
            columnHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(columnHeader);
        }
        table.setHeaderRows(1);
        // Fill table rows (rellenamos las filas de la tabla).                
        for (int row = 0; row < numRows; row++) {
            for (int column = 0; column < numColumns; column++) {
                table.addCell("Row " + row + " - Col" + column);
            }
        }
        // We add the table (Aadimos la tabla)
        paragraphMore.add(table);
        // We add the paragraph with the table (Aadimos el elemento con la tabla).
        document.add(chapTitle);
        document.close();
        System.out.println("Your PDF file has been generated!(Se ha generado tu hoja PDF!");
    } catch (DocumentException documentException) {
        System.out.println(
                "The file not exists (Se ha producido un error al generar un documento): " + documentException);
    }
}

From source file:CTD.planer2.util.ExportToPdf.java

/**
 * In this function, we add the content to the pdf-file
 * // w w  w. j av  a  2s  .c o  m
 * @param document
 * @throws DocumentException
 */
private static void addContent(Document document) throws DocumentException {

    // Big header
    Chapter catPart = new Chapter(new Paragraph(""), 1);
    catPart.setNumberDepth(0);
    if (roomToExport == null && CPlaner.selDay == null) {
        String[] tmp = FILE.split("\\.");
        catPart.add(new Paragraph(tmp[0], catFont));
    } else if (CPlaner.selDay == null) {
        catPart.add(new Paragraph(roomToExport.toString(), catFont));
    } else {
        catPart.add(new Paragraph(CPlaner.selDay.toString(), catFont));
    }
    addEmptyLine(catPart, 1);

    // Add a table
    if (CPlaner.selDay != null) {
        createDayTable(catPart);
    } else {

        if (roomToExport == null && CPlaner.selDay == null) {
            createTable(catPart);
        } else {
            createTable(catPart, roomToExport);
        }
    }
    document.add(catPart);
}

From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java

License:Apache License

/**
 * Erzeugt das Inhaltsverzeichnis aus den bereits vorhandenen Elementen in
 * der Liste <code>listEntries</code>.
 * //from   w w w  .j  ava 2  s .  c  o  m
 * @param docPdf
 *            Zieldokument, falls Inhaltsverzeichnis nicht temporr erzeugt
 *            wird
 * @param temp
 *            Gibt an, ob das Inhaltsverzeichnis temporr in einer neuen
 *            Datei/Dokument erzeugt werden soll
 * @return Anzahl der Seiten
 * @throws DocumentException
 * @throws IOException
 */
private int erzeugeInhaltsverzeichnis(Document docPdf, boolean temp) throws DocumentException, IOException {

    int anzPages = 0;
    Document docInhalt = docPdf;
    String filePathTempInhaltString = "";

    if (temp) {
        // temp. Dateinamen bestimmen
        File fileDokuFile = new File(dateiname);
        filePathTempInhaltString = fileDokuFile.getParent() + "/tmp_inhalt.pdf";
        // Neues Dokument erzeugen
        docInhalt = initPdfWriterAndDocument(filePathTempInhaltString, false);
    }

    // berschrift
    Chapter currChapter = new Chapter(getParagraphChapter("Inhaltsverzeichnis"), 0);
    // 0, damit keine Nummerierung
    currChapter.setNumberDepth(0);
    docInhalt.add(currChapter);
    // eine Zeile Abstand
    docInhalt.add(getEmptyLineTextHalf());

    for (ContPdfEntry currEntry : listEntries) {

        // Eintrag erzeugen inkl. Abstand
        String strEintrag = currEntry.getBezeichnung() + "  ";
        Chunk chunkBezeichnung;
        Chunk chunkSeitenzahlChunk;
        if (currEntry.getParentEntry() == null) {
            // 1. Ebene => fett, Abstand davor einfgen
            docInhalt.add(getEmptyLineTextHalf());
            chunkBezeichnung = getChunkTextBold(strEintrag);
            chunkSeitenzahlChunk = getChunkTextBold("" + currEntry.getPageNumber());
        } else {
            // 2. Ebene
            chunkBezeichnung = getChunkText(strEintrag);
            chunkSeitenzahlChunk = getChunkText("" + currEntry.getPageNumber());
        }
        // Referenz setzen
        chunkBezeichnung.setLocalGoto(currEntry.getDestination());
        chunkSeitenzahlChunk.setLocalGoto(currEntry.getDestination());
        // Abstandzeichen generieren, Breite auffllen
        float widthAbstand = docInhalt.getPageSize().getWidth() * 0.81f;
        ;
        while (chunkBezeichnung.getWidthPoint() <= widthAbstand) {
            chunkBezeichnung.append(".");
        }

        // Tabelle erzeugen und formatieren
        PdfPTable currTable = new PdfPTable(2);
        currTable.setWidthPercentage(100f);
        currTable.setWidths(new int[] { 96, 4 });

        // Inhalte einfgen
        // Zelle Bezeichnung
        PdfPCell currCellBezeichnung = new PdfPCell(new Phrase(chunkBezeichnung));
        currCellBezeichnung.setBorder(0);
        currCellBezeichnung.setHorizontalAlignment(Element.ALIGN_JUSTIFIED_ALL);

        // Zelle Seitennummer
        PdfPCell currCellPageNumberCell = new PdfPCell(new Phrase(chunkSeitenzahlChunk));
        currCellPageNumberCell.setBorder(0);
        currCellPageNumberCell.setHorizontalAlignment(Element.ALIGN_RIGHT);

        // Zellen zur Tabelle hinzufgen
        currTable.addCell(currCellBezeichnung);
        currTable.addCell(currCellPageNumberCell);

        docInhalt.add(currTable);
    }

    if (temp) {
        // Dokument schlieen
        docInhalt.close();

        // Anzahl der Seitenzahlen bestimmen
        PdfReader reader = new PdfReader(filePathTempInhaltString);
        anzPages = reader.getNumberOfPages();
        reader.close();

        // temp. Datei lschen
        File currFileInhaltFile = new File(filePathTempInhaltString);
        currFileInhaltFile.delete();
    }
    return anzPages;
}