Saturday, 5 November 2016

Itext: Set border colors to a table

PdfPCell class provides following methods to set border color.

public void setBorderColor(final BaseColor borderColor)
public void setBorderColorLeft(final BaseColor borderColorLeft)
public void setBorderColorRight(final BaseColor borderColorRight)
public void setBorderColorTop(final BaseColor borderColorTop)
public void setBorderColorBottom(final BaseColor borderColorBottom)

Following is the complete working application.

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(2);

  PdfPCell cell = new PdfPCell();
  cell.setBorderColor(BaseColor.GREEN);
  cell.setVerticalAlignment(Element.ALIGN_BOTTOM);

  cell.setPhrase(new Phrase("Number"));
  table.addCell(cell);

  cell.addElement(new Phrase("Square"));
  table.addCell(cell);

  for (int i = 1; i < 11; i++) {
   cell.setPhrase(new Phrase(Integer.toString(i)));
   table.addCell(cell);

   cell.setPhrase(new Phrase(Integer.toString(i * i)));
   table.addCell(cell);
  }

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

itext: Align text in table

PdfPCell class provides to align the text in table cell.

public void setHorizontalAlignment(int horizontalAlignment)
public void setVerticalAlignment(int verticalAlignment)

Following is the complete working application.

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(2);

  PdfPCell cell = new PdfPCell();
  cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);

  cell.setPhrase(new Phrase("Core Java"));
  table.addCell(cell);

  cell.setPhrase(new Phrase(
    "An introduction to Java Technology, It is best place to start for a beginner. You can learn OOPs, threads, Collections etc.,"));
  table.addCell(cell);

  cell.setPhrase(new Phrase("Design Patterns"));
  table.addCell(cell);

  cell.setPhrase(new Phrase(
    "A software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. I documented mostly used design patterns here."));
  table.addCell(cell);

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

Friday, 4 November 2016

Launch URI Scheme over java file

URI stands for Uniform Resource Identifier, used to uniquely identify a resource.

What is URI Scheme?
URI scheme tells how to interpret given URI.

In this post, I am going to explain how to launch URI scheme in windows. I am going to develop simple java application, where it read an url and open it in browser.

Following is my URI open class.

import java.awt.Desktop;
import java.io.IOException;

public class URIOpen {
 public static void main(String args[]) {
  if (args.length == 0) {
   return;
  }

  String uri = args[0];

  try {
   java.net.URI theURI = java.net.URI.create(uri);
   
   // System.out.println(theURI.getScheme()); => myDocs
   String uriBrowsablePart = theURI.getRawSchemeSpecificPart();
   
   // System.out.println(uriBrowsablePart); => http://google.com
   Desktop.getDesktop().browse(java.net.URI.create(uriBrowsablePart));
   
   // the above statement will open default browser on http://google.com
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
  
 }
}

Compile the class and generate URIOpen.class file.

It is time to register our application with windows registry.

Step 1:
To register an application to handle a particular URI scheme, add a new key, along with the appropriate sub keys and values, to HKEY_CLASSES_ROOT. The root key must match the URI scheme that is being added. For instance, to add an ‘myDocs:’ scheme, add an alert key to HKEY_CLASSES_ROOT, as follows.

Open run prompt and type ‘regedit’ and press Enter. It opens registry window like below.


Right click on ‘HKEY_CLASSES_ROOT’ and add new key to it.

Give the key name as ‘myDocs’.

You need to add new value under the key ‘myDocs’. Under this new key, the URL Protocol string value indicates that this key declares a custom pluggable protocol handler. Without this key, the handler application will not launch. The value should be an empty string.

Right click on ‘myDocs’ -> New -> String Value. Give the string value as ' URL Protocol’.

You need to add the value of ‘Default’ to "URL : myDocs Protocol"

Now you need to add two new keys ‘DefaultIcon and shell’ to ‘myDocs’.

Right click on myDocs -> New -> Key. Give the key value as ‘DefaultIcon’.

Right click on myDocs -> New -> Key. Give the key value as ‘shell’.


The name of the first key under the shell key should be an action verb, such as open. Under this key, a command key or a DDEEXEC key indicate how the handler should be invoked. The values under the command and DDEEXEC keys describe how to launch the application handling the new protocol.

Right click on shell -> New -> key. Give the key name as ‘open’.

Give new key named ‘command’ under the key ‘open’.

Right click on open -> New -> key. Give the key name as ‘command’.

Following is the complete tree structure.



Update the (Default) value field of the command key like below.

"C:\Program Files (x86)\Java\jdk1.8.0_102\bin\java.exe"  "C:\Users\I335077\Documents\Study\Miscellaneous\examples\URIOpen" "%1"

When user clicks on ‘myDocs:someURL’, it launch the application URIOpen.

For example, Open run prompt and type ‘mydocs:http://google.com’, it opens google.com in browser.

Itext: Add list of images to same cell in table

Following statements are used to add list of images to same cell in pdf table.

PdfPTable table = new PdfPTable(1);

PdfPCell cell = new PdfPCell();
cell.addElement(Image.getInstance("lion.jpeg"));
cell.addElement(Image.getInstance("tiger.jpg"));
cell.addElement(Image.getInstance("rabbit.jpeg"));

table.addCell(cell);

Following is the complete working application.
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(1);

  PdfPCell cell = new PdfPCell();
  cell.addElement(Image.getInstance("lion.jpeg"));
  cell.addElement(Image.getInstance("tiger.jpg"));
  cell.addElement(Image.getInstance("rabbit.jpeg"));

  table.addCell(cell);

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

Itext: Add hyper links to table

In this post, I am going to show you how to add hyper links to cells in a table.

Following snippet add hyper links to a table.

private static void addHyperLink(PdfPTable table, String title, String link) {
         Phrase phrase = new Phrase();
         Chunk chunk = new Chunk(title);
         chunk.setAnchor(link);
         phrase.add(chunk);
         table.addCell(phrase);
}


Following is the complete working application.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 private static Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN, 15, Font.BOLD, BaseColor.RED);

 private static void addHyperLink(PdfPTable table, String title, String link) {
  Phrase phrase = new Phrase();
  Chunk chunk = new Chunk(title);
  chunk.setAnchor(link);
  phrase.add(chunk);
  table.addCell(phrase);
 }

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  /* Create parent directories to destination pdf file */
  File file = new File(fileName);
  file.getParentFile().mkdirs();

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(1);
  table.addCell(new Phrase("My Tutorials", font));

  addHyperLink(table, "Java Tutorial", "https://self-learning-java-tutorial.blogspot.com");
  addHyperLink(table, "MongoDB Tutorial",
    "https://self-learning-java-tutorial.blogspot.com/2016/05/mongodb-home.html");
  addHyperLink(table, "R Tutorial for beginners",
    "https://self-learning-java-tutorial.blogspot.com/2015/06/r-tutorial-for-beginners.html");

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

Tuesday, 1 November 2016

Itext: Adding background image to a cell in table

PdfPCell class provides setImage method to set an image in table cell.

public void setImage(Image image)

Following application adds images to table cells.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  /* Create parent directories to destination pdf file */
  File file = new File(fileName);
  file.getParentFile().mkdirs();

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(2);
  PdfPCell cell = new PdfPCell();

  Image image = Image.getInstance("lion.jpeg");
  cell.setImage(image);
  table.addCell(cell);

  image = Image.getInstance("tiger.jpg");
  cell.setImage(image);
  table.addCell(cell);

  image = Image.getInstance("rabbit.jpeg");
  cell.setImage(image);
  table.addCell(cell);

  image = Image.getInstance("lion.jpeg");
  cell.setImage(image);
  table.addCell(cell);

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

Saturday, 29 October 2016

Itext: Set background color to table

PdfPCell class provides setBackgroundColor method, by using this you can change the background color of a table.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";
 private static Font tableHeaderFont = FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD, BaseColor.WHITE);

 public static void main(String args[]) throws FileNotFoundException, DocumentException {

  /* Create parent directories to destination pdf file */
  File file = new File(fileName);
  file.getParentFile().mkdirs();

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(2);
  PdfPCell cell = new PdfPCell(new Phrase("number", tableHeaderFont));
  cell.setBackgroundColor(BaseColor.BLACK);
  table.addCell(cell);
  
  cell.setPhrase(new Phrase("square", tableHeaderFont));
  table.addCell(cell);
  
  cell.setBackgroundColor(BaseColor.GRAY);
  for (int i = 1; i < 11; i++) {
   cell.setPhrase(new Phrase(Integer.toString(i)));
   table.addCell(cell);

   cell.setPhrase(new Phrase(Integer.toString(i * i)));
   table.addCell(cell);
  }
  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home