Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 31, 2022 08:37 am GMT

Java - How to Create Lists in a PDF Document

Grouping related items in a list makes the information easy to read. There are three very common list types: ordered lists (such as numbered lists), unordered lists (such as bulleted lists), and nested lists. In this article, I am going to introduce how to create a numbered list, a bulleted list and a multi-level list in Java using Free Spire.PDF for Java.

  • Create a numbered list
  • Create a bulleted list
  • Create a nested list

Add Spire.Pdf.jar as dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

<repositories>    <repository>        <id>com.e-iceblue</id>        <name>e-iceblue</name>        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>    </repository></repositories><dependencies>    <dependency>        <groupId>e-iceblue</groupId>        <artifactId>spire.pdf.free</artifactId>        <version>5.1.0</version>    </dependency></dependencies>

If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.

Prerequisite knowledge

Spire.PDF for Java provides the PdfSortedList class and PdfUnsortedList class to work with the ordered lists and unordered lists in a PDF document. The following table lists the important classes, methods, and properties involved in this tutorial.

Classes

Create a numbered list

The steps to create a numbered list are as follows:

  • Create a PDF document.
  • Create a brush object and a font object.
  • Create an instance of PdfSortedList class, specifying the list content, font, indent, and brush.
  • Draw the list at the specified location of a page.
  • Save the document to a PDF file.The following is the code snippet of creating a numbered list in PDF using Spire.PDF for Java.
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 margins        PdfMargins margins = new PdfMargins(30);        //Add a page        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margins);        //Specify the initial coordinate        float x = 0;        float y = 0;        //Draw title        PdfBrush brush = PdfBrushes.getBlack();        PdfFont titleFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Bold);        String title = "4 Tips to Learn Java 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 = "Start with basics
"
+"Make some notes
"
+"Try with small projects
"
+"Practice code daily"; 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"); }}

NumberedList

Create a bulleted list

The steps to create a bulleted list are as follows.

  • Create a PDF document.
  • Create a brush object and a font object.
  • Create an instance of PdfUnsortedList class, specifying the list content, font, indent, and brush.
  • Draw the list at the specified location of a page.
  • Save the document to a PDF file.The following code example shows you how to create a bulleted list using Spire.PDF for Java.
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(30);        //Add a page        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);        //Specify the initial coordinate        float x = 0;        float y = 0;        //Draw title        PdfBrush brush = PdfBrushes.getBlack();        PdfFont titleFont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Bold);        String title = "Worst Fruit for Weight Loss:";        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 = "Bananas
"
+"Grapes
"
+"Pomegranate
"
+"Apples
"
+"Blueberries"; 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"); }}

BulletedList

Create a nested list

The main steps to create a multi-level list are as follows.

  • Create a PDF document.
  • Create a parent list.
  • Create two sub lists and add them to two separate items of the parent list as sub list.
  • Create a sub-sub list and add it to an item of a sub list.
  • Save the document to a PDF file.The following provides a code example of creating a nested list in a PDF document using Spire.PDF for Java.
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 margins        PdfMargins margin = new PdfMargins(30);        //Add a page        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);        //Specify the initial coordinate        float x = 0;        float y = 0;        //Create two brushes        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 = "XHTML Tutorials/FAQs:";        String title = "Multi Level 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.Numeric, listFont);        PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);        //Create a parent list        String parentListContent = "Parent Item 1
"
+ "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
"
+ "Child Item 1-2"; PdfSortedList subList_1 = new PdfSortedList(subListContent_1); subList_1.setIndent(16); subList_1.setFont(listFont); subList_1.setBrush(purpleBrush); subList_1.setMarker(marker2); subList_1.setMarkerHierarchy(true); //Create another sub list -"subList_2" String subListContent_2 = "Child Item 2-1
"
+ "Child Item 2-2
"
+ "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); subList_2.setMarkerHierarchy(true); //Create a sub-sub list - "subSubList" String subSubListContent = "Child Item 1-1-1
"
+ "Child Item 1-1-2"; PdfSortedList subSubList = new PdfSortedList(subSubListContent); subSubList.setIndent(24); subSubList.setFont(listFont); subSubList.setBrush(purpleBrush); subSubList.setMarker(marker2); subSubList.setMarkerHierarchy(true); //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); //Set sub-sub list as the sub list of "subList_1" PdfListItem item_1_1 = subList_1.getItems().get(0); item_1_1.setSubList(subSubList); //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"); }}

NestedList


Original Link: https://dev.to/alexis92/java-how-to-create-lists-in-a-pdf-document-5do8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To