728x90 AdSpace

Saturday, 27 May 2017

iText 5 PDF Chunk - Bullets Example

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

 

Bullets.java
package com.javatutorialcorner.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
 


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

public class Bullets {
 
  public static final String DEST = "C:/JTC/Bullets.pdf";
   public static final String FONT = "C:/JTC/FreeSans.ttf";
    public static final String[] ITEMS = {
        "Insurance system", "Agent", "Agency", "Agent Enrollment", "Agent Settings",
        "Appointment", "Continuing Education", "Hierarchy", "Recruiting", "Contract",
        "Message", "Correspondence", "Licensing", "Party"
    };
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Bullets().createPdf(DEST);
    }
 
 
    public void createPdf(String dest) throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Font zapfdingbats = new Font(FontFamily.ZAPFDINGBATS, 8);
        Font font = new Font();
        Chunk bullet = new Chunk(String.valueOf((char) 108), zapfdingbats);
 
        Paragraph p = new Paragraph("Items can be split if they don't fit at the end: ", font);
        for (String item: ITEMS) {
            p.add(bullet);
            p.add(new Phrase(" " + item + " ", font));
        }
        document.add(p);
        document.add(Chunk.NEWLINE);
 
        p = new Paragraph("Items can't be split if they don't fit at the end: ", font);
        for (String item: ITEMS) {
            p.add(bullet);
            p.add(new Phrase("\u00a0" + item.replace(' ', '\u00a0') + " ", font));
        }
        document.add(p);
        document.add(Chunk.NEWLINE);
 
        BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f = new Font(bf, 12);
        p = new Paragraph("Items can't be split if they don't fit at the end: ", f);
        for (String item: ITEMS) {
            p.add(new Phrase("\u2022\u00a0" + item.replace(' ', '\u00a0') + " ", f));
        }
        document.add(p);
 
        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 Chunk - Bullets Example Rating: 5 Reviewed By: Annamalai Thangaraj