2014年10月25日 星期六

itext-anchor


http://tutorials.jenkov.com/java-itext/anchor.html
-

T com.itextpdf.text.Anchor class in IText represents an link, either to an external website, or internally in the document. The anchor (link) can be clicked just like a link in a web page.
Here is a simple code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class AnchorExample {

  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Anchor.pdf"));

      document.open();

      Paragraph paragraph = new Paragraph();
      paragraph.add(new Phrase("You can find the IText tutorial at "));

      
          Anchor anchor = new Anchor(
          "http://tutorials.jenkov.com/java-itext/index.html");
          anchor.setReference(
          "http://tutorials.jenkov.com/java-itext/index.html");
      
      paragraph.add(anchor);

      document.add(paragraph);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }
}
Here is what the generated document looks like:
An IText Anchor example
An IText Anchor example
Notice how the mouse pointer is shaped as a hand. This means that you can now click the text. Also notice that there is no special style associated with the anchor by default. You will have to add this yourself.

Internal Links

You can create internal links in your documents too, just like internal links in an HTML page. Just like in HTML, you need both a link and a target anchor (an anchor with a name). Here is a code example:
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**

 */
public class Anchor2Example {

  public static void main(String[] args) {

    Document document = new Document();

    try {
        PdfWriter.getInstance(document,
                new FileOutputStream("Anchor2.pdf"));

        document.open();

        
            Anchor anchor =
            new Anchor("Jump down to next paragraph");
            anchor.setReference("#linkTarget");
            Paragraph paragraph = new Paragraph();
            paragraph.add(anchor);
            document.add(paragraph);

            Anchor anchorTarget =
            new Anchor("This is the target of the link above");
            anchor.setName("linkTarget");
            Paragraph targetParagraph = new Paragraph();
            targetParagraph.setSpacingBefore(50);

            targetParagraph.add(anchorTarget);
            document.add(targetParagraph);
        

        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

  }
}
Here is the resulting document. Notice how the mouse pointer is again shaped as a hand. Since the target of the link is located just below on the same page, the Adobe Reader would probably not react when the link is clicked. But, if the target paragraph was located on a different page, the Adobe Reader would jump to that page.
An IText internal Anchor example
An IText internal Anchor example

沒有留言:

張貼留言