Java Code Examples for com.lowagie.text.pdf.PdfPCell#setHorizontalAlignment()

The following examples show how to use com.lowagie.text.pdf.PdfPCell#setHorizontalAlignment() . 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: PdfCacheInformationsReport.java    From javamelody with Apache License 2.0 6 votes vote down vote up
private void writeCacheInformations(CacheInformations cacheInformations) {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(cacheInformations.getName());
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	if (configurationEnabled) {
		addCell(integerFormat.format(cacheInformations.getInMemoryPercentUsed()));
	}
	addCell(integerFormat.format(cacheInformations.getInMemoryObjectCount()));
	addCell(integerFormat.format(cacheInformations.getOnDiskObjectCount()));
	if (hitsRatioEnabled) {
		addCell(integerFormat.format(cacheInformations.getInMemoryHitsRatio()));
		addCell(integerFormat.format(cacheInformations.getHitsRatio()));
	}
	if (configurationEnabled) {
		defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		addCell(cacheInformations.getConfiguration());
	}
}
 
Example 2
Source File: Cell.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a PdfPCell based on this Cell object.
 * @return a PdfPCell
 * @throws BadElementException
 */
public PdfPCell createPdfPCell() throws BadElementException {
	if (rowspan > 1) throw new BadElementException("PdfPCells can't have a rowspan > 1");
	if (isTable()) return new PdfPCell(((Table)arrayList.get(0)).createPdfPTable());
	PdfPCell cell = new PdfPCell();
	cell.setVerticalAlignment(verticalAlignment);
	cell.setHorizontalAlignment(horizontalAlignment);
	cell.setColspan(colspan);
	cell.setUseBorderPadding(useBorderPadding);
	cell.setUseDescender(useDescender);
	cell.setLeading(getLeading(), 0);
	cell.cloneNonPositionParameters(this);
	cell.setNoWrap(getMaxLines() == 1);
	for (Iterator i = getElements(); i.hasNext(); ) {
           Element e = (Element)i.next();
           if (e.type() == Element.PHRASE || e.type() == Element.PARAGRAPH) {
               Paragraph p = new Paragraph((Phrase)e);
               p.setAlignment(horizontalAlignment);
               e = p;
           }
		cell.addElement(e);
	}
	return cell;
}
 
Example 3
Source File: PdfProcessInformationsReport.java    From javamelody with Apache License 2.0 6 votes vote down vote up
private void writeProcessInformations(ProcessInformations processInformations) {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(processInformations.getUser());
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	addCell(integerFormat.format(processInformations.getPid()));
	if (!windows) {
		addCell(percentFormat.format(processInformations.getCpuPercentage()));
		addCell(percentFormat.format(processInformations.getMemPercentage()));
	}
	addCell(integerFormat.format(processInformations.getVsz()));
	if (!windows) {
		addCell(integerFormat.format(processInformations.getRss()));
		defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		addCell(processInformations.getTty());
		addCell(processInformations.getStat());
		defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		addCell(processInformations.getStart());
	}
	addCell(processInformations.getCpuTime());
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(processInformations.getCommand());
}
 
Example 4
Source File: PdfDatabaseInformationsReport.java    From javamelody with Apache License 2.0 6 votes vote down vote up
private void writeRow(String[] row) {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setVerticalAlignment(Element.ALIGN_TOP);
	for (final String value : row) {
		if (value == null || value.isEmpty()) {
			addCell("");
		} else {
			if (isNumber(value)) {
				defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			} else {
				defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
			}
			addCell(value);
		}
	}
}
 
Example 5
Source File: Cell.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a PdfPCell based on this Cell object.
 * @return a PdfPCell
 * @throws BadElementException
 */
public PdfPCell createPdfPCell() throws BadElementException {
	if (rowspan > 1) throw new BadElementException("PdfPCells can't have a rowspan > 1");
	if (isTable()) return new PdfPCell(((Table)arrayList.get(0)).createPdfPTable());
	PdfPCell cell = new PdfPCell();
	cell.setVerticalAlignment(verticalAlignment);
	cell.setHorizontalAlignment(horizontalAlignment);
	cell.setColspan(colspan);
	cell.setUseBorderPadding(useBorderPadding);
	cell.setUseDescender(useDescender);
	cell.setLeading(leading(), 0);
	cell.cloneNonPositionParameters(this);
	for (Iterator i = getElements(); i.hasNext(); ) {
		cell.addElement((Element)i.next());
	}
	return cell;
}
 
Example 6
Source File: PdfInstructionalOfferingTableBuilder.java    From unitime with Apache License 2.0 6 votes vote down vote up
private PdfPCell pdfBuildPrefGroupDemand(PreferenceGroup prefGroup, boolean isEditable){
  	if (prefGroup instanceof Class_) {
	Class_ c = (Class_) prefGroup;
	if (StudentClassEnrollment.sessionHasEnrollments(c.getSessionId())){
		PdfPCell tc = createCell();
		if (c.getEnrollment() != null){
			addText(tc, c.getEnrollment().toString());
		} else {
			addText(tc, "0");
		}
		tc.setHorizontalAlignment(Element.ALIGN_RIGHT);
		return(tc);
	}
}
  	return createCell();
  }
 
Example 7
Source File: Cell.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a PdfPCell based on this Cell object.
 * 
 * @return a PdfPCell
 * @throws BadElementException
 */
public PdfPCell createPdfPCell() throws BadElementException {
	if (rowspan > 1) {
		throw new BadElementException("PdfPCells can't have a rowspan > 1");
	}
	if (isTable()) {
		return new PdfPCell(((Table) arrayList.get(0)).createPdfPTable());
	}
	PdfPCell cell = new PdfPCell();
	cell.setVerticalAlignment(verticalAlignment);
	cell.setHorizontalAlignment(horizontalAlignment);
	cell.setColspan(colspan);
	cell.setUseBorderPadding(useBorderPadding);
	cell.setUseDescender(useDescender);
	cell.setLeading(getLeading(), 0);
	cell.cloneNonPositionParameters(this);
	cell.setNoWrap(getMaxLines() == 1);
	for (Iterator i = getElements(); i.hasNext();) {
		Element e = (Element) i.next();
		if (e.type() == Element.PHRASE || e.type() == Element.PARAGRAPH) {
			Paragraph p = new Paragraph((Phrase) e);
			p.setAlignment(horizontalAlignment);
			e = p;
		}
		cell.addElement(e);
	}
	return cell;
}
 
Example 8
Source File: PdfJCacheInformationsReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeCacheInformations(JCacheInformations jcacheInformations) {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(jcacheInformations.getName());
	if (hitsRatioEnabled) {
		defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		addCell(integerFormat.format(jcacheInformations.getHitsRatio()));
	}
}
 
Example 9
Source File: PdfRequestAndGraphDetailReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeRequest(CounterRequest childRequest, float executionsByRequest,
		boolean allChildHitsDisplayed) throws IOException, DocumentException {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	final Paragraph paragraph = new Paragraph(defaultCell.getLeading() + cellFont.getSize());
	if (executionsByRequest != -1) {
		paragraph.setIndentationLeft(5);
	}
	final Counter parentCounter = getCounterByRequestId(childRequest);
	if (parentCounter != null && parentCounter.getIconName() != null) {
		paragraph.add(new Chunk(getSmallImage(parentCounter.getIconName()), 0, -1));
	}
	paragraph.add(new Phrase(childRequest.getName(), cellFont));
	final PdfPCell requestCell = new PdfPCell();
	requestCell.addElement(paragraph);
	requestCell.setGrayFill(defaultCell.getGrayFill());
	requestCell.setPaddingTop(defaultCell.getPaddingTop());
	addCell(requestCell);

	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	if (executionsByRequest != -1) {
		addCell(nbExecutionsFormat.format(executionsByRequest));
	} else {
		final boolean hasChildren = !request.getChildRequestsExecutionsByRequestId().isEmpty();
		if (hasChildren) {
			addCell("");
		}
	}
	writeRequestValues(childRequest, allChildHitsDisplayed);
}
 
Example 10
Source File: PdfThreadInformationsReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeThreadInformations(ThreadInformations threadInformations)
		throws DocumentException, IOException {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(threadInformations.getName());
	defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	if (threadInformations.isDaemon()) {
		addCell(getString("oui"));
	} else {
		addCell(getString("non"));
	}
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	addCell(integerFormat.format(threadInformations.getPriority()));
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	final PdfPCell cell = new PdfPCell();
	final Paragraph paragraph = new Paragraph(
			getDefaultCell().getLeading() + cellFont.getSize());
	paragraph.add(new Chunk(
			getImage(
					"bullets/" + HtmlThreadInformationsReport.getStateIcon(threadInformations)),
			0, -1));
	paragraph.add(new Phrase(String.valueOf(threadInformations.getState()), cellFont));
	cell.addElement(paragraph);
	addCell(cell);
	if (stackTraceEnabled) {
		addCell(threadInformations.getExecutedMethod());
	}
	if (cpuTimeEnabled) {
		defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		addCell(integerFormat.format(threadInformations.getCpuTimeMillis()));
		addCell(integerFormat.format(threadInformations.getUserTimeMillis()));
	}
}
 
Example 11
Source File: PDFUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a cell.
 *
 * @param text            The text to include in the cell.
 * @param colspan         The column span of the cell.
 * @param font            The font of the cell text.
 * @param horizontalAlign The vertical alignment of the text in the cell.
 * @return A PdfCell.
 */
public static PdfPCell getCell( String text, int colspan, Font font, int horizontalAlign )
{
    Paragraph paragraph = new Paragraph( text, font );

    PdfPCell cell = new PdfPCell( paragraph );

    cell.setColspan( colspan );
    cell.setBorder( 0 );
    cell.setMinimumHeight( 15 );
    cell.setHorizontalAlignment( horizontalAlign );

    return cell;
}
 
Example 12
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void insertTable_OrgAndPeriod( PdfPTable mainTable, PdfWriter writer, List<Period> periods )
    throws IOException, DocumentException
{
    boolean hasBorder = false;
    float width = 220.0f;

    // Input TextBox size
    Rectangle rectangle = new Rectangle( width, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT );

    // Add Organization ID/Period textfield
    // Create A table to add for each group AT HERE
    PdfPTable table = new PdfPTable( 2 ); // Code 1
    table.setWidths( new int[]{ 1, 3 } );
    table.setHorizontalAlignment( Element.ALIGN_LEFT );


    addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), "Organization unit identifier", Element.ALIGN_RIGHT );
    addCell_WithTextField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), PdfDataEntryFormUtil.LABELCODE_ORGID,
        PdfFieldCell.TYPE_TEXT_ORGUNIT );

    String[] periodsTitle = getPeriodTitles( periods, format );
    String[] periodsValue = getPeriodValues( periods );

    addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), "Period", Element.ALIGN_RIGHT );
    addCell_WithDropDownListField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), PdfDataEntryFormUtil.LABELCODE_PERIODID, periodsTitle, periodsValue );

    // Add to the main table
    PdfPCell cell_withInnerTable = new PdfPCell( table );
    // cell_withInnerTable.setPadding(0);
    cell_withInnerTable.setBorder( Rectangle.NO_BORDER );

    cell_withInnerTable.setHorizontalAlignment( Element.ALIGN_LEFT );

    mainTable.addCell( cell_withInnerTable );
}
 
Example 13
Source File: PdfInstructionalOfferingTableBuilder.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void addText(PdfPCell cell, String text, boolean bold, boolean italic,  int orientation, Color color, boolean newLine) {
	if (text==null) return;
	if (cell.getPhrase()==null) {
		Chunk ch = new Chunk(text, PdfFont.getFont(bold, italic, color));
		cell.setPhrase(new Paragraph(ch));
		cell.setVerticalAlignment(Element.ALIGN_TOP);
		cell.setHorizontalAlignment(orientation);
	} else {
		cell.getPhrase().add(new Chunk((newLine?"\n":"")+text, PdfFont.getFont(bold, italic, color)));
	}
}
 
Example 14
Source File: IncCell.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of IncCell */
public IncCell(String tag, ChainedProperties props) {
    cell = new PdfPCell((Phrase)null);
    String value = props.getProperty("colspan");
    if (value != null)
        cell.setColspan(Integer.parseInt(value));
    value = props.getProperty("align");
    if (tag.equals("th"))
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    if (value != null) {
        if ("center".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        else if ("right".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        else if ("left".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        else if ("justify".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
    }
    value = props.getProperty("valign");
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    if (value != null) {
        if ("top".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_TOP);
        else if ("bottom".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    }
    value = props.getProperty("border");
    float border = 0;
    if (value != null)
        border = Float.parseFloat(value);
    cell.setBorderWidth(border);
    value = props.getProperty("cellpadding");
    if (value != null)
        cell.setPadding(Float.parseFloat(value));
    cell.setUseDescender(true);
    value = props.getProperty("bgcolor");
    cell.setBackgroundColor(Markup.decodeColor(value));
}
 
Example 15
Source File: PdfSessionInformationsReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeSession(SessionInformations session) throws IOException, BadElementException {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(session.getId());
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	addCell(durationFormat.format(session.getLastAccess()));
	addCell(durationFormat.format(session.getAge()));
	addCell(expiryFormat.format(session.getExpirationDate()));
	addCell(integerFormat.format(session.getAttributeCount()));
	defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	if (session.isSerializable()) {
		addCell(getString("oui"));
	} else {
		final Phrase non = new Phrase(getString("non"), severeCellFont);
		addCell(non);
	}
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	addCell(integerFormat.format(session.getSerializedSize()));
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	final String remoteAddr = session.getRemoteAddr();
	if (remoteAddr == null) {
		addCell("");
	} else {
		addCell(remoteAddr);
	}
	defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	writeCountry(session);
	writeBrowserAndOs(session);
	if (displayUser) {
		defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		final String remoteUser = session.getRemoteUser();
		if (remoteUser == null) {
			addCell("");
		} else {
			addCell(remoteUser);
		}
	}
}
 
Example 16
Source File: PdfTimetableGridTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
public PdfPCell createCellNoBorder() {
	PdfPCell cell = new PdfPCell();
	cell.setBorderColor(sBorderColor);
	cell.setPadding(3);
	cell.setBorderWidth(0);
	cell.setVerticalAlignment(Element.ALIGN_TOP);
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	return cell;
}
 
Example 17
Source File: PdfDataEntryFormUtil.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static PdfPCell getPdfPCell( float minHeight, int cellContentType, boolean hasBorder )
{
    PdfPCell cell = new PdfPCell();
    cell.setMinimumHeight( minHeight );
    
    if( hasBorder )
    {
        cell.setBorderWidth( 0.1f );
        cell.setBorderColor( COLOR_CELLBORDER );            
    }
    else
    {
        cell.setBorder( Rectangle.NO_BORDER );
    }
    
    cell.setPadding( 2.0f );

    switch ( cellContentType )
    {
        case CELL_COLUMN_TYPE_ENTRYFIELD:
            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
            cell.setVerticalAlignment( Element.ALIGN_MIDDLE );

            break;

        case CELL_COLUMN_TYPE_HEADER:
            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
            cell.setVerticalAlignment( Element.ALIGN_MIDDLE );

            break;

        case CELL_COLUMN_TYPE_LABEL:
            cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
            cell.setVerticalAlignment( Element.ALIGN_TOP );

        default:
            break;
    }

    return cell;
}
 
Example 18
Source File: VerticalTextInCellsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 19
Source File: ExampleEAN128Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example Barcode EAN128.
 */
@Test
public void main() throws Exception {

	// step 1
	Document document = new Document();
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ean128.pdf"));
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	PdfPTable pageTot = new PdfPTable(1);
	pageTot.getDefaultCell().setPadding(0f);
	pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	pageTot.setWidthPercentage(100f);
	// Data for the barcode : it is composed of 3 blocks whith AI 402, 90
	// and 421
	// The blocks whith the type 402 and 90 are of variable size so you must
	// put a FNC1
	// to delimitate the block
	String code402 = "24132399420058289" + Barcode128.FNC1;
	String code90 = "3700000050" + Barcode128.FNC1;
	String code421 = "422356";
	String data = code402 + code90 + code421;

	PdfPTable cell = new PdfPTable(1);
	cell.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	cell.getDefaultCell().setPadding(0f);

	PdfPCell info = new PdfPCell(new Phrase("Barcode EAN 128"));
	info.setBorder(Rectangle.NO_BORDER);
	pageTot.addCell(info);

	Barcode128 shipBarCode = new Barcode128();
	shipBarCode.setX(0.75f);
	shipBarCode.setN(1.5f);
	shipBarCode.setChecksumText(true);
	shipBarCode.setGenerateChecksum(true);
	shipBarCode.setSize(10f);
	shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
	shipBarCode.setBaseline(10f);
	shipBarCode.setCode(data);
	shipBarCode.setBarHeight(50f);

	Image imgShipBarCode = shipBarCode.createImageWithBarcode(cb, Color.black, Color.blue);
	PdfPCell shipment = new PdfPCell(new Phrase(new Chunk(imgShipBarCode, 0, 0)));
	shipment.setFixedHeight(shipBarCode.getBarcodeSize().getHeight() + 16f);
	shipment.setPaddingTop(5f);
	shipment.setPaddingBottom(10f);
	shipment.setBorder(Rectangle.BOX);
	shipment.setVerticalAlignment(Element.ALIGN_TOP);
	shipment.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.addCell(shipment);

	pageTot.addCell(cell);

	document.add(pageTot);

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

   }