Sunday 16 October 2016

itext: Anchor object: Specify hyper links

By using Anchor tag, you can specify hyper links. You can specify hyper links that are internal to the document (or) outside the document. In this post, I am going to explain how to specify external and internal hyper links.

Anchor class provides following constructors to define Anchor instance.

public Anchor()
public Anchor(final float leading)
public Anchor(final Chunk chunk)
public Anchor(final String string)
public Anchor(final String string, final Font font)
public Anchor(final float leading, final Chunk chunk)
public Anchor(final float leading, final String string)
public Anchor(final float leading, final String string, final Font font)
public Anchor(final Phrase phrase) 

Adding External hyper links
It is very simple, Define hyper link with some title.
Anchor anchor = new Anchor("Java Tutorial");

After that specify the url for above anchor instance using reference method.
anchor.setReference("https://self-learning-java-tutorial.blogspot.com");

Following is the complete working application.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Anchor;
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.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class TestAnchor {
 private static Font font = FontFactory.getFont(FontFactory.COURIER, 15, Font.NORMAL, BaseColor.GRAY);

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

  Anchor anchor = new Anchor("Java Tutorial", font);
  anchor.setReference("https://self-learning-java-tutorial.blogspot.com");

  Paragraph paragraph = new Paragraph("You Can learn Java Tutorials at ");
  paragraph.add(anchor);

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream("tutorials.pdf"));
  document.open();
  document.add(paragraph);
  document.close();

 }
}

Adding internal hyper links
Some times you want to point to internal hyper links. Following post explains how to add internal hyper links.

It is two-step process.

1. Create target anchor and give name to it.
Anchor anchorTarget =new Anchor("Java is Platform Independent");
anchorTarget.setName("linkTarget");

2. Create another anchor tag and give the reference as the name of anchor tag created in step 1.

Anchor anchor = new Anchor("Java Tutorial", font);
anchor.setReference("#linkTarget");


Following is the complete working application.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Anchor;
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.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class TestAnchor {
 private static Font font = FontFactory.getFont(FontFactory.COURIER, 15, Font.NORMAL, BaseColor.GRAY);

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

  Anchor anchor = new Anchor("Java Tutorial", font);
  anchor.setReference("#linkTarget");

  Paragraph paragraph = new Paragraph("You Can learn Java Tutorials at ");
  paragraph.add(anchor);

  Anchor anchorTarget = new Anchor("Java is Platform Independent");
  anchorTarget.setName("linkTarget");
  Paragraph targetParagraph = new Paragraph();
  targetParagraph.add(anchorTarget);

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream("tutorials.pdf"));
  document.open();

  document.add(paragraph);
  document.add(new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));
  document.add(targetParagraph);

  document.close();

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment