Java Code Examples for com.lowagie.text.pdf.PdfContentByte#sanityCheck()

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#sanityCheck() . 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: ShadingPatternTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Shading example.
 */
@Test
public void main() throws Exception {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("shading_pattern.pdf"));
	document.open();

	PdfShading shading = PdfShading.simpleAxial(writer, 100, 100, 400, 100,
			Color.red, Color.cyan);
	PdfShadingPattern shadingPattern = new PdfShadingPattern(shading);
	PdfContentByte cb = writer.getDirectContent();
	BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD,
			BaseFont.WINANSI, false);
	cb.setShadingFill(shadingPattern);
	cb.beginText();
	cb.setTextMatrix(100, 100);
	cb.setFontAndSize(bf, 40);
	cb.showText("Look at this text!");
	cb.endText();
	PdfShading shadingR = PdfShading.simpleRadial(writer, 200, 500, 50,
			300, 500, 100, new Color(255, 247, 148), new Color(247, 138,
					107), false, false);
	cb.paintShading(shadingR);
	cb.sanityCheck();
	document.close();

}
 
Example 3
Source File: JFreeChartTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
  * Converts a JFreeChart to PDF syntax.
  * @param filename	the name of the PDF file
  * @param chart		the JFreeChart
  * @param width		the width of the resulting PDF
  * @param height	the height of the resulting PDF
  */
 public static void convertToPdf(JFreeChart chart, int width, int height, String filename) {
 	// step 1
 	Document document = new Document(new Rectangle(width, height));
 	try {
 		// step 2
 		PdfWriter writer;
writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream(filename));
// step 3
 		document.open();
 		// step 4
 		PdfContentByte cb = writer.getDirectContent();
 		PdfTemplate tp = cb.createTemplate(width, height);
 		Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
 		Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
 		chart.draw(g2d, r2d);
 		g2d.dispose();
 		tp.sanityCheck();
 		cb.addTemplate(tp, 0, 0);
 		cb.sanityCheck();
 	}
 	catch(DocumentException de) {
 		de.printStackTrace();
 	}
 	catch (FileNotFoundException e) {
 		e.printStackTrace();
 	}
 	// step 5
 	document.close();
 }
 
Example 4
Source File: ShadingTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void main() throws Exception {

	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream( "shading.pdf"));
	document.open();

	PdfFunction function1 = PdfFunction.type2(writer, new float[] { 0, 1 },
			null, new float[] { .929f, .357f, 1, .298f }, new float[] {
					.631f, .278f, 1, .027f }, 1.048f);
	PdfFunction function2 = PdfFunction.type2(writer, new float[] { 0, 1 },
			null, new float[] { .929f, .357f, 1, .298f }, new float[] {
					.941f, .4f, 1, .102f }, 1.374f);
	PdfFunction function3 = PdfFunction.type3(writer, new float[] { 0, 1 },
			null, new PdfFunction[] { function1, function2 },
			new float[] { .708f }, new float[] { 1, 0, 0, 1 });
	PdfShading shading = PdfShading.type3(writer,
			new CMYKColor(0, 0, 0, 0),
			new float[] { 0, 0, .096f, 0, 0, 1 }, null, function3,
			new boolean[] { true, true });
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(316.789f, 140.311f);
	cb.curveTo(303.222f, 146.388f, 282.966f, 136.518f, 279.122f, 121.983f);
	cb.lineTo(277.322f, 120.182f);
	cb.curveTo(285.125f, 122.688f, 291.441f, 121.716f, 298.156f, 119.386f);
	cb.lineTo(336.448f, 119.386f);
	cb.curveTo(331.072f, 128.643f, 323.346f, 137.376f, 316.789f, 140.311f);
	cb.clip();
	cb.newPath();
	cb.saveState();
	cb.concatCTM(27.7843f, 0, 0, -27.7843f, 310.2461f, 121.1521f);
	cb.paintShading(shading);
	cb.restoreState();

	cb.sanityCheck();

	document.close();
}
 
Example 5
Source File: SoftMaskTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates the Transparency functionality.
 */
@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 a writer
	PdfWriter writer = PdfWriter.getInstance(document,PdfTestBase.getOutputStream("softmask.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: content
	PdfContentByte cb = writer.getDirectContent();
	String text = "text ";
	text += text;
	text += text;
	text += text;
	text += text;
	text += text;
	text += text;
	text += text;
	text += text;
	document.add(new Paragraph(text));
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR	+ "otsoe.jpg");
	img.setAbsolutePosition(100, 550);
	byte gradient[] = new byte[256];
	for (int k = 0; k < 256; ++k) {
		gradient[k] = (byte) k;
	}
	Image smask = Image.getInstance(256, 1, 1, 8, gradient);
	smask.makeMask();
	img.setImageMask(smask);
	cb.addImage(img);
	cb.sanityCheck();
	// step 5: we close the document
	document.close();
}
 
Example 6
Source File: TransformImageTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
    * Add an image using different transformation matrices.
    */
@Test
public void main() throws Exception {
       Document.compress = false;
       // step 1: creation of a document-object
       Document document = new Document(PageSize.A4);
       
       try {
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "transformimage.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png");
           cb.addImage(img, 271, -50, -30, 550, 100, 100);
           cb.sanityCheck();
  
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 7
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 8
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 9
Source File: PageNumbersWatermarkTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // for odd pagenumbers, show the footer at the left
    if ((writer.getPageNumber() & 1) == 1) {
        cb.setTextMatrix(document.left(), textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.left() + textSize, textBase);
    }
    // for even numbers, show the footer at the right
    else {
        float adjust = helv.getWidthPoint("0", 12);
        cb.setTextMatrix(document.right() - textSize - adjust, textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.right() - adjust, textBase);
    }

    // draw a Rectangle around the page
    cb.setColorStroke(Color.orange);
    cb.setLineWidth(2);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();

    // starting on page 3, a watermark with an Image that is made transparent
    if (writer.getPageNumber() >= 3) {

        cb.setGState(gstate);
        cb.setColorFill(Color.red);
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
        cb.endText();
        try {
            cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80);
        }
        catch(Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    cb.restoreState();
    cb.sanityCheck();
}
 
Example 10
Source File: LogoTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Draws the iText logo.
 */
 @Test
public void main() throws Exception {

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

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream("logo.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	PdfContentByte cb = writer.getDirectContent();
	PdfTemplate template = cb.createTemplate(500, 200);
	template.setLineWidth(2f);
	template.rectangle(2.5f, 2.5f, 495f, 195f);
	template.stroke();
	template.setLineWidth(12f);
	template.arc(40f - (float) Math.sqrt(12800),
			120f + (float) Math.sqrt(12800),
			200f - (float) Math.sqrt(12800),
			-40f + (float) Math.sqrt(12800), 281.25f, 33.75f);
	template.arc(40f, 120f, 200f, -40f, 90f, 45f);
	template.stroke();
	template.setLineCap(1);
	template.setLineWidth(12f);
	template.arc(80f, 40f, 160f, 120f, 90f, 180f);
	template.arc(115f, 75f, 125f, 85f, 0f, 360f);
	template.stroke();
	template.beginText();
	template.setFontAndSize(bf, 180);
	template.setRGBColorFill(0xFF, 0x00, 0x00);
	template.showTextAligned(PdfContentByte.ALIGN_LEFT, "T", 125f, 35f, 0f);
	template.resetRGBColorFill();
	template.showTextAligned(PdfContentByte.ALIGN_LEFT, "ext", 220f, 35f,
			0f);
	template.endText();
	template.sanityCheck();

	cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
	cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
	cb.addTemplate(template, 0.25f, 0, 0, 0.25f, 100, 100);
	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 11
Source File: TextTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding text at absolute positions.
 */
@Test
public void main() throws Exception {

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

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("text.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	// first we draw some lines to be able to visualize the text alignment
	// functions
	cb.setLineWidth(0f);
	cb.moveTo(250, 500);
	cb.lineTo(250, 800);
	cb.moveTo(50, 700);
	cb.lineTo(400, 700);
	cb.moveTo(50, 650);
	cb.lineTo(400, 650);
	cb.moveTo(50, 600);
	cb.lineTo(400, 600);
	cb.stroke();

	// we tell the ContentByte we're ready to draw text
	cb.beginText();

	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	cb.setFontAndSize(bf, 12);
	String text = "Sample text for alignment";
	// we show some text starting on some absolute position with a given
	// alignment
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250,
			700, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250,
			650, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600,
			0);

	// we draw some text on a certain position
	cb.setTextMatrix(100, 400);
	cb.showText("Text at position 100,400.");

	// we draw some rotated text on a certain position
	cb.setTextMatrix(0, 1, -1, 0, 100, 300);
	cb.showText("Text at position 100,300, rotated 90 degrees.");

	// we draw some mirrored, rotated text on a certain position
	cb.setTextMatrix(0, 1, 1, 0, 200, 200);
	cb.showText("Text at position 200,200, mirrored and rotated 90 degrees.");

	// we tell the contentByte, we've finished drawing text
	cb.endText();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 12
Source File: LayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Draws different things into different layers.
 */
@Test
public void main() throws Exception {


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

	
	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream("layers.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:

	// high level
	Paragraph p = new Paragraph();
	for (int i = 0; i < 100; i++)
		p.add(new Chunk("Blah blah blah blah blah. "));
	document.add(p);
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
			+ "hitchcock.png");
	img.setAbsolutePosition(100, 500);
	document.add(img);

	// low level
	PdfContentByte cb = writer.getDirectContent();
	PdfContentByte cbu = writer.getDirectContentUnder();
	cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
	cb.circle(250.0f, 500.0f, 50.0f);
	cb.fill();
	cb.sanityCheck();

	cbu.setRGBColorFill(0xFF, 0x00, 0x00);
	cbu.circle(250.0f, 500.0f, 100.0f);
	cbu.fill();
	cbu.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 13
Source File: SpotColorsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Demonstrates the use of spotcolors.
    */
@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("spotcolor.pdf"));
           BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
           
           // step 3: we open the document
           document.open();
           
           // step 4: we grab the ContentByte and do some stuff with it
           PdfContentByte cb = writer.getDirectContent();
           
           // step 5: we instantiate PdfSpotColor
           
           // Note: I made up these names unless someone give me a PANTONE swatch as gift ([email protected])
           PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", new CMYKColor(0.9f, .2f, .3f, .1f));
           PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", new Color(114, 94, 38));
           PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", new GrayColor(0.9f));
           
           // Stroke a rectangle with CMYK alternate
           cb.setColorStroke(spc_cmyk, .5f);
           cb.setLineWidth(10f);
           // draw a rectangle
           cb.rectangle(100, 700, 100, 100);
           // add the diagonal
           cb.moveTo(100, 700);
           cb.lineTo(200, 800);
           // stroke the lines
           cb.stroke();
           
           // Fill a rectangle with CMYK alternate
           cb.setColorFill(spc_cmyk, 0.25f);
           cb.rectangle(250, 700, 100, 100);
           cb.fill();
           
           // Stroke a circle with RGB alternate
           cb.setColorStroke(spc_rgb, 0.9f);
           cb.setLineWidth(5f);
           cb.circle(150f, 500f, 100f);
           cb.stroke();
           
           // Fill the circle with RGB alternate
           cb.setColorFill(spc_rgb, 0.9f);
           cb.circle(150f, 500f, 50f);
           cb.fill();
           
           // example with colorfill
           cb.setColorFill(spc_g, 0.5f);
           cb.moveTo(100f, 200f);
           cb.lineTo(200f, 250f);
           cb.lineTo(400f, 150f);
           cb.fill();
           // cb.sanityCheck is called during newPage().
           document.newPage();
           String text = "Some text to show";
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.25f))));
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
           
           // example with template
           PdfTemplate t = cb.createTemplate(500f, 500f);
           // Stroke a rectangle with CMYK alternate
           t.setColorStroke(new SpotColor(spc_cmyk, .5f));
           t.setLineWidth(10f);
           // draw a rectangle
           t.rectangle(100, 10, 100, 100);
           // add the diagonal
           t.moveTo(100, 10);
           t.lineTo(200, 100);
           // stroke the lines
           t.stroke();
           
           // Fill a rectangle with CMYK alternate
           t.setColorFill(spc_g, 0.5f);
           t.rectangle(100, 125, 100, 100);
           t.fill();
           t.beginText();
           t.setFontAndSize(bf, 20f);
           t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
           t.showText("Template text upside down");
           t.endText();
           t.rectangle(0, 0, 499, 499);
           t.stroke();
           t.sanityCheck();
           cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
           
           cb.sanityCheck();

       
       // step 5: we close the document
       document.close();
   }
 
Example 14
Source File: PatternTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Painting Patterns.
 * 
 * @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:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("pattern.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add some content
	PdfContentByte cb = writer.getDirectContent();
	PdfTemplate tp = cb.createTemplate(400, 300);
	PdfPatternPainter pat = cb.createPattern(15, 15, null);
	pat.rectangle(5, 5, 5, 5);
	pat.fill();
	pat.sanityCheck();

	PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV",
			new CMYKColor(0.9f, .2f, .3f, .1f));
	SpotColor spot = new SpotColor(spc_cmyk, 0.25f);
	tp.setPatternFill(pat, spot, .9f);
	tp.rectangle(0, 0, 400, 300);
	tp.fill();
	tp.sanityCheck();

	cb.addTemplate(tp, 50, 50);
	PdfPatternPainter pat2 = cb.createPattern(10, 10, null);
	pat2.setLineWidth(2);
	pat2.moveTo(-5, 0);
	pat2.lineTo(10, 15);
	pat2.stroke();
	pat2.moveTo(0, -5);
	pat2.lineTo(15, 10);
	pat2.stroke();
	cb.setLineWidth(1);
	cb.setColorStroke(Color.black);
	cb.setPatternFill(pat2, Color.red);
	cb.rectangle(100, 400, 30, 210);
	cb.fillStroke();
	cb.setPatternFill(pat2, Color.green);
	cb.rectangle(150, 400, 30, 100);
	cb.fillStroke();
	cb.setPatternFill(pat2, Color.blue);
	cb.rectangle(200, 400, 30, 130);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.5f));
	cb.rectangle(250, 400, 30, 80);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.7f));
	cb.rectangle(300, 400, 30, 170);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.9f));
	cb.rectangle(350, 400, 30, 40);
	cb.fillStroke();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 15
Source File: TransformationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding a template using different transformation matrices.
 */
@Test
public void main() throws Exception {

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

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream("transformations.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();

	// we create a PdfTemplate
	PdfTemplate template = cb.createTemplate(120, 120);

	// we add some graphics
	template.moveTo(30, 10);
	template.lineTo(90, 10);
	template.lineTo(90, 80);
	template.lineTo(110, 80);
	template.lineTo(60, 110);
	template.lineTo(10, 80);
	template.lineTo(30, 80);
	template.closePath();
	template.stroke();
	template.sanityCheck();

	// we add the template on different positions
	cb.addTemplate(template, 0, 0);
	cb.addTemplate(template, 0, 1, -1, 0, 200, 600);
	cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
	cb.sanityCheck();

	// we go to a new page
	document.newPage();
	cb.addTemplate(template, 0, 500);
	cb.addTemplate(template, 2, 0, -1, 2, 200, 300);
	cb.sanityCheck();
	// step 5: we close the document
	document.close();
}
 
Example 16
Source File: XandYcoordinatesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Creates a PDF document with shapes, lines and text at specific X and Y coordinates.
    */
@Test
public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document();
       
       try {      
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "XandY.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           
           // we create a PdfTemplate
           PdfTemplate template = cb.createTemplate(25, 25);
           
           // we add some crosses to visualize the coordinates
           template.moveTo(13, 0);
           template.lineTo(13, 25);
           template.moveTo(0, 13);
           template.lineTo(50, 13);
           template.stroke();
           template.sanityCheck();
           
           // we add the template on different positions
           cb.addTemplate(template, 216 - 13, 720 - 13);
           cb.addTemplate(template, 360 - 13, 360 - 13);
           cb.addTemplate(template, 360 - 13, 504 - 13);
           cb.addTemplate(template, 72 - 13, 144 - 13);
           cb.addTemplate(template, 144 - 13, 288 - 13);

           cb.moveTo(216, 720);
           cb.lineTo(360, 360);
           cb.lineTo(360, 504);
           cb.lineTo(72, 144);
           cb.lineTo(144, 288);
           cb.stroke();
           
           BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.beginText();
           cb.setFontAndSize(bf, 12);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
           cb.endText(); 
           
           cb.sanityCheck();
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 17
Source File: GStateTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changing the Graphics State with PdfGState.
 * 
 */
@Test
public void main() throws Exception {

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

	try {

		// 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( "gstate.pdf"));

		// step 3: we open the document
		document.open();

		// step 4: we grab the ContentByte and do some stuff with it
		PdfContentByte cb = writer.getDirectContent();

           PdfGState gs = new PdfGState();
           gs.setFillOpacity(0.5f);
           cb.setGState(gs);
		cb.setColorFill(Color.red);
		cb.circle(260.0f, 500.0f, 250.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 200.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 150.0f);
		cb.fill();
		gs.setFillOpacity(0.2f);
		cb.setGState(gs);
		cb.setColorFill(Color.blue);
		cb.circle(260.0f, 500.0f, 100.0f);
		cb.fill();
		cb.circle(260.0f, 500.0f, 50.0f);
		cb.fill();
		
		cb.sanityCheck();
	} catch (DocumentException de) {
		System.err.println(de.getMessage());
	} catch (IOException ioe) {
		System.err.println(ioe.getMessage());
	}

	// step 5: we close the document
	document.close();
}
 
Example 18
Source File: StateTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changing the Graphics State with saveState() and restoreState().
 * 
 */
@Test
public void main() throws Exception {

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

	try {

		// 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( "state.pdf"));

		// step 3: we open the document
		document.open();

		// step 4: we grab the ContentByte and do some stuff with it
		PdfContentByte cb = writer.getDirectContent();

		cb.circle(260.0f, 500.0f, 250.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.red);
		cb.circle(260.0f, 500.0f, 200.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.blue);
		cb.circle(260.0f, 500.0f, 150.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 100.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 50.0f);
		cb.fill();
		
		cb.sanityCheck();
	} catch (DocumentException de) {
		System.err.println(de.getMessage());
	} catch (IOException ioe) {
		System.err.println(ioe.getMessage());
	}

	// step 5: we close the document
	document.close();
}
 
Example 19
Source File: CirclesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Draws some concentric circles.
    */
@Test
public void main() throws Exception {
       
       
       // step 1: creation of a document-object
       Document document = new Document();
       
       try {
           
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "circles.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4: we grab the ContentByte and do some stuff with it
           PdfContentByte cb = writer.getDirectContent();

           cb.circle(250.0f, 500.0f, 200.0f);
           cb.circle(250.0f, 500.0f, 150.0f);
           cb.stroke();
           cb.setRGBColorFill(0xFF, 0x00, 0x00);
           cb.circle(250.0f, 500.0f, 100.0f);
           cb.fillStroke();
           cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
           cb.circle(250.0f, 500.0f, 50.0f);
           cb.fill();
           
           cb.sanityCheck();
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 20
Source File: TemplatesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
   
    */
@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("templates.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	// we create a PdfTemplate
	PdfTemplate template = cb.createTemplate(500, 200);

	// we add some graphics
	template.moveTo(0, 200);
	template.lineTo(500, 0);
	template.stroke();
	template.setRGBColorStrokeF(255f, 0f, 0f);
	template.circle(250f, 100f, 80f);
	template.stroke();

	// we add some text
	template.beginText();
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	template.setFontAndSize(bf, 12);
	template.setTextMatrix(100, 100);
	template.showText("Text at the position 100,100 (relative to the template!)");
	template.endText();
	template.sanityCheck();

	// we add the template on different positions
	cb.addTemplate(template, 0, 0);
	cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
	cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);

	// we go to a new page
	document.newPage();
	cb.addTemplate(template, 0, 400);
	cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
	cb.sanityCheck();

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