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

The following examples show how to use com.lowagie.text.pdf.PdfPCell#addElement() . 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: 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 2
Source File: PdfCounterRequestContextReport.java    From javamelody with Apache License 2.0 6 votes vote down vote up
private void writeRequests(List<CounterRequestContext> contexts)
		throws DocumentException, IOException {
	final PdfPCell defaultCell = getDefaultCell();
	final PdfPCell requestCell = new PdfPCell();
	final Paragraph phrase = new Paragraph("", cellFont);
	int margin = 0;
	for (final CounterRequestContext context : contexts) {
		writeRequest(context, requestCell, margin);
		margin += 5;
	}
	// on utilise ici PdfPCell et addElement pour que les propriétés
	// leading et indentationLeft des paragraphes soient prises en compte
	requestCell.addElement(phrase);
	requestCell.setGrayFill(defaultCell.getGrayFill());
	requestCell.setPaddingTop(defaultCell.getPaddingTop());
	addCell(requestCell);
}
 
Example 3
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 4
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 5
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 6
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 7
Source File: PdfCounterRequestContextReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeRequest(CounterRequestContext context, PdfPCell cell, int margin)
		throws DocumentException, IOException {
	final Paragraph paragraph = new Paragraph(
			getDefaultCell().getLeading() + cellFont.getSize());
	paragraph.setIndentationLeft(margin);
	if (context.getParentCounter().getIconName() != null) {
		paragraph.add(new Chunk(getImage(context.getParentCounter().getIconName()), 0, -1));
	}
	paragraph.add(new Phrase(context.getCompleteRequestName(), cellFont));
	cell.addElement(paragraph);
}
 
Example 8
Source File: PDFPrinter.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void printHeader(String... fields) {
	iTable = new PdfPTable(fields.length - iHiddenColumns.size());
	iMaxWidth = new float[fields.length];
	iTable.setHeaderRows(1);
	iTable.setWidthPercentage(100);

	for (int idx = 0; idx < fields.length; idx++) {
		if (iHiddenColumns.contains(idx)) continue;
		String f = fields[idx];
		
		PdfPCell cell = new PdfPCell();
		cell.setBorder(Rectangle.BOTTOM);
		cell.setVerticalAlignment(Element.ALIGN_TOP);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		
		Font font = PdfFont.getFont(true);
		Paragraph ch = new Paragraph(f, font);
		ch.setLeading(0f, 1f);
		cell.addElement(ch);		
		iTable.addCell(cell);
		
		float width = 0; 
		if (f.indexOf('\n')>=0) {
			for (StringTokenizer s = new StringTokenizer(f,"\n"); s.hasMoreTokens();)
				width = Math.max(width,font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
		} else 
			width = Math.max(width,font.getBaseFont().getWidthPoint(f, font.getSize()));
		iMaxWidth[idx] = width;
	}
}
 
Example 9
Source File: SimpleCell.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a PdfPCell with these attributes.
 * 
 * @param rowAttributes
 * @return a PdfPCell based on these attributes.
 */
public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
	PdfPCell cell = new PdfPCell();
	cell.setBorder(NO_BORDER);
	SimpleCell tmp = new SimpleCell(CELL);
	tmp.setSpacing_left(spacing_left);
	tmp.setSpacing_right(spacing_right);
	tmp.setSpacing_top(spacing_top);
	tmp.setSpacing_bottom(spacing_bottom);
	tmp.cloneNonPositionParameters(rowAttributes);
	tmp.softCloneNonPositionParameters(this);
	cell.setCellEvent(tmp);
	cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
	cell.setVerticalAlignment(rowAttributes.verticalAlignment);
	cell.setUseAscender(rowAttributes.useAscender);
	cell.setUseBorderPadding(rowAttributes.useBorderPadding);
	cell.setUseDescender(rowAttributes.useDescender);
	cell.setColspan(colspan);
	if (horizontalAlignment != Element.ALIGN_UNDEFINED) {
		cell.setHorizontalAlignment(horizontalAlignment);
	}
	if (verticalAlignment != Element.ALIGN_UNDEFINED) {
		cell.setVerticalAlignment(verticalAlignment);
	}
	if (useAscender) {
		cell.setUseAscender(useAscender);
	}
	if (useBorderPadding) {
		cell.setUseBorderPadding(useBorderPadding);
	}
	if (useDescender) {
		cell.setUseDescender(useDescender);
	}
	float p;
	float sp_left = spacing_left;
	if (Float.isNaN(sp_left)) {
		sp_left = 0f;
	}
	float sp_right = spacing_right;
	if (Float.isNaN(sp_right)) {
		sp_right = 0f;
	}
	float sp_top = spacing_top;
	if (Float.isNaN(sp_top)) {
		sp_top = 0f;
	}
	float sp_bottom = spacing_bottom;
	if (Float.isNaN(sp_bottom)) {
		sp_bottom = 0f;
	}
	p = padding_left;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingLeft(p + sp_left);
	p = padding_right;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingRight(p + sp_right);
	p = padding_top;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingTop(p + sp_top);
	p = padding_bottom;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingBottom(p + sp_bottom);
	Element element;
	for (Iterator i = content.iterator(); i.hasNext();) {
		element = (Element) i.next();
		cell.addElement(element);
	}
	return cell;
}
 
Example 10
Source File: SimpleCell.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a PdfPCell with these attributes.
 * @param rowAttributes
 * @return a PdfPCell based on these attributes.
 */
public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
	PdfPCell cell = new PdfPCell();
	cell.setBorder(NO_BORDER);
	SimpleCell tmp = new SimpleCell(CELL);
	tmp.setSpacing_left(spacing_left);
	tmp.setSpacing_right(spacing_right);
	tmp.setSpacing_top(spacing_top);
	tmp.setSpacing_bottom(spacing_bottom);
	tmp.cloneNonPositionParameters(rowAttributes);
	tmp.softCloneNonPositionParameters(this);
	cell.setCellEvent(tmp);
	cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
	cell.setVerticalAlignment(rowAttributes.verticalAlignment);
	cell.setUseAscender(rowAttributes.useAscender);
	cell.setUseBorderPadding(rowAttributes.useBorderPadding);
	cell.setUseDescender(rowAttributes.useDescender);
	cell.setColspan(colspan);
	if (horizontalAlignment != Element.ALIGN_UNDEFINED)
		cell.setHorizontalAlignment(horizontalAlignment);
	if (verticalAlignment != Element.ALIGN_UNDEFINED)
		cell.setVerticalAlignment(verticalAlignment);
	if (useAscender)
		cell.setUseAscender(useAscender);
	if (useBorderPadding)
		cell.setUseBorderPadding(useBorderPadding);
	if (useDescender)
		cell.setUseDescender(useDescender);
	float p;
	float sp_left = spacing_left;
	if (Float.isNaN(sp_left)) sp_left = 0f;
	float sp_right = spacing_right;
	if (Float.isNaN(sp_right)) sp_right = 0f;
	float sp_top = spacing_top;
	if (Float.isNaN(sp_top)) sp_top = 0f;
	float sp_bottom = spacing_bottom;
	if (Float.isNaN(sp_bottom)) sp_bottom = 0f;
	p = padding_left;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingLeft(p + sp_left);
	p = padding_right;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingRight(p + sp_right);
	p = padding_top;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingTop(p + sp_top);
	p = padding_bottom;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingBottom(p + sp_bottom);
	Element element;
	for (Iterator i = content.iterator(); i.hasNext(); ) {
		element = (Element)i.next();
		cell.addElement(element);
	}
	return cell;
}
 
Example 11
Source File: ImageChunksTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Images wrapped in a Chunk.
 */
@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.getInstance(document, PdfTestBase.getOutputStream("imageChunks.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we create a table and add it to the document
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "pngnow.png");
	img.scalePercent(70);
	Chunk ck = new Chunk(img, 0, -5);
	PdfPTable table = new PdfPTable(3);
	PdfPCell cell = new PdfPCell();
	cell.addElement(new Chunk(img, 5, -5));
	cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see an image\non my right");
	table.addCell(cell);
	table.addCell("I see an image\non my left");
	table.addCell(cell);
	table.addCell("I see images\neverywhere");
	table.addCell(cell);
	table.addCell("I see an image\non my right");
	table.addCell(cell);
	table.addCell("I see an image\non my left");

	Phrase p1 = new Phrase("This is an image ");
	p1.add(ck);
	p1.add(" just here.");
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(table);

	// step 5: we close the document
	document.close();
}