Java Tutorial/PDF/Annotation

Материал из Java эксперт
Версия от 05:21, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Annotation

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleBookmark;
public class MainClass {
  public static void main(String[] args) throws Exception {
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfReader reader = new PdfReader("test.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my.pdf"));
    stamper.insertPage(1, PageSize.A4);
    PdfContentByte cb = stamper.getOverContent(1);
    cb.beginText();
    cb.setFontAndSize(bf, 18);
    cb.setTextMatrix(36, 770);
    cb.showText("Inserted Title Page");
    cb.endText();
    stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(30f, 750f,
        80f, 800f), "inserted page", "This page is the title page.", true, null), 1);
    stamper.close();
  }
}





Annotated Images

import java.io.FileOutputStream;
import com.lowagie.text.Annotation;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image gif = Image.getInstance("Logo.gif");
    gif.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.jexp.ru"));
    gif.setAbsolutePosition(30f, 750f);
    document.add(gif);
    Image jpeg = Image.getInstance("dog.jpg");
    jpeg.setAnnotation(new Annotation("picture", "dog", 0, 0,
        0, 0));
    jpeg.setAbsolutePosition(120f, 550f);
    document.add(jpeg);
    document.close();
  }
}





PdfAnnotation and Javascript

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter
        .getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfAnnotation text = PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f),
        "Fox", "The fox is quick", true, "Comment");
    PdfAnnotation javascript = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction
        .javaScript("app.alert("hi");\r", writer));
    Chunk fox = new Chunk("test").setAnnotation(text);
    Chunk dog = new Chunk("javascript").setAnnotation(javascript);
    document.add(fox);
    document.add(dog);
    document.close();
  }
}





Text Annotations

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter
        .getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(50, 780, 70, 800),
        "Comment", "This is a Comment", false, "Comment"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(100, 780, 120, 800),
        "Help", "This is a Help ", true, "Help"));
    document.close();
  }
}