We are moved to new domain
Click -> www.ehowtonow.com
Monday, 5 June 2017

iText 5 - Adding annotations to a table cell

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

LinkInTableCell.java
package com.javatutorialcorner.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
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 LinkInTableCell {
	 public static final String DEST = "C:/JTC/LinkInTableCell.pdf";
 
    class LinkInCell implements PdfPCellEvent {
        protected String url;
        public LinkInCell(String url) {
            this.url = url;
        }
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfWriter writer = canvases[0].getPdfWriter();
            PdfAction action = new PdfAction(url);
            PdfAnnotation link = PdfAnnotation.createLink(
                writer, position, PdfAnnotation.HIGHLIGHT_INVERT, action);
            writer.addAnnotation(link);
        }
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(1);
        // Part of the content is a link:
        Phrase phrase = new Phrase();
        phrase.add("The founders of iText are nominated for a ");
        Chunk chunk = new Chunk("European Business Award!");
        chunk.setAnchor("http://itextpdf.com/blog/european-business-award-kick-ceremony");
        phrase.add(chunk);
        table.addCell(phrase);
        // The complete cell is a link:
        PdfPCell cell = new PdfPCell(new Phrase("Help us win a European Business Award!"));
        cell.setCellEvent(new LinkInCell("http://itextpdf.com/blog/help-us-win-european-business-award"));
        table.addCell(cell);
        document.add(table);
        document.close();
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new LinkInTableCell().createPdf(DEST);
    }
}

Output
Reference : iText Website

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 - Adding annotations to a table cell Rating: 5 Reviewed By: eHowToNow