/*
 * $Id: VerticalTextInCells.java 3373 2008-05-12 16:21:24Z xlv $
 *
 * This code is part of the 'iText Tutorial'.
 * You can find the complete tutorial at the following address:
 * http://itextdocs.lowagie.com/tutorial/
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * [email protected]
 */
package com.lowagie.examples.objects.tables.pdfptable;

import java.awt.Color;

import org.junit.Test;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTestBase;
import com.lowagie.text.pdf.PdfWriter;

/**
 * Vertical Text in Cells.
 */
public class VerticalTextInCellsTest {

	/**
	 * Example with vertical text in Cells.
	 */
	@Test
	public void main() throws Exception {

		// step1
		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		// step2
		PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("VerticalText.pdf"));
		// step3
		document.open();
		// step4

		// make a PdfTemplate with the vertical text
		PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
		BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
		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);
		// make an Image object from the template
		Image img = Image.getInstance(template);
		img.setRotationDegrees(90);
		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");
		document.add(table);

		// step5
		document.close();
	}
}