Java/PDF RTF/Annotations

Материал из Java эксперт
Перейти к: навигация, поиск

Simple Annotations

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsPDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsPDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a1 = new Annotation("authors", "Text for an annotation", 250f, 700f, 350f, 800f);
      document1.add(a1);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations: Add Annotation without Specifying Coordinates

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsAddAnnotationWithoutSpecifyingCoordinates {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsAddAnnotationWithoutSpecifyingCoordinates.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      document1.add(new Annotation("blahblah", "Adding an annotation without specifying coordinates"));
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations: Draw Rectangle to Show Annotations

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsDrawRectangleToShowAnnotation {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsDrawRectangleToShowAnnotation.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation("authors", "Text for an annotation",250f, 250f, 350f, 350f);
      document1.add(a);
      // draw rectangles to show where the annotations were added
      PdfContentByte cb1 = writer1.getDirectContent();
      cb1.rectangle(250, 700, 100, 100);
      cb1.rectangle(250, 550, 100, 100);
      cb1.rectangle(250, 400, 100, 100);
      cb1.rectangle(250, 250, 100, 100);
      cb1.stroke();
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations: Local Destination

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsLocalDestinationPDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsLocalDestinationPDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      
      
      Annotation a = new Annotation("SimpleAnnotationsLocalDestination.pdf", "mark");
      document1.add(a);
      
      document1.newPage();
      document1.newPage();
      document1.add(new Chunk("marked chunk").setLocalDestination("mark"));
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations with Another PDF document

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsWithAnotherPDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsWithAnotherPDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation(100f, 400f, 200f, 500f, "SimpleAnnotations1.pdf", 2);
      document1.add(a);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations with Application(Notepad.exe)

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsWithApplicationPDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsWithApplicationPDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation( 100f, 250f, 200f, 350f, "C://windows/notepad.exe", null, null, null);
      document1.add(a);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations with HyperLink

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsWithHyperLinkPDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsWithHyperLinkPDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation(250f, 550f, 350f, 650f,
          new URL("http://www.jexp.ru"));
      document1.add(a);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations with HyperLink In an Image

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsWithHyperLinkInAnImagePDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsWithHyperLinkInAnImagePDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation(250f, 400f, 350f, 500f, "http://www.jexp.ru");
      document1.add(a);
      Image image = Image.getInstance("logo.png");
      image.setAnnotation(a);
      document1.add(image);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}





Simple Annotations with MpegMovie

import java.io.FileOutputStream;
import java.net.URL;
import com.lowagie.text.Annotation;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class SimpleAnnotationsWithMpegMoviePDF {
  public static void main(String[] args) {
    Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
    try {
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream("SimpleAnnotationsWithMpegMoviePDF.pdf"));
      writer1.setPdfVersion(PdfWriter.VERSION_1_5);
      document1.open();
      Annotation a = new Annotation(100f, 700f, 200f, 800f, "cards.mpg", "video/mpeg", true);
      document1.add(a);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document1.close();
  }
}