15
Add Lists to PDF in Java
There are two types of lists commonly seen in electronic document: ordered list and unordered list. An ordered list is a collection of related items with a special order or sequence, which are usually organized by different schemes of numbers. Items in an unordered list have no obvious order and are usually organized by bullets.
Spire.PDF for Java provides the PdfSortedList class and PdfUnsortedList class for developers to create these two types of lists respectively. In the following sections, I am going to introduce how to add a numbered list, a bulleted list and a nested listed in PDF by using this library.
If you're creating a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf</artifactId>
<verson>4.1.2</version>
</dependency>
</dependencies>
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfSortedList;
public class CreateNumberedList {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margin
PdfMargins margin = new PdfMargins(60,60,40,40);
//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
//Specify the initial coordinate
float x = 0;
float y = 15;
//Draw title
PdfBrush brush = PdfBrushes.getBlack();
PdfFont titleFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Bold);
String title = "5 Tips to Learn English Quickly and Easily:";
page.getCanvas().drawString(title, titleFont, brush, x, y);
y = y + (float) titleFont.measureString(title).getHeight();
y = y + 5;
//Draw numbered list
PdfFont listFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Regular);
String listContent = "Practice,practice,practice\n"
+"Find your motivation\n"
+"Focus on communication first\n"
+"For better future output,get lots of input now\n"
+"Don't translate everything";
PdfSortedList list = new PdfSortedList(listContent);
list.setFont(listFont);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
list.draw(page, 0, y);
//Save to file
doc.saveToFile("output/NumberedList.pdf");
}
}
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfUnorderedList;
public class CreateBulletedList {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margin
PdfMargins margin = new PdfMargins(60,60,40,40);
//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
//Specify the initial coordinate
float x = 0;
float y = 15;
//Draw title
PdfBrush brush = PdfBrushes.getBlack();
PdfFont titleFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Bold);
String title = "5 Tips to Learn English Quickly and Easily:";
page.getCanvas().drawString(title, titleFont, brush, x, y);
y = y + (float) titleFont.measureString(title).getHeight();
y = y + 5;
//Draw bullet list
PdfFont listFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Regular);
String listContent = "Practice,practice,practice\n"
+"Find your motivation\n"
+"Focus on communication first\n"
+"For better future output,get lots of input now\n"
+"Don't translate everything";
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setFont(listFont);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
list.draw(page, 0, y);
//Save to file
doc.saveToFile("output/BulletedList.pdf");
}
}
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfNumberStyle;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfListItem;
import com.spire.pdf.lists.PdfOrderedMarker;
import com.spire.pdf.lists.PdfSortedList;
import java.awt.*;
import java.awt.geom.Point2D;
public class CreateMultiLevelList {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margin
PdfMargins margin = new PdfMargins(60, 60, 40, 40);
//Create one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
//Specify the initial coordinate
float x = 0;
float y = 15;
//Create two brushed
PdfBrush blackBrush = PdfBrushes.getBlack();
PdfBrush purpleBrush = PdfBrushes.getPurple();
//Create two fonts
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new java.awt.Font("Times New Roman", Font.BOLD, 12));
PdfTrueTypeFont listFont = new PdfTrueTypeFont(new java.awt.Font("Calibri Light", Font.PLAIN, 12));
//Draw title
String title = "Nested and ordered list";
page.getCanvas().drawString(title, titleFont, blackBrush, x, y);
y = y + (float) titleFont.measureString(title).getHeight();
y = y + 5;
//Create two ordered makers, which are used to define the number style of sorted list
PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Upper_Roman, listFont);
PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);
//Create a parent list
String parentListContent = "Parent Item 1\n"
+ "Parent Item 2";
PdfSortedList parentList = new PdfSortedList(parentListContent);
parentList.setFont(listFont);
parentList.setIndent(8);
parentList.setBrush(purpleBrush);
parentList.setMarker(marker1);
//Create a sub list - "subList_1"
String subListContent_1 = "Child Item 1-1\n"
+ "Child Item 1-2\n"
+ "Child Item 1-3\n"
+ "Child Item 1-4";
PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
subList_1.setIndent(16);
subList_1.setFont(listFont);
subList_1.setBrush(purpleBrush);
subList_1.setMarker(marker2);
//Create another sub list -"subList_2"
String subListContent_2 = "Child Item 2-1\n"
+ "Child Item 2-2\n"
+ "Child Item 2-3";
PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
subList_2.setIndent(16);
subList_2.setFont(listFont);
subList_2.setBrush(purpleBrush);
subList_2.setMarker(marker2);
//Set subList_1 as sub list of the first item of parent list
PdfListItem item_1 = parentList.getItems().get(0);
item_1.setSubList(subList_1);
//Set subList_2 as sub list of the second item of parent list
PdfListItem item_2 = parentList.getItems().get(1);
item_2.setSubList(subList_2);
//Draw parent list
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.setBreak(PdfLayoutBreakType.Fit_Page);
textLayout.setLayout(PdfLayoutType.Paginate);
parentList.draw(page,new Point2D.Float(x,y),textLayout);
//Save to file
doc.saveToFile("output/MultiLevelList.pdf");
}
}
15