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

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#rectangle() . 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: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void lowerRightAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARGIN,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 2
Source File: GroupsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prints a square and fills half of it with a gray rectangle.
 * @param x
 * @param y
 * @param cb
 * @throws Exception
 */
public static void pictureBackdrop(float x, float y, PdfContentByte cb) throws Exception {
    cb.setColorStroke(Color.black);
    cb.setColorFill(Color.red);
    cb.rectangle(x, y, 100, 200);
    cb.fill();
    cb.setLineWidth(2);
    cb.rectangle(x, y, 200, 200);
    cb.stroke();
}
 
Example 3
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void upperRightAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH,
                 (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARGIN - ALIGNMENT_MARK_HEIGHT,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 4
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void upperLeftAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(ALIGNMENT_MARGIN,
                 (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARK_HEIGHT - ALIGNMENT_MARGIN,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 5
Source File: TransparencyTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prints a square and fills half of it with a gray rectangle.
 * 
 * @param x
 * @param y
 * @param cb
 * @throws Exception
 */
private static void pictureBackdrop(float x, float y, PdfContentByte cb)
		throws Exception {
	cb.setColorStroke(Color.black);
	cb.setColorFill(Color.gray);
	cb.rectangle(x, y, 100, 200);
	cb.fill();
	cb.setLineWidth(2);
	cb.rectangle(x, y, 200, 200);
	cb.stroke();
}
 
Example 6
Source File: PdfExportHelper.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
    if (active) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.rectangle(document.left(), paragraphPosition - offset, document.right() - document.left(),
                startPosition - paragraphPosition);
        cb.stroke();
    }
}
 
Example 7
Source File: VerticalTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writing vertical text.
 */
@Test
public void main() throws Exception {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	texts[3] = convertCid(texts[0]);
	texts[4] = convertCid(texts[1]);
	texts[5] = convertCid(texts[2]);
	PdfWriter writer = PdfWriter.getInstance(document,PdfTestBase.getOutputStream("vertical.pdf"));
	int idx = 0;
	document.open();
	PdfContentByte cb = writer.getDirectContent();
	for (int j = 0; j < 2; ++j) {
		BaseFont bf = BaseFont.createFont("KozMinPro-Regular", encs[j],	false);
		cb.setRGBColorStroke(255, 0, 0);
		cb.setLineWidth(0);
		float x = 400;
		float y = 700;
		float height = 400;
		float leading = 30;
		int maxLines = 6;
		for (int k = 0; k < maxLines; ++k) {
			cb.moveTo(x - k * leading, y);
			cb.lineTo(x - k * leading, y - height);
		}
		cb.rectangle(x, y, -leading * (maxLines - 1), -height);
		cb.stroke();
		VerticalText vt = new VerticalText(cb);
		vt.setVerticalLayout(x, y, height, maxLines, leading);
		vt.addText(new Chunk(texts[idx++], new Font(bf, 20)));
		vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.blue)));
		vt.go();
		vt.setAlignment(Element.ALIGN_RIGHT);
		vt.addText(new Chunk(texts[idx++],	new Font(bf, 20, 0, Color.orange)));
		vt.go();
		document.newPage();
	}
	document.close();

}
 
Example 8
Source File: FloatingBoxesTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
 *      com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
 */
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
	float x1 = position.getLeft() + 2;
	float x2 = position.getRight() - 2;
	float y1 = position.getTop() - 2;
	float y2 = position.getBottom() + 2;
	PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
	canvas.setRGBColorStroke(0xFF, 0x00, 0x00);
	canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
	canvas.stroke();
	canvas.resetRGBColorStroke();
}
 
Example 9
Source File: FloatingBoxesTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
 *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] height, int headerRows, int rowStart,
		PdfContentByte[] canvases) {
	float widths[] = width[0];
	float x1 = widths[0];
	float x2 = widths[widths.length - 1];
	float y1 = height[0];
	float y2 = height[height.length - 1];
	PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
	canvas.setRGBColorStroke(0x00, 0x00, 0xFF);
	canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
	canvas.stroke();
	canvas.resetRGBColorStroke();
}
 
Example 10
Source File: ColumnSimpleTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrating the use of ColumnText
 */
@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("columnsimple.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();

	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.addText(new Phrase(15, "UNI\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, uni[i] + "\n", font));
	}
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.addText(new Phrase(15, "char\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, code[i] + "\n", font));
	}
	ct.go();
	ct.setSimpleColumn(160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.addText(new Phrase(15, "NAME" + "\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, name[i] + "\n", font));
	}
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 11
Source File: TableEvents1Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
 *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart,
		PdfContentByte[] canvases) {

	// widths of the different cells of the first row
	float widths[] = width[0];

	PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
	cb.saveState();
	// border for the complete table
	cb.setLineWidth(2);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0]
			- heights[heights.length - 1]);
	cb.stroke();

	// border for the header rows
	if (headerRows > 0) {
		cb.setRGBColorStroke(0, 0, 255);
		cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0]
				- heights[headerRows]);
		cb.stroke();
	}
	cb.restoreState();

	cb = canvases[PdfPTable.BASECANVAS];
	cb.saveState();
	// border for the cells
	cb.setLineWidth(.5f);
	// loop over the rows
	for (int line = 0; line < heights.length - 1; ++line) {
		// loop over the columns
		for (int col = 0; col < widths.length - 1; ++col) {
			if (line == 0 && col == 0)
				cb.setAction(new PdfAction("http://www.lowagie.com/iText/"), widths[col], heights[line + 1],
						widths[col + 1], heights[line]);
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			// horizontal borderline
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col + 1], heights[line]);
			cb.stroke();
			// vertical borderline
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col], heights[line + 1]);
			cb.stroke();
		}
	}
	cb.restoreState();
}
 
Example 12
Source File: TableEvents2Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
	
	// widths of the different cells of the first row
    float widths[] = width[0];
    
    PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
    cb.saveState();
	// border for the complete table
    cb.setLineWidth(2);
    cb.setRGBColorStroke(255, 0, 0);
    cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
    cb.stroke();
    
    // border for the header rows
    if (headerRows > 0) {
        cb.setRGBColorStroke(0, 0, 255);
        cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
        cb.stroke();
    }
    cb.restoreState();
    
    cb = canvases[PdfPTable.BASECANVAS];
    cb.saveState();
    // border for the cells
    cb.setLineWidth(.5f);
    // loop over the rows
    for (int line = 0; line < heights.length - 1; ++line) {
        widths = width[line];
    	// loop over the columns
        for (int col = 0; col < widths.length - 1; ++col) {
            if (line == 0 && col == 0)
                cb.setAction(new PdfAction("http://www.lowagie.com/iText/"),
                    widths[col], heights[line + 1], widths[col + 1], heights[line]);
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            // horizontal borderline
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col + 1], heights[line]);
            cb.stroke();
            // vertical borderline
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col], heights[line + 1]);
            cb.stroke();
        }
    }
    cb.restoreState();
}
 
Example 13
Source File: ColumnTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrating the use of ColumnText
 */
@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("column.pdf"));

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

	// step 4:

	// we create some content
	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	Phrase unicodes = new Phrase(15, "UNI\n", font);
	Phrase characters = new Phrase(15, "\n", font);
	Phrase names = new Phrase(15, "NAME\n", font);

	for (int i = 0; i < 27; i++) {
		unicodes.add(uni[i] + "\n");
		characters.add(code[i] + "\n");
		names.add(name[i] + "\n");
	}

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

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(unicodes, 60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(characters, 105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.go();
	ct.setSimpleColumn(names, 160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 14
Source File: ShapesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Draws some shapes.
    */
@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( "shapes.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();
           
           // an example of a rectangle with a diagonal in very thick lines
           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();
           
           // an example of some circles
           cb.setLineDash(3, 3, 0);
           cb.setRGBColorStrokeF(0f, 255f, 0f);
           cb.circle(150f, 500f, 100f);
           cb.stroke();
           
           cb.setLineWidth(5f);
           cb.resetRGBColorStroke();
           cb.circle(150f, 500f, 50f);
           cb.stroke();
           
           // example with colorfill
           cb.setRGBColorFillF(0f, 255f, 0f);
           cb.moveTo(100f, 200f);
           cb.lineTo(200f, 250f);
           cb.lineTo(400f, 150f);
           // because we change the fill color BEFORE we stroke the triangle
           // the color of the triangle will be red instead of green
           cb.setRGBColorFillF(255f, 0f, 0f);
           cb.closePathFillStroke();
           
           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 15
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 16
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 17
Source File: ComplexTextTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding text at absolute positions.
 * 
 * @param args
 *            no arguments needed
 */
@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("complextext.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();

	File font = new File (PdfTestBase.RESOURCES_DIR + "/liberation-fonts-ttf/LiberationSans-Regular.ttf");
	// we construct a font
	BaseFont bf = BaseFont.createFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, true);
	Font ft = new Font(bf, 12);
	// This is the text:
	String text = "\u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a \u0627\u0644\u062d\u0627\u0633\u0648\u0628 + \u0627\u0646\u062a\u0631\u0646\u064a\u062a :";
	Phrase center = new Phrase(text + " Center", ft);
	ColumnText
			.showTextAligned(cb, PdfContentByte.ALIGN_CENTER, center, 250, 700, 0, PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_RIGHT, new Phrase(text + " Right", ft), 250, 650, 20,
			PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, new Phrase("Some text Left aligned", ft), 250, 600,
			20);
	float size = ColumnText.getWidth(center, PdfWriter.RUN_DIRECTION_RTL, 0);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(250 - size / 2, 690, size, 30);
	cb.stroke();

	// step 5: we close the document
	document.close();
}
 
Example 18
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 19
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 20
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();
}