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

The following examples show how to use com.lowagie.text.pdf.PdfTemplate#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: 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 2
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 3
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 4
Source File: TextOnlySignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void showText(ITextFontMetrics iTextFontMetrics, Rectangle sigFieldRect) {
	
	SignatureImageTextParameters textParameters = parameters.getTextParameters();
	String text = textParameters.getText();

	float size = getProperSize();
	
	PdfTemplate layer = appearance.getLayer(2);
	layer.setFontAndSize(iTextFont.getBaseFont(), size);
	
	Rectangle boundingRectangle = new Rectangle(sigFieldRect.getWidth(), sigFieldRect.getHeight()); // defines text field borders
	boundingRectangle.setBackgroundColor(parameters.getTextParameters().getBackgroundColor());
	layer.rectangle(boundingRectangle);
	
	layer.setBoundingBox(boundingRectangle);
	layer.setColorStroke(textParameters.getTextColor());
	
	String[] lines = iTextFontMetrics.getLines(text);
	
	layer.beginText();
	
	float strHeight = iTextFontMetrics.getHeight(lines[0], size);
	float y = boundingRectangle.getHeight() - textParameters.getPadding();
	float x = textParameters.getPadding();
	
	layer.moveText(x, y);
	layer.newlineText();

	y = -strHeight;

       float previousOffset = 0;
	for (String line : lines) {
           float offsetX = 0;
		float lineWidth = iTextFontMetrics.getWidth(line, size);
		switch (textParameters.getSignerTextHorizontalAlignment()) {
			case RIGHT:
				offsetX = boundingRectangle.getWidth() - lineWidth - textParameters.getPadding() * 2 - previousOffset;
				break;
			case CENTER:
				offsetX = (boundingRectangle.getWidth() - lineWidth) / 2 - textParameters.getPadding() - previousOffset;
				break;
			default:
				break;
		}
		previousOffset += offsetX;
		layer.moveText(offsetX, y);
		layer.newlineShowText(line);
	}
	
	layer.endText();
}