Chapter 4: examples AbstractElement (part 1)

This is a code example of iText PDF, discover more.

2nd July 2016
admin-marketing

Switch code for this example

C04E01_DivExample1.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E01_DivExample1 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV1.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E01_DivExample1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List record : resultSet) {
            Div div = new Div()
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            div.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            File file = new File(String.format(
                "src/main/resources/img/%s.jpg", record.get(0)));
            if (file.exists()) {
                Image img = new Image(ImageDataFactory.create(file.getPath()));
                img.scaleToFit(10000, 120);
                div.add(img);
            }
            document.add(div);
        }
        document.close();
    }
    
}
C04E02_DivExample2.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E02_DivExample2 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV2.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E02_DivExample2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List record : resultSet) {
            Div div = new Div()
                .setKeepTogether(true)
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            div.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            File file = new File(String.format(
                "src/main/resources/img/%s.jpg", record.get(0)));
            if (file.exists()) {
                Image img = new Image(ImageDataFactory.create(file.getPath()));
                img.scaleToFit(10000, 120);
                div.add(img);
            }
            document.add(div);
        }
        document.close();
    }
    
}
C04E03_LineSeparatorExample.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.*;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E03_LineSeparatorExample {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV3.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E03_LineSeparatorExample().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        SolidLine line = new SolidLine(1f);
        line.setColor(Color.RED);
        LineSeparator ls = new LineSeparator(line);
        ls.setWidthPercent(50);
        ls.setMarginTop(5);
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List record : resultSet) {
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            Div div = new Div()
                .setKeepTogether(true)
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10)
                .add(new Paragraph(movie.setFontSize(14f)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            File file = new File(String.format(
                "src/main/resources/img/%s.jpg", record.get(0)));
            if (file.exists()) {
                Image img = new Image(ImageDataFactory.create(file.getPath()));
                img.scaleToFit(10000, 120);
                div.add(img);
            }
            div.add(ls);
            document.add(div);
        }
        document.close();
    }
    
}
C04E04_ParagraphAndDiv1.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E04_ParagraphAndDiv1 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV1.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E04_ParagraphAndDiv1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            Paragraph title = new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0);
            div = new Div()
                .add(title)
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);

        //Close document
        document.close();
    }

}
C04E05_ParagraphAndDiv2.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E05_ParagraphAndDiv2 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV2.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E05_ParagraphAndDiv2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0)
                .setKeepWithNext(true));
            div = new Div()
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);

        //Close document
        document.close();
    }

}
C04E05_ParagraphAndDiv3.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E05_ParagraphAndDiv3 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV3.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E05_ParagraphAndDiv3().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0)
                .setKeepWithNext(true));
            div = new Div()
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                        .setMultipliedLeading(1.2f)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);

        //Close document
        document.close();
    }

}
C04E06_CustomParagraph.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.CanvasArtifact;
import com.itextpdf.kernel.pdf.tagutils.IAccessibleElement;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.Background;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.ParagraphRenderer;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E06_CustomParagraph {
    
    class MyParagraphRenderer extends ParagraphRenderer {

        public MyParagraphRenderer(Paragraph modelElement) {
            super(modelElement);
        }

        @Override
        public void drawBackground(DrawContext drawContext) {
            Background background = this.getProperty(Property.BACKGROUND);
            if (background != null) {
                Rectangle bBox = getOccupiedAreaBBox();
                boolean isTagged =
                    drawContext.isTaggingEnabled()
                    && getModelElement() instanceof IAccessibleElement;
                if (isTagged) {
                    drawContext.getCanvas().openTag(new CanvasArtifact());
                }
                Rectangle bgArea = applyMargins(bBox, false);
                if (bgArea.getWidth() <= 0 || bgArea.getHeight() <= 0) {
                    return;
                }
                drawContext.getCanvas().saveState()
                    .setFillColor(background.getColor())
                    .roundRectangle(
                    (double)bgArea.getX() - background.getExtraLeft(),
                    (double)bgArea.getY() - background.getExtraBottom(),
                    (double)bgArea.getWidth()
                        + background.getExtraLeft() + background.getExtraRight(),
                    (double)bgArea.getHeight()
                        + background.getExtraTop() + background.getExtraBottom(),
                    5)
                    .fill().restoreState();
                if (isTagged) {
                    drawContext.getCanvas().closeTag();
                }
            }
        }
    }
    
    public static final String DEST = "results/chapter04/custom_paragraph.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E06_CustomParagraph().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        
        Paragraph p1 = new Paragraph(
            "The Strange Case of Dr. Jekyll and Mr. Hyde");
        p1.setBackgroundColor(Color.ORANGE);
        document.add(p1);
        
        Paragraph p2 = new Paragraph(
            "The Strange Case of Dr. Jekyll and Mr. Hyde");
        p2.setBackgroundColor(Color.ORANGE);
        p2.setNextRenderer(new MyParagraphRenderer(p2));
        document.add(p2);
        
        document.close();
    }
}
C04E07_ListTypes.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.property.ListNumberingType;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E07_ListTypes {
    
    public static final String DEST = "results/chapter04/list_types.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E07_ListTypes().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;

        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));    

        List list = new List();
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.DECIMAL);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ENGLISH_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ENGLISH_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.GREEK_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.GREEK_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ROMAN_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ROMAN_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_1);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_2);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_3);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_4);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        //Close document
        document.close();
    }
}
C04E08_CustomListSymbols.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.ListNumberingType;
import com.itextpdf.layout.property.ListSymbolAlignment;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E08_CustomListSymbols {
    
    public static final String DEST = "results/chapter04/custom_list_symbols.pdf";
    public static final String INFO = "src/main/resources/img/test/info.png";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E08_CustomListSymbols().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;

        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));    

        List list = new List();
        list.setListSymbol("\u2022");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List();
        PdfFont font = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);
        list.setListSymbol(new Text("*").setFont(font).setFontColor(Color.ORANGE));
        list.setSymbolIndent(10);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        Image info = new Image(ImageDataFactory.create(INFO));
        info.scaleAbsolute(12, 12);
        list = new List().setSymbolIndent(3);
        list.setListSymbol(info);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List();
        list.setListSymbol(ListNumberingType.ENGLISH_LOWER);
        list.setPostSymbolText("- ");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.DECIMAL);
        list.setPreSymbolText("Part ");
        list.setPostSymbolText(": ");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.DECIMAL);
        list.setItemStartIndex(5);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ROMAN_LOWER);
        list.setListSymbolAlignment(ListSymbolAlignment.LEFT);
        for (int i = 0; i < 6; i++) {
            list.add("Dr. Jekyll");
            list.add("Mr. Hyde");
        }
        document.add(list);
        //Close document
        document.close();
    }
}
C04E09_ListItemExample.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.ListNumberingType;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E09_ListItemExample {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E09_ListItemExample().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        com.itextpdf.layout.element.List list =
            new com.itextpdf.layout.element.List(ListNumberingType.DECIMAL);
        for (List record : resultSet) {
            ListItem li = new ListItem();
            li.setKeepTogether(true);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            li.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            File file = new File(String.format(
                "src/main/resources/img/%s.jpg", record.get(0)));
            if (file.exists()) {
                Image img = new Image(ImageDataFactory.create(file.getPath()));
                img.scaleToFit(10000, 120);
                li.add(img);
            }
            list.add(li);
        }
        document.add(list);
        document.close();
    }
    
}
C04E10_NestedLists.java
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter04;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.property.ListNumberingType;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class C04E10_NestedLists {
    
    public static final String DEST = "results/chapter04/nested_list.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E10_NestedLists().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;

        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));    

        List list = new List();
        
        List list1 = new List(ListNumberingType.DECIMAL);
        List listEL = new List(ListNumberingType.ENGLISH_LOWER);
        listEL.add("Dr. Jekyll");
        listEL.add("Mr. Hyde");
        ListItem liEL = new ListItem();
        liEL.add(listEL);
        list1.add(liEL);
        List listEU = new List(ListNumberingType.ENGLISH_UPPER);
        listEU.add("Dr. Jekyll");
        listEU.add("Mr. Hyde");
        ListItem liEU = new ListItem();
        liEU.add(listEU);
        list1.add(liEU);
        ListItem li1 = new ListItem();
        li1.add(list1);
        list.add(li1);
        
        ListItem li = new ListItem();
        List listGL = new List(ListNumberingType.GREEK_LOWER);
        listGL.add("Dr. Jekyll");
        listGL.add("Mr. Hyde");
        li.add(listGL);
        List listGU = new List(ListNumberingType.GREEK_UPPER);
        listGU.add("Dr. Jekyll");
        listGU.add("Mr. Hyde");
        li.add(listGU);
        List listRL = new List(ListNumberingType.ROMAN_LOWER);
        listRL.add("Dr. Jekyll");
        listRL.add("Mr. Hyde");
        li.add(listRL);
        List listRU = new List(ListNumberingType.ROMAN_UPPER);
        listRU.add("Dr. Jekyll");
        listRU.add("Mr. Hyde");
        li.add(listRU);
        list.add(li);
        
        List listZ1 = new List(ListNumberingType.ZAPF_DINGBATS_1);
        listZ1.add("Dr. Jekyll");
        listZ1.add("Mr. Hyde");
        ListItem liZ1 = new ListItem();
        liZ1.add(listZ1);
        
        List listZ2 = new List(ListNumberingType.ZAPF_DINGBATS_2);
        listZ2.add("Dr. Jekyll");
        listZ2.add("Mr. Hyde");
        ListItem liZ2 = new ListItem();
        liZ2.add(listZ2);
        
        List listZ3 = new List(ListNumberingType.ZAPF_DINGBATS_3);
        listZ3.add("Dr. Jekyll");
        listZ3.add("Mr. Hyde");
        ListItem liZ3 = new ListItem();
        liZ3.add(listZ3);
        
        List listZ4 = new List(ListNumberingType.ZAPF_DINGBATS_4);
        listZ4.add("Dr. Jekyll");
        listZ4.add("Mr. Hyde");
        ListItem liZ4 = new ListItem();
        liZ4.add(listZ4);
        
        listZ3.add(liZ4);
        listZ2.add(liZ3);
        listZ1.add(liZ2);
        list.add(liZ1);
        document.add(list);
        //Close document
        document.close();
    }
}
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now