List

List (1)

Thursday, 06 December 2018 08:59

Create number list and bullet list on PDF in JAVA

Written by support iceblue

This article demonstrates how to use Spire. PDF for Java to create the bullet list and number list to PDF file.

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

public class PDFlist {
    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();
    }
}

Create number list and bullet list on PDF in JAVA

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

public class PDFlist {
    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/list.pdf");
        doc.close();
    }
}

Create number list and bullet list on PDF in JAVA