728x90 AdSpace

Sunday, 28 May 2017

iText 5 PDF - how to remove symbol from list

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

Maven Dependency



 com.itextpdf
 itextpdf
 5.5.11

RemoveListSymbol.java
package com.javatutorialcorner.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class RemoveListSymbol {
 
  public static final String DEST = "C:/JTC/RemoveListSymbol.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveListSymbol().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
 
        // This is how not to do it (but it works anyway):
 
        // We create a list:
        List list = new List();          
        list.setListSymbol("");
        list.add(new ListItem("Item 1"));
        list.add(new ListItem("Item 2"));
        list.add(new ListItem("Item 3"));
 
        // We wrap this list in a phrase:     
        Phrase phrase = new Phrase();
        phrase.add(list);
        // We add this phrase to a cell
        PdfPCell phraseCell = new PdfPCell();
        phraseCell.addElement(phrase);           
 
        // We add the cell to a table:
        PdfPTable phraseTable = new PdfPTable(2);
        phraseTable.setSpacingBefore(5);
        phraseTable.addCell("List wrapped in a phrase:");
        phraseTable.addCell(phraseCell);
 
        // We wrap the phrase table in another table:
        Phrase phraseTableWrapper = new Phrase();
        phraseTableWrapper.add(phraseTable);
 
        // We add these nested tables to the document:
        document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
        document.add(phraseTableWrapper);
 
        // This is how to do it:
 
        // We add the list directly to a cell:
        PdfPCell cell = new PdfPCell();
        cell.addElement(list);
 
        // We add the cell to the table:
        PdfPTable table = new PdfPTable(2);
        table.setSpacingBefore(5);
        table.addCell("List placed directly into cell");
        table.addCell(cell);
 
        // We add the table to the document:
        document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table:"));
        document.add(table);
 
        // Avoid adding tables to phrase (but it works anyway):
 
        Phrase tableWrapper = new Phrase();
        tableWrapper.add(table);document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
        document.add(tableWrapper);
 
        document.close();
    }
}

Output

Reference : iText Website
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 PDF - how to remove symbol from list Rating: 5 Reviewed By: Annamalai Thangaraj