The ordered or unordered list in documents is a way to reflect the hierarchical relationship or the same property of the content. Compared with the simple text description, it can effectively enhance the organization of the document content and highlight the key points. Therefore, this article will share methods for setting up ordered or unordered lists in PDF documents through Java programming.
Use tools: Free Spire.PDF for Java V2.2.2 (free version)
Jar file import:
Step 1: create a new folder in Java program to be named Lib. And copy the jar file in the download package (as shown below) to the new folder.
Step 2: after copying the file, add it to the reference class library: select the jar file, right-click, and select "Build Path" – "Add to Build Path". Complete the reference.
Java code example (for reference)
import com.spire.pdf.*; import com.spire.pdf.graphics.*; import com.spire.pdf.lists.*; import java.awt.*; import java.awt.geom.*; public class list { public static void main(String[] args) { //Establish PDFDocument object PdfDocument doc = new PdfDocument(); //Setting margins PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)); margin.setBottom(margin.getTop()); margin.setLeft(unitCvtr.convertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)); margin.setRight(margin.getLeft()); //Add a new page PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin); //Drawing title float y = 10; PdfBrush brush1 = PdfBrushes.getBlack(); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.BOLD, 16), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.getCanvas().drawString("Department activity purchase list", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1); y = y + (float) font1.measureString("Activity purchase list", format1).getHeight(); y = y + 5; //Format and text list 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); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.BOLD, 12), true); String formatted1 = "Administration Department\n Development Department\n Sales Department\n Logistics Department\n Security Department"; String formatted2 = "Drinks\n Condiment\n candy\n Dairy\n meat\n Vegetables\n seafood"; //Draw unordered list PdfListBase list = new PdfUnorderedList(formatted2); list.setFont(font); list.setIndent(8); list.setTextIndent(5); list.setBrush(brush); PdfLayoutResult result = list.draw(page, 0, y); y = (float) (result.getBounds().getHeight()+result.getBounds().getY()); //Drawing sequence table PdfSortedList sortedList = new PdfSortedList(formatted1); sortedList.setFont(font); sortedList.setIndent(8); sortedList.setTextIndent(5); sortedList.setBrush(brush); sortedList.draw(page, 0, y); //Save document doc.saveToFile("list.pdf"); doc.close(); } }
List setting effect:
(end of this paper)