Java Code Examples for com.lowagie.text.Rectangle#setBackgroundColor()

The following examples show how to use com.lowagie.text.Rectangle#setBackgroundColor() . 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: PdfPRow.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Writes the border and background of one cell in the row.
 * 
 * @param xPos The x-coordinate where the table starts on the canvas
 * @param yPos The y-coordinate where the table starts on the canvas
 * @param currentMaxHeight The height of the cell to be drawn.
 * @param cell
 * @param canvases
 * @since	2.1.6	extra parameter currentMaxHeight
 */
public void writeBorderAndBackground(float xPos, float yPos, float currentMaxHeight, PdfPCell cell, PdfContentByte[] canvases) {
	Color background = cell.getBackgroundColor();
	if (background != null || cell.hasBorders()) {
		// Add xPos resp. yPos to the cell's coordinates for absolute coordinates
		float right = cell.getRight() + xPos;
		float top = cell.getTop() + yPos;
		float left = cell.getLeft() + xPos;
		float bottom = top - currentMaxHeight;
		
		if (background != null) {
			PdfContentByte backgr = canvases[PdfPTable.BACKGROUNDCANVAS];
			backgr.setColorFill(background);
			backgr.rectangle(left, bottom, right - left, top - bottom);
			backgr.fill();
		}
		if (cell.hasBorders()) {
			Rectangle newRect = new Rectangle(left, bottom, right, top);
			// Clone non-position parameters except for the background color
			newRect.cloneNonPositionParameters(cell);
			newRect.setBackgroundColor(null);
			// Write the borders on the line canvas
			PdfContentByte lineCanvas = canvases[PdfPTable.LINECANVAS];
			lineCanvas.rectangle(newRect);
		}
	}
}
 
Example 2
Source File: PdfPRow.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writes the border and background of one cell in the row.
 * 
 * @param xPos The x-coordinate where the table starts on the canvas
 * @param yPos The y-coordinate where the table starts on the canvas
 * @param currentMaxHeight The height of the cell to be drawn.
 * @param cell
 * @param canvases
 * @since	2.1.6	extra parameter currentMaxHeight
 */
public void writeBorderAndBackground(float xPos, float yPos, float currentMaxHeight, PdfPCell cell, PdfContentByte[] canvases) {
	Color background = cell.getBackgroundColor();
	if (background != null || cell.hasBorders()) {
		// Add xPos resp. yPos to the cell's coordinates for absolute coordinates
		float right = cell.getRight() + xPos;
		float top = cell.getTop() + yPos;
		float left = cell.getLeft() + xPos;
		float bottom = top - currentMaxHeight;
		
		if (background != null) {
			PdfContentByte backgr = canvases[PdfPTable.BACKGROUNDCANVAS];
			backgr.setColorFill(background);
			backgr.rectangle(left, bottom, right - left, top - bottom);
			backgr.fill();
		}
		if (cell.hasBorders()) {
			Rectangle newRect = new Rectangle(left, bottom, right, top);
			// Clone non-position parameters except for the background color
			newRect.cloneNonPositionParameters(cell);
			newRect.setBackgroundColor(null);
			// Write the borders on the line canvas
			PdfContentByte lineCanvas = canvases[PdfPTable.LINECANVAS];
			lineCanvas.rectangle(newRect);
		}
	}
}
 
Example 3
Source File: CustomPageSizeTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a PDF document with a certain pagesize
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Rectangle pageSize = new Rectangle(216, 720);
	pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
	Document document = new Document(pageSize);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("CustomPageSize.pdf"));

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

	// step 4: we add some paragraphs to the document
	document.add(new Paragraph("The size of this page is 216x720 points."));
	document.add(new Paragraph("216pt / 72 points per inch = 3 inch"));
	document.add(new Paragraph("720pt / 72 points per inch = 10 inch"));
	document.add(new Paragraph("The size of this page is 3x10 inch."));
	document.add(new Paragraph("3 inch x 2.54 = 7.62 cm"));
	document.add(new Paragraph("10 inch x 2.54 = 25.4 cm"));
	document.add(new Paragraph("The size of this page is 7.62x25.4 cm."));
	document.add(new Paragraph("The backgroundcolor of the Rectangle used for this PageSize, is #FFFFDE."));
	document.add(new Paragraph("That's why the background of this document is yellowish..."));

	// step 5: we close the document
	document.close();
}
 
Example 4
Source File: TemplateImagesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * PdfTemplates can be wrapped in an Image.
    */
@Test
   public  void main() throws Exception {
       
           
       // step 1: creation of a document-object
       Rectangle rect = new Rectangle(PageSize.A4);
       rect.setBackgroundColor(new Color(238, 221, 88));
       Document document = new Document(rect, 50, 50, 50, 50);
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("templateImages.pdf"));
	// step 3: we open the document
	document.open();
	// step 4:
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
			BaseFont.NOT_EMBEDDED);
	String text = "Vertical";
	float size = 16;
	float width = bf.getWidthPoint(text, size);
	template.beginText();
	template.setRGBColorFillF(1, 1, 1);
	template.setFontAndSize(bf, size);
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(size + 2);
	template.sanityCheck();
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	Chunk ck = new Chunk(img, 0, 0);
	PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	PdfPCell cell = new PdfPCell(img);
	cell.setPadding(4);
	cell.setBackgroundColor(new Color(0, 0, 255));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	table.addCell(cell);
	table.addCell("I see a template everywhere");
	table.addCell(cell);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");

	Paragraph p1 = new Paragraph("This is a template ");
	p1.add(ck);
	p1.add(" just here.");
	p1.setLeading(img.getScaledHeight() * 1.1f);
	document.add(p1);
	document.add(table);
	Paragraph p2 = new Paragraph("More templates ");
	p2.setLeading(img.getScaledHeight() * 1.1f);
	p2.setAlignment(Element.ALIGN_JUSTIFIED);
	img.scalePercent(70);
	for (int k = 0; k < 20; ++k)
		p2.add(ck);
	document.add(p2);
	// step 5: we close the document
	document.close();

   }
 
Example 5
Source File: StudentCardTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates a StudentCard
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Rectangle rect = new Rectangle(243, 153);
	rect.setBackgroundColor(new Color(0xFF, 0xFF, 0xCC));
	Document document = new Document(rect, 10, 10, 10, 10);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("studentcard.pdf"));

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

	// step 4:
	Font font = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, Color.BLUE);
	Paragraph p = new Paragraph("Ghent University", font);
	p.setAlignment(Element.ALIGN_CENTER);
	document.add(p);
	PdfContentByte cb = writer.getDirectContent();
	Font f = FontFactory.getFont(FontFactory.HELVETICA, 8);
	PdfPTable outertable = new PdfPTable(3);
	outertable.setTotalWidth(200);
	outertable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	float[] outer = { 60, 25, 15 };
	outertable.setWidths(outer);
	PdfPTable innertable = new PdfPTable(2);
	float[] inner = { 35, 65 };
	innertable.setWidths(inner);
	innertable.addCell(new Paragraph("name:", f));
	innertable.addCell(new Paragraph("Bruno Lowagie", f));
	innertable.addCell(new Paragraph("date of birth:", f));
	innertable.addCell(new Paragraph("June 10th, 1970", f));
	innertable.addCell(new Paragraph("Study Program:", f));
	innertable.addCell(new Paragraph("master in civil engineering", f));
	innertable.addCell(new Paragraph("option:", f));
	innertable.addCell(new Paragraph("architecture", f));
	outertable.addCell(innertable);
	outertable.getDefaultCell().setBackgroundColor(new Color(0xFF, 0xDE, 0xAD));
	outertable.addCell(Image.getInstance(PdfTestBase.RESOURCES_DIR + "bruno.jpg"));
	BarcodeEAN codeEAN = new BarcodeEAN();
	codeEAN.setCodeType(Barcode.EAN13);
	codeEAN.setCode("8010012529736");
	Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
	imageEAN.setRotationDegrees(90);
	outertable.getDefaultCell().setBackgroundColor(Color.WHITE);
	outertable.addCell(imageEAN);
	outertable.writeSelectedRows(0, -1, 20, 100, writer.getDirectContent());

	// step 5: we close the document
	document.close();
}
 
Example 6
Source File: DvdCoverTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Method that generates the actual PDF file.
 */
public void generatePdf() throws Exception {

	// step 1: creation of a document-object
	Rectangle pageSize = new Rectangle(780, 525);
	if (backgroundcolor != null) {
		pageSize.setBackgroundColor(backgroundcolor);
	}
	Document document = new Document(pageSize);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	if (filename == null) {
		filename = PdfTestBase.OUTPUT_DIR + "dvdcover.pdf";
	}
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));

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

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	if (title != null) {
		cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
		cb.beginText();
		if (front == null) {
			cb.showTextAligned(Element.ALIGN_CENTER, title, 595f, 262f, 0f);
		}
		if (side == null) {
			cb.showTextAligned(Element.ALIGN_CENTER, title, 385f, 262f, 270f);
		}
		cb.endText();
	}
	cb.moveTo(370, 0);
	cb.lineTo(370, 525);
	cb.moveTo(410, 525);
	cb.lineTo(410, 0);
	cb.stroke();
	if (front != null) {
		front.scaleToFit(370, 525);
		front.setAbsolutePosition(410f + (370f - front.getScaledWidth()) / 2f,
				(525f - front.getScaledHeight()) / 2f);
		document.add(front);
	}
	if (back != null) {
		back.scaleToFit(370, 525);
		back.setAbsolutePosition((370f - back.getScaledWidth()) / 2f, (525f - back.getScaledHeight()) / 2f);
		document.add(back);
	}
	if (side != null) {
		side.scaleToFit(40, 525);
		side.setAbsolutePosition(370 + (40f - side.getScaledWidth()) / 2f, (525f - side.getScaledHeight()) / 2f);
		document.add(side);
	}

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