Java IText:列表

时间:2020-01-09 14:20:00  来源:igfitidea点击:

我们可以使用IText将有序列表和无序列表添加到PDF文档中。列表由类" com.itextpdf.text.List"表示。列表项由" com.itextpdf.text.ListItem"类表示。

这是一个简单的代码示例:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ListExample {

    public static void main(String[] args) {

        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("List.pdf"));

            document.open();

            List orderedList = new List(List.ORDERED);
            orderedList.add(new ListItem("Item 1"));
            orderedList.add(new ListItem("Item 2"));
            orderedList.add(new ListItem("Item 3"));

            document.add(orderedList);

            List unorderedList = new List(List.UNORDERED);
            unorderedList.add(new ListItem("Item 1"));
            unorderedList.add(new ListItem("Item 2"));
            unorderedList.add(new ListItem("Item 3"));

            document.add(unorderedList);

            document.close();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

罗马和希腊数字

我们也可以使用罗马和希腊数字创建列表。为此,请使用com.itextpdf.text.RomanListcom.itextpdf.text.GreekList类。这是一个例子:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class List2Example {

    public static void main(String[] args) {

        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("List2.pdf"));

            document.open();

            RomanList romanList = new RomanList();
            romanList.add(new ListItem("Item 1"));
            romanList.add(new ListItem("Item 2"));
            romanList.add(new ListItem("Item 3"));

            document.add(romanList);

            GreekList greekList = new GreekList();
            greekList.add(new ListItem("Item 1"));
            greekList.add(new ListItem("Item 2"));
            greekList.add(new ListItem("Item 3"));

            document.add(greekList);

            document.close();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

ZapfDingbats列表

IText具有使用ZapfDingbats字体的特殊列表实现。它的构造函数有两个参数:用作项目项目符号的符号编号,以及项目符号后的文本缩进(项目符号和文本之间的空格)。这是一个代码示例:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class List3Example {

  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("List3.pdf"));

      document.open();

      ZapfDingbatsList zapfDingbatsList1 =
            new ZapfDingbatsList(40, 15);
      zapfDingbatsList1.add(new ListItem("Item 1"));
      zapfDingbatsList1.add(new ListItem("Item 2"));
      zapfDingbatsList1.add(new ListItem("Item 3"));

      document.add(zapfDingbatsList1);

      ZapfDingbatsList zapfDingbatsList2 =
            new ZapfDingbatsList(43, 30);
      zapfDingbatsList2.add(new ListItem("Item 1"));
      zapfDingbatsList2.add(new ListItem("Item 2"));
      zapfDingbatsList2.add(new ListItem("Item 3"));

      document.add(zapfDingbatsList2);

      ZapfDingbatsList zapfDingbatsList3 =
            new ZapfDingbatsList(47, 45);
      zapfDingbatsList3.add(new ListItem("Item 1"));
      zapfDingbatsList3.add(new ListItem("Item 2"));
      zapfDingbatsList3.add(new ListItem("Item 3"));

      document.add(zapfDingbatsList3);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }
}