Thursday 17, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 28 May 2017

iText 5 PDF - how to set label to itext list

To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository

Maven Dependency

2.<dependency>
3. <groupid>com.itextpdf</groupid>
4. <artifactid>itextpdf</artifactid>
5. <version>5.5.11</version>
6.</dependency>

ListWithLabel.java
01.package com.javatutorialcorner.itextpdf;
02. 
03.import com.itextpdf.text.Chunk;
04.import com.itextpdf.text.Document;
05.import com.itextpdf.text.DocumentException;
06.import com.itextpdf.text.Element;
07.import com.itextpdf.text.List;
08.import com.itextpdf.text.ListItem;
09.import com.itextpdf.text.Paragraph;
10.import com.itextpdf.text.pdf.PdfPCell;
11.import com.itextpdf.text.pdf.PdfPTable;
12.import com.itextpdf.text.pdf.PdfWriter;
13.  
14. 
15.import java.io.File;
16.import java.io.FileOutputStream;
17.import java.io.IOException;
18.  
19.public class ListWithLabel {
20.  
21.  public static final String DEST = "C:/JTC/ListWithLabel.pdf";
22.  
23.    public static void main(String[] args) throws IOException,
24.            DocumentException {
25.        File file = new File(DEST);
26.        file.getParentFile().mkdirs();
27.        new ListWithLabel().createPdf(DEST);
28.    }
29.  
30.    public void createPdf(String dest) throws IOException, DocumentException {
31.        Document document = new Document();
32.        PdfWriter.getInstance(document, new FileOutputStream(dest));
33.        document.open();
34.        PdfPTable table = new PdfPTable(2);
35.        table.setTotalWidth(200);
36.        table.setWidths(new int[]{ 1, 10 });
37.        table.setHorizontalAlignment(Element.ALIGN_LEFT);
38.        PdfPCell cell;
39.        cell = new PdfPCell();
40.        cell.setBorder(PdfPCell.NO_BORDER);
41.        cell.addElement(new Paragraph("Label"));
42.        table.addCell(cell);
43.        cell = new PdfPCell();
44.        cell.setBorder(PdfPCell.NO_BORDER);
45.        List list = new List(List.UNORDERED);
46.        list.add(new ListItem(new Chunk("Value 1")));
47.        list.add(new ListItem(new Chunk("Value 2")));
48.        list.add(new ListItem(new Chunk("Value 3")));
49.        cell.addElement(list);
50.        table.addCell(cell);
51.        document.add(table);
52.        document.close();
53.    }
54.}

Output

Reference : iText Website

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 PDF - how to set label to itext list Rating: 5 Reviewed By: eHowToNow