List examples

Tags: Listlabellist symbolListItemiText 5
Files: 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27844914/align-justified-for-itext-list-item
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ListAlignment {
 
    public static final String DEST = "results/objects/list_alignment.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListAlignment().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        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);
        document.add(list);
        document.close();
    }
}
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/33277998/itextpdf-numbered-list-with-no-indent
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class ListNumbers {
 
    public static final String DEST = "results/objects/list_without_indent.pdf";
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListNumbers().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("Numbered list with automatic indentation"));
        List list1 = new List(List.ORDERED);
        list1.setFirst(8);
        for (int i = 0; i < 5; i++) {
            list1.add("item");
        }
        document.add(list1);
        document.add(new Paragraph("Numbered list without indentation"));
        List list2 = new List(List.ORDERED);
        list2.setFirst(8);
        list2.setAlignindent(false);
        for (int i = 0; i < 5; i++) {
            list2.add("item");
        }
        document.add(list2);
        document.add(new Paragraph("Nested numbered list"));
        List list3 = new List(List.ORDERED);
        list3.setFirst(8);
        list3.setAlignindent(false);
        list3.setPostSymbol(" ");
        for (int i = 0; i < 5; i++) {
            list3.add("item");
            List list = new List(List.ORDERED);
            list.setPreSymbol(String.valueOf(8 + i) + "_");
            list.setPostSymbol(" ");
            list.add("item 1");
            list.add("item 2");
            list3.add(list);
        }
        document.add(list3);
        document.close();
    }
 
}
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ListWithImageAsBullet {
 
    public static final String IMG = "resources/images/bulb.gif";
    public static final String DEST = "results/objects/list_with_image_bullet.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListWithImageAsBullet().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Image image = Image.getInstance(IMG);
        image.scaleAbsolute(12, 12);
        image.setScaleToFitHeight(false);
        List list = new List();
        list.setListSymbol(new Chunk(Image.getInstance(image), 0, 0));
        list.add("Hello World");
        list.add("This is a list item with a lot of text. It will certainly take more than one line. This shows that the list item is indented and that the image is used as bullet.");
        list.add("This is a test");
        document.add(list);
        document.close();
    }
 
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/21579204/how-to-set-label-to-itext-list
 * 
 * Workaround to add a label to a list.
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ListWithLabel {
 
    public static final String DEST = "results/objects/list_with_label.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListWithLabel().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(200);
        table.setWidths(new int[]{ 1, 10 });
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        PdfPCell cell;
        cell = new PdfPCell();
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.addElement(new Paragraph("Label"));
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setBorder(PdfPCell.NO_BORDER);
        List list = new List(List.UNORDERED);
        list.add(new ListItem(new Chunk("Value 1")));
        list.add(new ListItem(new Chunk("Value 2")));
        list.add(new ListItem(new Chunk("Value 3")));
        cell.addElement(list);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29566115/adding-veritcal-spacing-itextsharp-in-list
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ListWithLeading {
 
    public static final String DEST = "results/objects/list_with_leading.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ListWithLeading().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Font font = new Font(FontFamily.HELVETICA, 12);
        List list1 = new List(12);
        list1.setListSymbol("\u2022");
        list1.add(new ListItem("Value 1", font));
        list1.add(new ListItem("Value 2", font));
        list1.add(new ListItem("Value 3", font));
        document.add(list1);
        List list2 = new List(12);
        list2.setListSymbol("\u2022");
        list2.add(new ListItem(30, "Value 1", font));
        list2.add(new ListItem(30, "Value 2", font));
        list2.add(new ListItem(30, "Value 3", font));
        list2.setIndentationLeft(60);
        document.add(list2);
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27820159/itext-list-how-to-remove-symbol-from-list
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class RemoveListSymbol {
 
    public static final String DEST = "results/objects/remove_listsymbol.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveListSymbol().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
 
        // This is how not to do it (but it works anyway):
 
        // We create a list:
        List list = new List();          
        list.setListSymbol("");
        list.add(new ListItem("Item 1"));
        list.add(new ListItem("Item 2"));
        list.add(new ListItem("Item 3"));
 
        // We wrap this list in a phrase:     
        Phrase phrase = new Phrase();
        phrase.add(list);
        // We add this phrase to a cell
        PdfPCell phraseCell = new PdfPCell();
        phraseCell.addElement(phrase);           
 
        // We add the cell to a table:
        PdfPTable phraseTable = new PdfPTable(2);
        phraseTable.setSpacingBefore(5);
        phraseTable.addCell("List wrapped in a phrase:");
        phraseTable.addCell(phraseCell);
 
        // We wrap the phrase table in another table:
        Phrase phraseTableWrapper = new Phrase();
        phraseTableWrapper.add(phraseTable);
 
        // We add these nested tables to the document:
        document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
        document.add(phraseTableWrapper);
 
        // This is how to do it:
 
        // We add the list directly to a cell:
        PdfPCell cell = new PdfPCell();
        cell.addElement(list);
 
        // We add the cell to the table:
        PdfPTable table = new PdfPTable(2);
        table.setSpacingBefore(5);
        table.addCell("List placed directly into cell");
        table.addCell(cell);
 
        // We add the table to the document:
        document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table:"));
        document.add(table);
 
        // Avoid adding tables to phrase (but it works anyway):
 
        Phrase tableWrapper = new Phrase();
        tableWrapper.add(table);document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
        document.add(tableWrapper);
 
        document.close();
    }
}
Resources: 
File nameRaw URLUpdated
bulb.gifbulb.gif2015-11-08 12:36 pm