Java Code Examples for com.lowagie.text.pdf.PdfWriter#setPdfVersion()

The following examples show how to use com.lowagie.text.pdf.PdfWriter#setPdfVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptionalContentTest.java    From itext2 with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * Demonstrates the use of layers.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("optionalcontent.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3: opening the document
	document.open();
	// step 4: content
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase(
			"Automatic layers, form fields, images, templates and actions",
			new Font(Font.HELVETICA, 18, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
	PdfLayerMembership m1 = new PdfLayerMembership(writer);
	m1.addMember(l2);
	m1.addMember(l3);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2 or layer 3");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f);
	cb.endLayer();
	cb.beginLayer(m1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620),
			"field1");
	ff.setBorderColor(Color.blue);
	ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
	ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
	ff.setText("I'm a form field");
	PdfFormField form = ff.getTextField();
	form.setLayer(l4);
	writer.addAnnotation(form);
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
			+ "pngnow.png");
	img.setLayer(l4);
	img.setAbsolutePosition(200, 550);
	cb.addImage(img);
	PdfTemplate tp = cb.createTemplate(100, 20);
	Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12,
			Font.NORMAL, Color.magenta));
	ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0);
	tp.setLayer(l4);
	tp.setBoundingBox(new Rectangle(0, -10, 100, 20));
	cb.addTemplate(tp, 200, 500);
	ArrayList<Object> state = new ArrayList<Object>();
	state.add("toggle");
	state.add(l1);
	state.add(l2);
	state.add(l3);
	state.add(l4);
	PdfAction action = PdfAction.setOCGstate(state, true);
	Chunk ck = new Chunk("Click here to toggle the layers", new Font(
			Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground(
			Color.blue).setAction(action);
	ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck),
			250, 400, 0);
	cb.sanityCheck();

	// step 5: closing the document
	document.close();
}
 
Example 2
Source File: PdfVersionTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a PDF document and shows the PDF version.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pdfversion.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_2);
	// step 3: we open the document
	document.open();

	// step 4:
	document.add(new Paragraph("This is a PDF-1.2 document"));

	// step 5: we close the document
	document.close();
}
 
Example 3
Source File: OrderedLayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates how to order optional content groups.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("orderedlayers.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase("Ordered layers", new Font(
			Font.HELVETICA, 20, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	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(l2);
	m1.addMember(l3);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2 or layer 3");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
	cb.endLayer();
	cb.beginLayer(m1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	cb.sanityCheck();

	PdfOCProperties p = writer.getOCProperties();
	PdfArray order = new PdfArray();
	order.add(l1.getRef());
	order.add(l2.getRef());
	order.add(l3.getRef());
	PdfDictionary d = new PdfDictionary();
	d.put(PdfName.ORDER, order);
	p.put(PdfName.D, d);
	// step 5
	document.close();
}
 
Example 4
Source File: LayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Layer radio group and zoom.
    */
@Test
public void main() throws Exception {
       	// step 1
           Document document = new Document(PageSize.A4, 50, 50, 50, 50);
           // step 2
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "layers.pdf"));
           writer.setPdfVersion(PdfWriter.VERSION_1_5);
           writer.setViewerPreferences(PdfWriter.PageModeUseOC);
           // step 3
           document.open();
           // step 4
           PdfContentByte cb = writer.getDirectContent();
           Phrase explanation = new Phrase("Layer radio group and zoom", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0);
           PdfLayer title = PdfLayer.createTitle("Layer radio group", writer);
           PdfLayer l1 = new PdfLayer("Layer 1", writer);
           PdfLayer l2 = new PdfLayer("Layer 2", writer);
           PdfLayer l3 = new PdfLayer("Layer 3", writer);
           PdfLayer l4 = new PdfLayer("Layer 4", writer);
           title.addChild(l1);
           title.addChild(l2);
           title.addChild(l3);
           l4.setZoom(2, -1);
           l4.setOnPanel(false);
           l4.setPrint("Print", true);
           l2.setOn(false);
           l3.setOn(false);
           ArrayList<PdfLayer> radio = new ArrayList<PdfLayer>();
           radio.add(l1);
           radio.add(l2);
           radio.add(l3);
           writer.addOCGRadioGroup(radio);
           Phrase p1 = new Phrase("Text in layer 1");
           Phrase p2 = new Phrase("Text in layer 2");
           Phrase p3 = new Phrase("Text in layer 3");
           Phrase p4 = new Phrase("Text in layer 4");
           cb.beginLayer(l1);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
           cb.endLayer();
           cb.beginLayer(l2);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
           cb.endLayer();
           cb.beginLayer(l3);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
           cb.endLayer();
           cb.beginLayer(l4);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p4, 50, 450, 0);
           cb.endLayer();
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("<< Zoom here!", new Font(Font.COURIER, 12, Font.NORMAL, Color.blue)), 150, 450, 0);
           // step 5
           document.close();

   }
 
Example 5
Source File: AutomaticTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Automatic grouping and nesting.
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("automatic.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase("Automatic layer grouping and nesting",
			new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l12 = new PdfLayer("Layer nesting", writer);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer title = PdfLayer.createTitle("Layer grouping", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	PdfLayer l4 = new PdfLayer("Layer 4", writer);
	l12.addChild(l1);
	l12.addChild(l2);
	title.addChild(l3);
	title.addChild(l4);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2");
	Phrase p3 = new Phrase("Text in layer 3");
	Phrase p4 = new Phrase("Text in layer 4");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
	cb.endLayer();
	cb.beginLayer(l2);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	cb.beginLayer(l4);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p4, 50, 450, 0);
	cb.endLayer();
	cb.sanityCheck();

	// step 5
	document.close();
}
 
Example 6
Source File: NestedLayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates the use of nested layers
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("nestedlayers.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase("Layer nesting", new Font(
			Font.HELVETICA, 20, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l23 = new PdfLayer("Top Layer 2 3", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
	cb.endLayer();
	cb.beginLayer(l23);
	cb.beginLayer(l2);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	cb.endLayer();
	cb.sanityCheck();

	PdfOCProperties p = writer.getOCProperties();
	PdfArray order = new PdfArray();
	order.add(l1.getRef());
	order.add(l23.getRef());
	PdfArray group = new PdfArray();
	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);
	// step 5
	document.close();
}
 
Example 7
Source File: ContentGroupsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Demonstrates how to group optional content.
    */
@Test
public void main() throws Exception {
       	// step 1
           Document document = new Document(PageSize.A4, 50, 50, 50, 50);
           // step 2
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "contentgroups.pdf"));
           writer.setPdfVersion(PdfWriter.VERSION_1_5);
           writer.setViewerPreferences(PdfWriter.PageModeUseOC);
           // step 3
           document.open();
           // step 4
           PdfContentByte cb = writer.getDirectContent();
           Phrase explanation = new Phrase("Layer grouping", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0);
           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(l2);
           m1.addMember(l3);
           Phrase p1 = new Phrase("Text in layer 1");
           Phrase p2 = new Phrase("Text in layer 2 or layer 3");
           Phrase p3 = new Phrase("Text in layer 3");
           cb.beginLayer(l1);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
           cb.endLayer();
           cb.beginLayer(m1);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
           cb.endLayer();
           cb.beginLayer(l3);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
           cb.endLayer();
           cb.sanityCheck();
           
           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);
           // step 5
           document.close();
   }
 
Example 8
Source File: AnnotationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a document with some PdfAnnotations.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Annotations.pdf"));
	// step 3:
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	document.open();
	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	// page 1
	PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, PdfTestBase.RESOURCES_DIR + "cards.mpg");
	writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs,
			"video/mpeg", true));
	PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript(
			"app.alert('Hello');\r", writer));
	document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top"));
	writer.addAnnotation(a);
	writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
			"This is some text", "some text".getBytes(), null, "some.txt"));
	writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help",
			"This Help annotation was made with 'createText'", false, "Help"));
	writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help",
			"This Comment annotation was made with 'createText'", true, "Comment"));
	cb.rectangle(200, 700, 100, 100);
	cb.rectangle(200, 550, 100, 100);
	cb.rectangle(200, 400, 100, 100);
	cb.rectangle(200, 250, 100, 100);
	cb.stroke();
	document.newPage();
	// page 2
	writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f),
			PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer)));
	writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f),
			PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
	writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f),
			PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
	writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f),
			"This Comment annotation was made with 'createSquareCircle'", false));
	document.newPage();
	// page 3
	PdfContentByte pcb = new PdfContentByte(writer);
	pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
	writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f),
			"This is some free text, blah blah blah", pcb));
	writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line",
			200, 550, 300, 650));
	writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f),
			"This is a stamp", "Stamp"));
	writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f),
			"Hello, I'm a popup!", true));
	cb.rectangle(200, 700, 100, 100);
	cb.rectangle(200, 550, 100, 100);
	cb.rectangle(200, 250, 100, 100);
	cb.stroke();

	// step 5: we close the document
	document.close();
}
 
Example 9
Source File: SimpleAnnotationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates documents with some simple annotations.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
	Document document2 = new Document(PageSize.A4, 10, 10, 10, 10);

	// step 2:
	PdfWriter writer1 = PdfWriter.getInstance(document1, PdfTestBase.getOutputStream("SimpleAnnotations1.pdf"));
	PdfWriter writer2 = PdfWriter.getInstance(document2, PdfTestBase.getOutputStream("SimpleAnnotations2.pdf"));
	// step 3:
	writer2.setPdfVersion(PdfWriter.VERSION_1_5);
	document1.open();
	document2.open();
	// step 4:
	document1.add(new Paragraph("Each square on this page represents an annotation."));
	// document1
	PdfContentByte cb1 = writer1.getDirectContent();
	Annotation a1 = new Annotation("authors",
			"Maybe it's because I wanted to be an author myself that I wrote iText.", 250f, 700f, 350f, 800f);
	document1.add(a1);
	Annotation a2 = new Annotation(250f, 550f, 350f, 650f, new URL("http://www.lowagie.com/iText/"));
	document1.add(a2);
	Annotation a3 = new Annotation(250f, 400f, 350f, 500f, "http://www.lowagie.com/iText");
	document1.add(a3);
	Image image = Image.getInstance(PdfTestBase.RESOURCES_DIR + "iText.gif");
	image.setAnnotation(a3);
	document1.add(image);
	Annotation a4 = new Annotation(250f, 250f, 350f, 350f, PdfAction.LASTPAGE);
	document1.add(a4);
	// draw rectangles to show where the annotations were added
	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();
	// more content
	document1.newPage();
	for (int i = 0; i < 5; i++) {
		document1.add(new Paragraph("blahblahblah"));
	}
	document1.add(new Annotation("blahblah", "Adding an annotation without specifying coordinates"));
	for (int i = 0; i < 3; i++) {
		document1.add(new Paragraph("blahblahblah"));
	}
	document1.newPage();
	document1.add(new Chunk("marked chunk").setLocalDestination("mark"));
	
	File videoFile = new File(PdfTestBase.RESOURCES_DIR + "cards.mpg");
	File license = new File("LICENSE");

	// document2
	document2.add(new Paragraph("Each square on this page represents an annotation."));
	PdfContentByte cb2 = writer2.getDirectContent();
	Annotation a5 = new Annotation(100f, 700f, 200f, 800f, videoFile.getAbsolutePath(), "video/mpeg", true);
	document2.add(a5);
	Annotation a6 = new Annotation(100f, 550f, 200f, 650f, "SimpleAnnotations1.pdf", "mark");
	document2.add(a6);
	Annotation a7 = new Annotation(100f, 400f, 200f, 500f, "SimpleAnnotations1.pdf", 2);
	document2.add(a7);
	Annotation a8 = new Annotation(100f, 250f, 200f, 350f, license.getAbsolutePath(), null, null, null);
	document2.add(a8);
	// draw rectangles to show where the annotations were added
	cb2.rectangle(100, 700, 100, 100);
	cb2.rectangle(100, 550, 100, 100);
	cb2.rectangle(100, 400, 100, 100);
	cb2.rectangle(100, 250, 100, 100);
	cb2.stroke();

	// step 5: we close the document
	document1.close();
	document2.close();
}
 
Example 10
Source File: LayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates some Layer functionality.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Layers.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	// step 3:
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	document.open();
	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase("Layer grouping", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0);
	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(l2);
	m1.addMember(l3);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2 or layer 3");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
	cb.endLayer();
	cb.beginLayer(m1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	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);

	// step 5: we close the document
	document.close();
}