Java Code Examples for com.lowagie.text.Chunk#setBackground()

The following examples show how to use com.lowagie.text.Chunk#setBackground() . 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: PdfWebTable.java    From unitime with Apache License 2.0 6 votes vote down vote up
private float addText(PdfPCell cell, String text, boolean bold, boolean italic, boolean underline, Color color, Color bgColor) {
	Font font = PdfFont.getFont(bold, italic, underline, color);
	Chunk chunk = new Chunk(text, font);
	if (bgColor!=null) chunk.setBackground(bgColor);
	if (cell.getPhrase()==null) {
	    cell.setPhrase(new Paragraph(chunk));
		cell.setVerticalAlignment(Element.ALIGN_TOP);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	} else {
		cell.getPhrase().add(chunk);
	}
	float width = 0; 
	if (text.indexOf('\n')>=0) {
		for (StringTokenizer s = new StringTokenizer(text,"\n"); s.hasMoreTokens();)
			width = Math.max(width,font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
	} else 
		width = Math.max(width,font.getBaseFont().getWidthPoint(text, font.getSize()));
	return width;
}
 
Example 2
Source File: LockboxServiceImpl.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void writeBatchGroupSectionTitle(com.lowagie.text.Document pdfDoc, String batchSeqNbr, java.sql.Date procInvDt, String cashControlDocNumber) {
    Font font = FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD);

    String lineText = "CASHCTL " + rightPad(cashControlDocNumber, 12) + " " +
    "BATCH GROUP: " + rightPad(batchSeqNbr, 5) + " " +
    rightPad((procInvDt == null ? "NONE" : procInvDt.toString()), 35);

    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(com.lowagie.text.Element.ALIGN_LEFT);
    Chunk chunk = new Chunk(lineText, font);
    chunk.setBackground(Color.LIGHT_GRAY, 5, 5, 5, 5);
    paragraph.add(chunk);

    //  blank line
    paragraph.add(new Chunk("", font));

    try {
        pdfDoc.add(paragraph);
    }
    catch (DocumentException e) {
        LOG.error("iText DocumentException thrown when trying to write content.", e);
        throw new RuntimeException("iText DocumentException thrown when trying to write content.", e);
    }
}
 
Example 3
Source File: CustomerLoadServiceImpl.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void writeFileNameSectionTitle(Document pdfDoc, String filenameLine) {
    Font font = FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD);

    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(Element.ALIGN_LEFT);
    Chunk chunk = new Chunk(filenameLine, font);
    chunk.setBackground(Color.LIGHT_GRAY, 5, 5, 5, 5);
    paragraph.add(chunk);

    //  blank line
    paragraph.add(new Chunk("", font));

    try {
        pdfDoc.add(paragraph);
    }
    catch (DocumentException e) {
        LOG.error("iText DocumentException thrown when trying to write content.", e);
        throw new RuntimeException("iText DocumentException thrown when trying to write content.", e);
    }
}
 
Example 4
Source File: RTFTextExtractor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void add( final String text ) {
  int style = Font.NORMAL;
  if ( bold ) {
    style |= Font.BOLD;
  }
  if ( italic ) {
    style |= Font.ITALIC;
  }
  if ( strikethrough ) {
    style |= Font.STRIKETHRU;
  }
  if ( underline ) {
    style |= Font.UNDERLINE;
  }

  final BaseFontFontMetrics fontMetrics =
      metaData.getBaseFontFontMetrics( fontName, fontSize, bold, italic, "utf-8", false, false );
  final BaseFont baseFont = fontMetrics.getBaseFont();
  final Font font = new Font( baseFont, (float) fontSize, style, textColor );
  final Chunk c = new Chunk( text, font );
  if ( backgroundColor != null ) {
    c.setBackground( backgroundColor );
  }
  target.add( c );
}
 
Example 5
Source File: DocStyleUtils.java    From DWSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
/** 
 * 功能说明:为文字填充浅灰色背景</BR> 
 * 修改日期:2011-04-27 
 * @author myclover 
 * @param content   需要填充背景颜色的内容 
 * @param appendStr 不需要填充背景颜色的内容 
 * @return 
 */  
private static Phrase setPhraseStyle(String content , String appendStr){  
    Chunk chunk = new Chunk(content);  
    //填充的背景颜色为浅灰色  
    chunk.setBackground(Color.LIGHT_GRAY);  
    Phrase phrase = new Phrase(chunk);  
    phrase.add(appendStr);  
    return phrase;  
}
 
Example 6
Source File: ChunksTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates some Chunk functionality.
 */
@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
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Chunks.pdf"));

	// step 3: we open the document
	document.open();
	// step 4:
	Chunk fox = new Chunk("quick brown fox");
	float superscript = 8.0f;
	fox.setTextRise(superscript);
	fox.setBackground(new Color(0xFF, 0xDE, 0xAD));
	Chunk jumps = new Chunk(" jumps over ");
	Chunk dog = new Chunk("the lazy dog");
	float subscript = -8.0f;
	dog.setTextRise(subscript);
	dog.setUnderline(new Color(0xFF, 0x00, 0x00), 3.0f, 0.0f, -5.0f + subscript, 0.0f,
			PdfContentByte.LINE_CAP_ROUND);
	document.add(fox);
	document.add(jumps);
	document.add(dog);

	// step 5: we close the document
	document.close();
}
 
Example 7
Source File: CustomerInvoiceWriteoffBatchServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void writeFileNameSectionTitle(com.lowagie.text.Document pdfDoc, String filenameLine) {
    Font font = FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD);

    //  file name title, get title only, on windows & unix platforms
    String fileNameOnly = filenameLine.toUpperCase();
    int indexOfSlashes = fileNameOnly.lastIndexOf("\\");
    if (indexOfSlashes < fileNameOnly.length()) {
        fileNameOnly = fileNameOnly.substring(indexOfSlashes + 1);
    }
    indexOfSlashes = fileNameOnly.lastIndexOf("/");
    if (indexOfSlashes < fileNameOnly.length()) {
        fileNameOnly = fileNameOnly.substring(indexOfSlashes + 1);
    }

    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(com.lowagie.text.Element.ALIGN_LEFT);
    Chunk chunk = new Chunk(fileNameOnly, font);
    chunk.setBackground(Color.LIGHT_GRAY, 5, 5, 5, 5);
    paragraph.add(chunk);

    //  blank line
    paragraph.add(new Chunk("", font));

    try {
        pdfDoc.add(paragraph);
    }
    catch (DocumentException e) {
        LOG.error("iText DocumentException thrown when trying to write content.", e);
        throw new RuntimeException("iText DocumentException thrown when trying to write content.", e);
    }
}
 
Example 8
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a Chunk object based on a list of properties.
 * 
 * @param attributes
 * @return a Chunk
 */
public static Chunk getChunk(Properties attributes) {
	Chunk chunk = new Chunk();

	chunk.setFont(FontFactory.getFont(attributes));
	String value;

	value = attributes.getProperty(ElementTags.ITEXT);
	if (value != null) {
		chunk.append(value);
	}
	value = attributes.getProperty(ElementTags.LOCALGOTO);
	if (value != null) {
		chunk.setLocalGoto(value);
	}
	value = attributes.getProperty(ElementTags.REMOTEGOTO);
	if (value != null) {
		String page = attributes.getProperty(ElementTags.PAGE);
		if (page != null) {
			chunk.setRemoteGoto(value, Integer.parseInt(page));
		} else {
			String destination = attributes.getProperty(ElementTags.DESTINATION);
			if (destination != null) {
				chunk.setRemoteGoto(value, destination);
			}
		}
	}
	value = attributes.getProperty(ElementTags.LOCALDESTINATION);
	if (value != null) {
		chunk.setLocalDestination(value);
	}
	value = attributes.getProperty(ElementTags.SUBSUPSCRIPT);
	if (value != null) {
		chunk.setTextRise(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(Markup.CSS_KEY_VERTICALALIGN);
	if (value != null && value.endsWith("%")) {
		float p = Float.parseFloat(value.substring(0, value.length() - 1) + "f") / 100f;
		chunk.setTextRise(p * chunk.getFont().getSize());
	}
	value = attributes.getProperty(ElementTags.GENERICTAG);
	if (value != null) {
		chunk.setGenericTag(value);
	}
	value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);
	if (value != null) {
		chunk.setBackground(Markup.decodeColor(value));
	}
	return chunk;
}
 
Example 9
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a Chunk object based on a list of properties.
 * @param attributes
 * @return a Chunk
 */
public static Chunk getChunk(Properties attributes) {
	Chunk chunk = new Chunk();

	chunk.setFont(FontFactory.getFont(attributes));
	String value;

	value = attributes.getProperty(ElementTags.ITEXT);
	if (value != null) {
		chunk.append(value);
	}
	value = attributes.getProperty(ElementTags.LOCALGOTO);
	if (value != null) {
		chunk.setLocalGoto(value);
	}
	value = attributes.getProperty(ElementTags.REMOTEGOTO);
	if (value != null) {
		String page = attributes.getProperty(ElementTags.PAGE);
		if (page != null) {
			chunk.setRemoteGoto(value, Integer.parseInt(page));
		} else {
			String destination = attributes
					.getProperty(ElementTags.DESTINATION);
			if (destination != null) {
				chunk.setRemoteGoto(value, destination);
			}
		}
	}
	value = attributes.getProperty(ElementTags.LOCALDESTINATION);
	if (value != null) {
		chunk.setLocalDestination(value);
	}
	value = attributes.getProperty(ElementTags.SUBSUPSCRIPT);
	if (value != null) {
		chunk.setTextRise(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(Markup.CSS_KEY_VERTICALALIGN);
	if (value != null && value.endsWith("%")) {
		float p = Float.parseFloat(value.substring(0, value.length() - 1)
				+ "f") / 100f;
		chunk.setTextRise(p * chunk.getFont().getSize());
	}
	value = attributes.getProperty(ElementTags.GENERICTAG);
	if (value != null) {
		chunk.setGenericTag(value);
	}
	value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);
	if (value != null) {
		chunk.setBackground(Markup.decodeColor(value));
	}
	return chunk;
}
 
Example 10
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
protected Chunk getChunk(Map<Attribute,Object> attributes, String text, Locale locale)
{
	// underline and strikethrough are set on the chunk below
	Font font = getFont(attributes, locale, false);

	Chunk chunk = new Chunk(text, font);
	
	if (hasUnderline(attributes))
	{
		// using the same values as sun.font.Fond2D
		chunk.setUnderline(null, 0, 1f / 18, 0, -1f / 12, 0);
	}
	
	if (hasStrikethrough(attributes))
	{
		// using the same thickness as sun.font.Fond2D.
		// the position is calculated in Fond2D based on the ascent, defaulting 
		// to iText default position which depends on the font size
		chunk.setUnderline(null, 0, 1f / 18, 0, 1f / 3, 0);
	}

	Color backcolor = (Color)attributes.get(TextAttribute.BACKGROUND);
	if (backcolor != null)
	{
		chunk.setBackground(backcolor);
	}

	Object script = attributes.get(TextAttribute.SUPERSCRIPT);
	if (script != null)
	{
		if (TextAttribute.SUPERSCRIPT_SUPER.equals(script))
		{
			chunk.setTextRise(font.getCalculatedLeading(1f)/2);
		}
		else if (TextAttribute.SUPERSCRIPT_SUB.equals(script))
		{
			chunk.setTextRise(-font.getCalculatedLeading(1f)/2);
		}
	}

	if (splitCharacter != null)
	{
		//TODO use line break offsets if available?
		chunk.setSplitCharacter(splitCharacter);
	}

	return chunk;
}