Thursday, 26 August 2021

Create bulleted lists, numbered lists and multilevel lists in PDF

A bulleted list is an unordered list which consists of items introduced by special symbols, a numbered list is an ordered list which contains numbers indicating the order of items and a multilevel list is a list of items which can contain other lists and every level of a multilevel list can be formatted by using various number formats including Arabic numerals, Roman numerals, letters and a combination of numbered and bulleted lists.

This article will show you how to create a bulleted list, a numbered list and a multilevel list in PDF using Java codes with the help of a free third-party library called Free Spire.PDF for Java.

DEPENDENCY

Before running codes, we need to add a Jar file called Spire.Pdf.jar to IDEA. There are two methods to do it. One is that downloading the package of the free library from the link, finding Spire.Pdf.jar in the “lib” folder and then manually adding it to IDEA. The other is that creating a Maven project in IDEA, typing the following codes in the pom.xml file and finally clicking the button “import changes”.

<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>3.9.0</version>
</dependency>
</dependencies>

RUNNING CODES

Create a bulleted list in PDF using Java codes

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.*;
import java.awt.*;
import java.awt.geom.*;

public class BulletList {
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);
float y = 10;
//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Categories List", format1).getHeight();
y = y + 5;

//Draw text string and set the font
Rectangle2D rctg = new Rectangle2D.Float();
rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";

//Create a bullet list
PdfListBase list = new PdfUnorderedList(formatted);
list.setFont(font);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
PdfLayoutResult result = list.draw(page, 0, y);

doc.saveToFile("output/BulletList.pdf");
doc.close();
}
}

Output


Create a numbered list in PDF using Java codes

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.*;
import java.awt.*;
import java.awt.geom.*;

public class NumberList {
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);
float y = 10;
//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Categories List", format1).getHeight();
y = y + 5;

//Draw text string and set the font
Rectangle2D rctg = new Rectangle2D.Float();
rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";

//Create a number list
PdfSortedList list = new PdfSortedList(formatted);
list.setFont(font);
list.setIndent(8);
list.setTextIndent(5);
list.setBrush(brush);
PdfLayoutResult result = list.draw(page, 0, y);

doc.saveToFile("output/numberlist.pdf");
doc.close();
}
}
Output

Create a multilevel list in PDF using Java codes

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 MultilevelList {
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 = "XHTML Tutorials/FAQs:";
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 = "Introduction To XHTML 1.0\n"
+ "Introduction To Tag and Attribute Syntax";
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 = "What Is XHTML?\n"
+ "What Does an XHMTL Document Look Like?\n"
+ "What Is the Relation between XHTML and HTML?\n"
+ "What Is the Relation between XHTML and XML?";
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 = "What Is an XHTML Element?\n"
+ "How To Enter Comments into XHTML Documents?\n"
+ "How To Write the Opening Tag of an XHTML Element?";
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");
}
}
Output


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...