Java Code Examples for com.lowagie.text.Font#NORMAL

The following examples show how to use com.lowagie.text.Font#NORMAL . 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: OptionalContentTest.java    From itext2 with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * Demonstrates the use of layers.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("optionalcontent.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3: opening the document
	document.open();
	// step 4: content
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase(
			"Automatic layers, form fields, images, templates and actions",
			new Font(Font.HELVETICA, 18, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
	PdfLayerMembership m1 = new PdfLayerMembership(writer);
	m1.addMember(l2);
	m1.addMember(l3);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2 or layer 3");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f);
	cb.endLayer();
	cb.beginLayer(m1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620),
			"field1");
	ff.setBorderColor(Color.blue);
	ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
	ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
	ff.setText("I'm a form field");
	PdfFormField form = ff.getTextField();
	form.setLayer(l4);
	writer.addAnnotation(form);
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
			+ "pngnow.png");
	img.setLayer(l4);
	img.setAbsolutePosition(200, 550);
	cb.addImage(img);
	PdfTemplate tp = cb.createTemplate(100, 20);
	Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12,
			Font.NORMAL, Color.magenta));
	ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0);
	tp.setLayer(l4);
	tp.setBoundingBox(new Rectangle(0, -10, 100, 20));
	cb.addTemplate(tp, 200, 500);
	ArrayList<Object> state = new ArrayList<Object>();
	state.add("toggle");
	state.add(l1);
	state.add(l2);
	state.add(l3);
	state.add(l4);
	PdfAction action = PdfAction.setOCGstate(state, true);
	Chunk ck = new Chunk("Click here to toggle the layers", new Font(
			Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground(
			Color.blue).setAction(action);
	ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck),
			250, 400, 0);
	cb.sanityCheck();

	// step 5: closing the document
	document.close();
}
 
Example 2
Source File: FontEncodingTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
    * Specifying an encoding.
    */
@Test
   public void main() throws Exception {
       
       
       // step 1: creation of a document-object
       Document document = new Document();
       
           
           // step 2: creation of the writer
           PdfWriter.getInstance(document, PdfTestBase.getOutputStream("fontencoding.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4: we add content to the document
           BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
           Font font = new Font(helvetica, 12, Font.NORMAL);
           Chunk chunk = new Chunk("Sponsor this example and send me 1\u20ac. These are some special characters: \u0152\u0153\u0160\u0161\u0178\u017D\u0192\u02DC\u2020\u2021\u2030", font);
           document.add(chunk);
       
       // step 5: we close the document
       document.close();
   }
 
Example 3
Source File: PropertyUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public static int getFontStyle( String fontStyle, String fontWeight )
{
	int styleValue = Font.NORMAL;

	if ( CSSConstants.CSS_OBLIQUE_VALUE.equals( fontStyle )
			|| CSSConstants.CSS_ITALIC_VALUE.equals( fontStyle ) )
	{
		styleValue |= Font.ITALIC;
	}

	if ( CSSConstants.CSS_BOLD_VALUE.equals( fontWeight )
			|| CSSConstants.CSS_BOLDER_VALUE.equals( fontWeight )
			|| CSSConstants.CSS_600_VALUE.equals( fontWeight )
			|| CSSConstants.CSS_700_VALUE.equals( fontWeight )
			|| CSSConstants.CSS_800_VALUE.equals( fontWeight )
			|| CSSConstants.CSS_900_VALUE.equals( fontWeight ) )
	{
		styleValue |= Font.BOLD;
	}
	return styleValue;
}
 
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: StandardType1FontsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Generates a PDF file with the 14 standard Type 1 Fonts
 * 
 */
@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("StandardType1Fonts.pdf"));

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

	// the 14 standard fonts in PDF: do not use this Font constructor!
	// this is for demonstration purposes only, use FontFactory!
	Font[] fonts = new Font[14];
	fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[1] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[2] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD);
	fonts[3] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC);
	fonts[4] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[5] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[6] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
	fonts[7] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC);
	fonts[8] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[9] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[10] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD);
	fonts[11] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC);
	fonts[12] = new Font(Font.SYMBOL);
	fonts[13] = new Font(Font.ZAPFDINGBATS);
	// add the content
	for (int i = 0; i < 14; i++) {
		document.add(new Paragraph("quick brown fox jumps over the lazy dog", fonts[i]));
	}

	// step 5: we close the document
	document.close();
}
 
Example 6
Source File: FontHandler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * If the BaseFont can NOT find the correct physical glyph, we need to
 * simulate the proper style for the font. The "simulate" flag will be set
 * if we need to simulate it.
 */
private boolean needSimulate( BaseFont font )
{
	if ( fontStyle == Font.NORMAL )
	{
		return false;
	}

	String[][] fullNames = bf.getFullFontName( );
	String fullName = getEnglishName( fullNames );
	String lcf = fullName.toLowerCase( );

	int fs = Font.NORMAL;
	if ( lcf.indexOf( "bold" ) != -1 )
	{
		fs |= Font.BOLD;
	}
	if ( lcf.indexOf( "italic" ) != -1 || lcf.indexOf( "oblique" ) != -1 )
	{
		fs |= Font.ITALIC;
	}
	if ( ( fontStyle & Font.BOLDITALIC ) == fs )
	{
		if ( fontWeight > 400 && fontWeight != 700 )
		{
			// not a regular bold font.
			return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}
 
Example 7
Source File: FontConfigReaderTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isMappedTo( char c, String from, String to )
{
	FontHandler handler = new FontHandler( fontMappingManager,
			new String[]{from}, Font.NORMAL, true );
	BaseFont font = handler.getMappedFont( c );
	return hasName( font, to );
}
 
Example 8
Source File: HtmlWriter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Writes the representation of a <CODE>Font</CODE>.
 *
 * @param font a <CODE>Font</CODE>
 * @param styleAttributes the style of the font
 * @throws IOException
 */

protected void write(Font font, Properties styleAttributes) throws IOException {
	if (font == null || !isOtherFont(font) /* || styleAttributes == null*/) {
		return;
	}
	write(" ");
	write(HtmlTags.STYLE);
	write("=\"");
	if (styleAttributes != null) {
		String key;
		for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements();) {
			key = (String) e.nextElement();
			writeCssProperty(key, styleAttributes.getProperty(key));
		}
	}
	if (isOtherFont(font)) {
		writeCssProperty(Markup.CSS_KEY_FONTFAMILY, font.getFamilyname());

		if (font.getSize() != Font.UNDEFINED) {
			writeCssProperty(Markup.CSS_KEY_FONTSIZE, font.getSize() + "pt");
		}
		if (font.getColor() != null) {
			writeCssProperty(Markup.CSS_KEY_COLOR, HtmlEncoder.encode(font.getColor()));
		}

		int fontstyle = font.getStyle();
		BaseFont bf = font.getBaseFont();
		if (bf != null) {
			String ps = bf.getPostscriptFontName().toLowerCase();
			if (ps.indexOf("bold") >= 0) {
				if (fontstyle == Font.UNDEFINED) {
					fontstyle = 0;
				}
				fontstyle |= Font.BOLD;
			}
			if (ps.indexOf("italic") >= 0 || ps.indexOf("oblique") >= 0) {
				if (fontstyle == Font.UNDEFINED) {
					fontstyle = 0;
				}
				fontstyle |= Font.ITALIC;
			}
		}
		if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) {
			switch (fontstyle & Font.BOLDITALIC) {
				case Font.BOLD:
					writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
					break;
				case Font.ITALIC:
					writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
					break;
				case Font.BOLDITALIC:
					writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
					writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
					break;
			}

			// CSS only supports one decoration tag so if both are specified
			// only one of the two will display
			if ((fontstyle & Font.UNDERLINE) > 0) {
				writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_UNDERLINE);
			}
			if ((fontstyle & Font.STRIKETHRU) > 0) {
				writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_LINETHROUGH);
			}
		}
	}
	write("\"");
}
 
Example 9
Source File: RtfDestinationDocument.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void beforePropertyChange(String propertyName) {
	// do we have any text to do anything with?
	// if not, then just return without action.
	if(this.buffer.length() == 0) return;
	
	if(propertyName.startsWith(RtfProperty.CHARACTER)) {
		// this is a character change,
		// add a new chunk to the current paragraph using current character settings.
		Chunk chunk = new Chunk();
		chunk.append(this.buffer.toString());
		this.buffer = new StringBuffer(255);
		HashMap charProperties = this.rtfParser.getState().properties.getProperties(RtfProperty.CHARACTER);
		String defFont = (String)charProperties.get(RtfProperty.CHARACTER_FONT);
		if(defFont == null) defFont = "0";
		RtfDestinationFontTable fontTable = (RtfDestinationFontTable)this.rtfParser.getDestination("fonttbl");
		Font currFont = fontTable.getFont(defFont);
		int fs = Font.NORMAL;
		if(charProperties.containsKey(RtfProperty.CHARACTER_BOLD)) fs |= Font.BOLD; 
		if(charProperties.containsKey(RtfProperty.CHARACTER_ITALIC)) fs |= Font.ITALIC;
		if(charProperties.containsKey(RtfProperty.CHARACTER_UNDERLINE)) fs |= Font.UNDERLINE;
		Font useFont = FontFactory.getFont(currFont.getFamilyname(), 12, fs, new Color(0,0,0));
		
		
		chunk.setFont(useFont);
		if(iTextParagraph == null) this.iTextParagraph = new Paragraph();
		this.iTextParagraph.add(chunk);

	} else {
		if(propertyName.startsWith(RtfProperty.PARAGRAPH)) {
			// this is a paragraph change. what do we do?
		} else {
			if(propertyName.startsWith(RtfProperty.SECTION)) {
				
			} else {
				if(propertyName.startsWith(RtfProperty.DOCUMENT)) {

				}
			}
		}
	}		
}
 
Example 10
Source File: HtmlWriter.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes the representation of a <CODE>Font</CODE>.
 *
 * @param font              a <CODE>Font</CODE>
 * @param styleAttributes   the style of the font
 * @throws IOException
 */

protected void write(Font font, Properties styleAttributes) throws IOException {
    if (font == null || !isOtherFont(font) /* || styleAttributes == null*/) return;
    write(" ");
    write(HtmlTags.STYLE);
    write("=\"");
    if (styleAttributes != null) {
        String key;
        for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements(); ) {
            key = (String)e.nextElement();
            writeCssProperty(key, styleAttributes.getProperty(key));
        }
    }
    if (isOtherFont(font)) {
        writeCssProperty(Markup.CSS_KEY_FONTFAMILY, font.getFamilyname());
        
        if (font.getSize() != Font.UNDEFINED) {
            writeCssProperty(Markup.CSS_KEY_FONTSIZE, font.getSize() + "pt");
        }
        if (font.getColor() != null) {
            writeCssProperty(Markup.CSS_KEY_COLOR, HtmlEncoder.encode(font.getColor()));
        }
        
        int fontstyle = font.getStyle();
        BaseFont bf = font.getBaseFont();
        if (bf != null) {
            String ps = bf.getPostscriptFontName().toLowerCase();
            if (ps.indexOf("bold") >= 0) {
                if (fontstyle == Font.UNDEFINED)
                    fontstyle = 0;
                fontstyle |= Font.BOLD;
            }
            if (ps.indexOf("italic") >= 0 || ps.indexOf("oblique") >= 0) {
                if (fontstyle == Font.UNDEFINED)
                    fontstyle = 0;
                fontstyle |= Font.ITALIC;
            }
        }
        if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) {
            switch (fontstyle & Font.BOLDITALIC) {
                case Font.BOLD:
                    writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
                    break;
                case Font.ITALIC:
                    writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
                    break;
                case Font.BOLDITALIC:
                    writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
                    writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
                    break;
            }
            
            // CSS only supports one decoration tag so if both are specified
            // only one of the two will display
            if ((fontstyle & Font.UNDERLINE) > 0) {
                writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_UNDERLINE);
            }
            if ((fontstyle & Font.STRIKETHRU) > 0) {
                writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_LINETHROUGH);
            }
        }
    }
    write("\"");
}
 
Example 11
Source File: ChineseJapaneseKoreanTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Using CJK fonts
 * @param args no arguments needed
 */
@Test

public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document();
           
           // step 2: creation of the writer
           PdfWriter.getInstance(document, PdfTestBase.getOutputStream("cjk.pdf"));
           
           // step 3: we open the document
           document.open();
           String chinese = "\u53d6\u6e96\u53d7\u4fdd\u4eba\u5728\u6211\u56fd\u7ecf\u6d4e\u7ed3\u6784"
           + "\u8fdb\u884c\u6218\u7565\u6027\u8c03\u6574\u7684"
           + "\u80CC\u666F\u4e0B\uff0c\u4FE1\u606f\u4ea7\u4e1a"
           + "\u5c06\u6210\u4E3A\u62c9\u52A8\u7ecf\u6d4e\u589e"
           + "\u957f\u7684\u65b0\u52A8\u529b\uff0c\u800c\u4f5c"
           + "\u4E3A\u4FE1\u606f\u6280\u672f\u6838\u5fc3\u4e4b"
           + "\u4e00\u7684\u8f6f\u4ef6\u4ea7\u4e1a\u5fc5\u7136"
           + "\u6210\u4E3A\u4e4a\u540e\u56fd\u5bb6\u4ea7\u4e1a"
           + "\u54d1\u5c55\u7684\u6218\u7565\u91cd\u70b9\uff0c"
           + "\u6211\u56fd\u7684\u8f6f\u4ef6\u4ea7\u4e1a\u5c06"
           + "\u4f1a\u5f97\u5230\u98de\u901f\u7684\u54d1\u5c55"
           + "\u3002\u540c\u65f6\u4F34\u968f\u7740\u6211\u56fd"
           + "\u52a0\u5165\u4e16\u8d38\u7ec4\u7ec7\uff0c\u8de8"
           + "\u56fd\u4f01\u4e1a\u4e5f\u5c06\u5927\u89c4\uc4a3"
           + "\u8fdb\u9a7b\u4e2d\u56fd\u8f6f\u4ef6\ucad0\u573a"
           + "\u3002\u56e0\u6b64\uff0c\u9ad8\u7d20\u8d28\u5f0c"
           + "\u4ef6\u4eba\u624d\u7684\u7ade\u4e89\ucac6\u5fc5"
           + "\uc8d5\uc7f7\ubca4\uc1d2\u3002\u800c\u4e4b\u6b64"
           + "\u7678\u5bf9\u5e94\u7684\u662f\u76ee\u524d\u6211"
           + "\u56fd\u5bf9\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b"
           + "\u7684\u529b\u5ea6\u4e0d\u591f\uff0c\u6240\u80fd"
           + "\u63d0\u4f9b\u4e13\u4e1a\u4eba\u624d\u8fdc\u8fdc"
           + "\u6ee1\u8db3\u4e0d\u4e86\u5e02\u573a\u7684\u9700"
           + "\u6c42\u3002\u56e0\u6b64\u4ee5\u5e02\u573a\u9700"
           + "\u6c42\u4E3A\u7740\u773c\u70b9\uff0c\u6539\u9769"
           + "\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b\u7684\u6a21"
           + "\u5f0f\uff0c\u52a0\u5927\u5f0c\u4ef6\u4eba\u624d"
           + "\u57f9\u517b\u7684\u529b\u5ea6\uff0c\u5b9e\u73b0"
           + "\u6211\u56fd\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b"
           + "\u7684\u5d88\u8d8a\u5f0f\u54d1\u5c55\uff0c\u5df2"
           + "\u7ecf\u6210\u4E3A\u5f53\u524d\u6c0a\u7b49\u6559"
           + "\u80b2\u6539\u9769\u4e0e\u54d1\u5c55\u7684\u4e00"
           + "\u9879\u6781\u5176\u91cd\u8981\u800c\u4e14\u7678"
           + "\u5f53\u7d27\u8feb\u7684\u4efb\u52a1\u3002";
           
           // step 4: we add content to the document
           BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
           Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
           Paragraph p = new Paragraph(chinese, FontChinese);
           document.add(p);
   
       
       // step 5: we close the document
       document.close();
}
 
Example 12
Source File: ColumnSimpleTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrating the use of ColumnText
 */
@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 writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("columnsimple.pdf"));

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

	// step 4:

	// we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.addText(new Phrase(15, "UNI\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, uni[i] + "\n", font));
	}
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.addText(new Phrase(15, "char\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, code[i] + "\n", font));
	}
	ct.go();
	ct.setSimpleColumn(160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.addText(new Phrase(15, "NAME" + "\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, name[i] + "\n", font));
	}
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 13
Source File: ColumnTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrating the use of ColumnText
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("column.pdf"));

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

	// step 4:

	// we create some content
	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	Phrase unicodes = new Phrase(15, "UNI\n", font);
	Phrase characters = new Phrase(15, "\n", font);
	Phrase names = new Phrase(15, "NAME\n", font);

	for (int i = 0; i < 27; i++) {
		unicodes.add(uni[i] + "\n");
		characters.add(code[i] + "\n");
		names.add(name[i] + "\n");
	}

	// we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(unicodes, 60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(characters, 105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.go();
	ct.setSimpleColumn(names, 160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 14
Source File: ExtendingStylesheetsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creation of new paragraph stylesheets.
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2 writer = RtfWriter2.getInstance(document,PdfTestBase.getOutputStream("ExtendingStylesheets.rtf"));

	// Create the new RtfParagraphStyle. The second parameter is the name of
	// the RtfParagraphStyle that this style will inherit default properties
	// from.
	RtfParagraphStyle incorrectStyle = new RtfParagraphStyle("Incorrect", "Normal");
	// Change the desired properties
	incorrectStyle.setColor(Color.RED);
	incorrectStyle.setStyle(Font.STRIKETHRU);
	// Register the new paragraph stylesheet with the RtfWriter2.
	writer.getDocumentSettings().registerParagraphStyle(incorrectStyle);

	// Create a new RtfParagraphStyle that does not inherit from any other
	// style.
	RtfParagraphStyle correctStyle = new RtfParagraphStyle("Correct", "Arial", 12, Font.NORMAL, Color.GREEN);
	// Register the new paragraph stylesheet with the RtfWriter2.
	writer.getDocumentSettings().registerParagraphStyle(correctStyle);

	// Change the default font name. This will propagate to the paragraph
	// stylesheet
	// that inherits, but not the other one.
	RtfParagraphStyle.STYLE_NORMAL.setFontName("Times New Roman");

	document.open();

	// Simply set the stylesheet you wish to use as the Font
	// of the Paragraph
	document.add(new Paragraph("This is a heading level 1", RtfParagraphStyle.STYLE_HEADING_1));
	document.add(new Paragraph("This is a heading level 2", RtfParagraphStyle.STYLE_HEADING_2));
	document.add(new Paragraph("Just some text that is formatted " + "in the default style.",
			RtfParagraphStyle.STYLE_NORMAL));
	document.add(new Paragraph("This paragraph should be removed.", incorrectStyle));
	document.add(new Paragraph("It should be replaced with this.", correctStyle));

	document.close();

}
 
Example 15
Source File: TableOfContentsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Demonstrates creating and styling a table of contents
    * 
    * 
    */
@Test
   public void main() throws Exception {
           Document document = new Document();
           RtfWriter2 rtfWriter2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("TableOfContents.rtf"));

           // Create paragraph stylesheets for each heading level. They must be named
           // "toc N" for each heading level you are using
           RtfParagraphStyle tocLevel1Style = new RtfParagraphStyle("toc 1",
                   "Times New Roman", 11, Font.NORMAL, Color.BLACK);
           RtfParagraphStyle tocLevel2Style = new RtfParagraphStyle("toc 2",
                   "Times New Roman", 10, Font.NORMAL, Color.BLACK);
           tocLevel2Style.setIndentLeft(10);
           
           // Register the paragraph stylesheets with the RtfWriter2
           rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel1Style);
           rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel2Style);
           
           document.open();

           // Create a Paragraph and add the table of contents to it
           Paragraph par = new Paragraph();
           par.add(new RtfTableOfContents("Right-click here and select \"Update\" " +
                   "to see the table of contents."));
           document.add(par);
           
           for(int i = 1; i <= 5; i++) {
               // Create a level 1 heading
               document.add(new Paragraph("Heading " + i, RtfParagraphStyle.STYLE_HEADING_1));
               for(int j = 1; j <= 3; j++) {
                   // Create a level 2 heading
                   document.add(new Paragraph("Heading " + i + "." + j, RtfParagraphStyle.STYLE_HEADING_2));
                   for(int k = 1; k <= 20; k++) {
                       document.add(new Paragraph("Line " + k + " in section " + i + "." + k));
                   }
               }
           }
           
           document.close();
     
   }
 
Example 16
Source File: MarkupParser.java    From MesquiteCore with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Retrieves a font from the FontFactory based on some style attributes.
 * Looks for the font-family, font-size, font-weight, font-style and color.
 * Takes the default encoding and embedded value.
 * @param styleAttributes a Properties object containing keys and values
 * @return an iText Font object
 */
    
public Font retrieveFont(Properties styleAttributes) {
    String fontname = null;
    String encoding = FontFactory.defaultEncoding;
    boolean embedded = FontFactory.defaultEmbedding;
    float size = Font.UNDEFINED;
    int style = Font.NORMAL;
    Color color = null;
    String value = (String)styleAttributes.get(MarkupTags.CSS_KEY_FONTFAMILY);
    if (value != null) {
    	if (value.indexOf(",") == -1) {
    		fontname = value.trim();
    	}
        else {
        	String tmp;
        	while (value.indexOf(",") != -1) {
        		tmp = value.substring(0, value.indexOf(",")).trim();
        		if (FontFactory.isRegistered(tmp)) {
        			fontname = tmp;
        			break;
        		}
        		else {
        			value = value.substring(value.indexOf(",") + 1);
        		}
        	}
        }
    }
    if ((value = (String)styleAttributes.get(MarkupTags.CSS_KEY_FONTSIZE)) != null) {
         size = MarkupParser.parseLength(value);
    }
    if ((value = (String)styleAttributes.get(MarkupTags.CSS_KEY_FONTWEIGHT)) != null) {
        style |= Font.getStyleValue(value);
    }
    if ((value = (String)styleAttributes.get(MarkupTags.CSS_KEY_FONTSTYLE)) != null) {
        style |= Font.getStyleValue(value);
    }
    if ((value = (String)styleAttributes.get(MarkupTags.CSS_KEY_COLOR)) != null) {
        color = MarkupParser.decodeColor(value);
    }
    return FontFactory.getFont(fontname, encoding, embedded, size, style, color);
}