Java Tutorial/PDF/Layer

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

Create PdfLayer

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 MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer layer1 = new PdfLayer("layer 1", writer);
    PdfLayer layer2 = new PdfLayer("layer 2", writer);
    PdfLayer layer3 = new PdfLayer("layer 3", writer);
    PdfLayerMembership cat = new PdfLayerMembership(writer);
    cat.addMember(layer2);
    cat.addMember(layer3);
    PdfLayerMembership no_cat = new PdfLayerMembership(writer);
    no_cat.addMember(layer2);
    no_cat.addMember(layer3);
    no_cat.setVisibilityPolicy(PdfLayerMembership.ALLOFF);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("A"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("B"), 50, 750, 0);
    cb.endLayer();
    cb.beginLayer(layer3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("C"), 50, 725, 0);
    cb.endLayer();
    cb.beginLayer(cat);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("D"), 50, 700, 0);
    cb.endLayer();
    cb.beginLayer(no_cat);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("E"), 50, 700, 0);
    cb.endLayer();
    document.close();
  }
}





Grouped layers

import java.io.FileOutputStream;
import java.util.ArrayList;
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.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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
    PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
    PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
    group.addChild(layer1);
    group.addChild(layer2);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700,
        0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675,
        0);
    cb.endLayer();
    
    document.close();
  }
}





Nested Layers

import java.io.FileOutputStream;
import java.util.ArrayList;
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.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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer nested = new PdfLayer("Nested Layers", writer);
    PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer);
    PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer);
    nested.addChild(nested_1);
    nested.addChild(nested_2);
    cb.beginLayer(nested);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(nested_1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0);
    cb.endLayer();
    cb.beginLayer(nested_2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0);
    cb.endLayer();
    
    document.close();
  }
}





not printed layer

import java.io.FileOutputStream;
import java.util.ArrayList;
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.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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer not_printed = new PdfLayer("not printed", writer);
    not_printed.setOnPanel(false);
    not_printed.setPrint("Print", false);
    cb.beginLayer(not_printed);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700,
        90);
    cb.endLayer();
    
    document.close();
  }
}





Radio Group

import java.io.FileOutputStream;
import java.util.ArrayList;
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.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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer);
    PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
    radio1.setOn(true);
    PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
    radio2.setOn(false);
    PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
    radio3.setOn(false);
    radiogroup.addChild(radio1);
    radiogroup.addChild(radio2);
    radiogroup.addChild(radio3);
    ArrayList options = new ArrayList();
    options.add(radio1);
    options.add(radio2);
    options.add(radio3);
    writer.addOCGRadioGroup(options);
    cb.beginLayer(radio1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0);
    cb.endLayer();
    cb.beginLayer(radio2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0);
    cb.endLayer();
    cb.beginLayer(radio3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0);
    cb.endLayer();
    
    document.close();
  }
}





Show hide text with layer

import java.io.FileOutputStream;
import java.util.ArrayList;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfLayer;
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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfLayer a1 = new PdfLayer("answer 1", writer);
    a1.setOn(false);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte cb = writer.getDirectContent();
    cb.beginText();
    cb.setTextMatrix(50, 790);
    cb.setLeading(24);
    cb.setFontAndSize(bf, 18);
    cb.newlineShowText("This is a question?");
    cb.beginLayer(a1);
    cb.setRGBColorFill(0xFF, 0x00, 0x00);
    cb.newlineShowText("This is the answer");
    cb.resetRGBColorFill();
    cb.endLayer();
    cb.endText();
    ArrayList stateOn = new ArrayList();
    stateOn.add("ON");
    stateOn.add(a1);
    PdfAction actionOn = PdfAction.setOCGstate(stateOn, true);
    ArrayList stateOff = new ArrayList();
    stateOff.add("OFF");
    stateOff.add(a1);
    PdfAction actionOff = PdfAction.setOCGstate(stateOff, true);
    ArrayList stateToggle = new ArrayList();
    stateToggle.add("Toggle");
    stateToggle.add(a1);
    PdfAction actionToggle = PdfAction.setOCGstate(stateToggle, true);
    Phrase p = new Phrase("Change the state of the answers:");
    Chunk on = new Chunk(" on ").setAction(actionOn);
    p.add(on);
    Chunk off = new Chunk("/ off ").setAction(actionOff);
    p.add(off);
    Chunk toggle = new Chunk("/ toggle").setAction(actionToggle);
    p.add(toggle);
    document.add(p);
    document.close();
  }
}





Text on layer

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfLayer;
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"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfLayer layer = new PdfLayer("Do you see me?", writer);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte cb = writer.getDirectContent();
    cb.beginText();
    cb.setTextMatrix(50, 790);
    cb.setLeading(24);
    cb.setFontAndSize(bf, 18);
    cb.showText("Do you see me?");
    cb.beginLayer(layer);
    cb.newlineShowText("this is a test");
    cb.endLayer();
    cb.endText();
    document.close();
  }
}





Using Layer

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfContentByte;
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();
    PdfContentByte over = writer.getDirectContent();
    PdfContentByte under = writer.getDirectContentUnder();
    drawLayer(over, 70, 750, 150, 100);
    drawLayer(under, 70, 730, 150, 100);
    document.close();
  }
  public static void drawLayer(PdfContentByte cb, float llx, float lly, float w, float h) {
    cb.saveState();
    cb.setColorFill(new GrayColor(0.9f));
    cb.setColorStroke(new GrayColor(0.2f));
    cb.moveTo(llx, lly);
    cb.lineTo(llx + w / 4, lly + h / 3);
    cb.lineTo(llx + 7 * w / 8, lly + h / 3);
    cb.lineTo(llx + (2 * w) / 3, lly);
    cb.closePathFillStroke();
    cb.restoreState();
  }
}