Java/PDF RTF/Layer — различия между версиями

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

Текущая версия на 05:59, 1 июня 2010

Layer Image on Text

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class LayerImageOnTextPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LayerImageOnTextPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph();
      for (int i = 0; i < 100; i++)
        p.add(new Chunk("Text Text Text Text Text Text Text Text "));
      document.add(p);
      Image img = Image.getInstance("logo.png");
      img.setAbsolutePosition(100, 500);
      document.add(img);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Layers Demo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfLayerMembership;
import com.lowagie.text.pdf.PdfWriter;
public class LayersPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LayersPDF.pdf"));
      writer.setPdfVersion(PdfWriter.VERSION_1_5);
      writer.setViewerPreferences(PdfWriter.PageModeUseOC);
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfLayer l1 = new PdfLayer("Layer 1", writer);
      PdfLayer l2 = new PdfLayer("Layer 2", writer);
      PdfLayer l3 = new PdfLayer("Layer 3", writer);
      PdfLayerMembership m1 = new PdfLayerMembership(writer);
      m1.addMember(l1);
      m1.addMember(l2);
      m1.addMember(l3);
    
      cb.beginLayer(l1);
      ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Text in layer 1"), 50, 600, 0);
      cb.endLayer();
      cb.beginLayer(l2);
      ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Text in layer 2"), 50, 550, 0);
      cb.endLayer();
      cb.beginLayer(l3);
      ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Text in layer 3"), 50, 500, 0);
      cb.endLayer();
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Layers Group Demo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfLayerMembership;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfOCProperties;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfWriter;
public class LayersGroupPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LayersGroupPDF.pdf"));
      writer.setPdfVersion(PdfWriter.VERSION_1_5);
      writer.setViewerPreferences(PdfWriter.PageModeUseOC);
      document.open();
      PdfLayer l1 = new PdfLayer("Layer 1", writer);
      PdfLayer l2 = new PdfLayer("Layer 2", writer);
      PdfLayer l3 = new PdfLayer("Layer 3", writer);
      PdfLayerMembership m1 = new PdfLayerMembership(writer);
      m1.addMember(l1);
      m1.addMember(l2);
      m1.addMember(l3);
    
      PdfOCProperties p = writer.getOCProperties();
      PdfArray order = new PdfArray();
      order.add(l1.getRef());
      PdfArray group = new PdfArray();
      group.add(new PdfString("A group of two", PdfObject.TEXT_UNICODE));
      group.add(l2.getRef());
      group.add(l3.getRef());
      order.add(group);
      PdfDictionary d = new PdfDictionary();
      d.put(PdfName.ORDER, order);
      p.put(PdfName.D, d);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Layer Shape on Text

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class LayerShapeOnTextPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("LayerShapeOnTextPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph();
      for (int i = 0; i < 100; i++)
        p.add(new Chunk("Text Text Text Text Text Text Text Text "));
      document.add(p);
      PdfContentByte cb = writer.getDirectContent();
      cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
      cb.circle(250.0f, 500.0f, 50.0f);
      cb.fill();
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}