u can add ordered and unordered lists to a PDF document using IText. List are represented by the class
com.itextpdf.text.List. List items are represented by the classcom.itextpdf.text.ListItem.
Here is a simple code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ListExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("List.pdf"));
document.open();
List orderedList = new List(List.ORDERED);
orderedList.add(new ListItem("Item 1"));
orderedList.add(new ListItem("Item 2"));
orderedList.add(new ListItem("Item 3"));
document.add(orderedList);
List unorderedList = new List(List.UNORDERED);
unorderedList.add(new ListItem("Item 1"));
unorderedList.add(new ListItem("Item 2"));
unorderedList.add(new ListItem("Item 3"));
document.add(unorderedList);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Here is what the generated document looks like:
| An IText List and ListItem example |
Roman and Greek Numerals
You can create lists with roman and greek numerals too. To do this, use the
com.itextpdf.text.RomanList and com.itextpdf.text.GreekList classes. Here is an example:import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class List2Example {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("List2.pdf"));
document.open();
RomanList romanList = new RomanList();
romanList.add(new ListItem("Item 1"));
romanList.add(new ListItem("Item 2"));
romanList.add(new ListItem("Item 3"));
document.add(romanList);
GreekList greekList = new GreekList();
greekList.add(new ListItem("Item 1"));
greekList.add(new ListItem("Item 2"));
greekList.add(new ListItem("Item 3"));
document.add(greekList);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Here is what the generated document looks like:
| An IText RomanList and GreekList example |
ZapfDingbatsList
IText has a special list implementation that uses the ZapfDingbats font. It's constructor takes two parameters: The number of the symbol to use as item bullet, and the indentation of the text after the bullet (space between bullet and text). Here is a code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class List3Example {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("List3.pdf"));
document.open();
ZapfDingbatsList zapfDingbatsList1 =
new ZapfDingbatsList(40, 15);
zapfDingbatsList1.add(new ListItem("Item 1"));
zapfDingbatsList1.add(new ListItem("Item 2"));
zapfDingbatsList1.add(new ListItem("Item 3"));
document.add(zapfDingbatsList1);
ZapfDingbatsList zapfDingbatsList2 =
new ZapfDingbatsList(43, 30);
zapfDingbatsList2.add(new ListItem("Item 1"));
zapfDingbatsList2.add(new ListItem("Item 2"));
zapfDingbatsList2.add(new ListItem("Item 3"));
document.add(zapfDingbatsList2);
ZapfDingbatsList zapfDingbatsList3 =
new ZapfDingbatsList(47, 45);
zapfDingbatsList3.add(new ListItem("Item 1"));
zapfDingbatsList3.add(new ListItem("Item 2"));
zapfDingbatsList3.add(new ListItem("Item 3"));
document.add(zapfDingbatsList3);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Here is what the generated document looks like:
| An IText ZapfDingbats example |
沒有留言:
張貼留言