⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elementfactory.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: ElementFactory.java 2775 2007-05-23 09:12:39Z blowagie $
 * $Name$
 *
 * Copyright 2007 by Bruno Lowagie.
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library 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. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */
package com.lowagie.text.factories;

import java.awt.Color;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import com.lowagie.text.Anchor;
import com.lowagie.text.Annotation;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.ChapterAutoNumber;
import com.lowagie.text.Chunk;
import com.lowagie.text.ElementTags;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.Utilities;
import com.lowagie.text.html.Markup;

/**
 * This class is able to create Element objects based on a list of properties.
 */

public class ElementFactory {

	/**
	 * 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;
	}

	/**
	 * Creates a Phrase object based on a list of properties.
	 * @param attributes
	 * @return a Phrase
	 */
	public static Phrase getPhrase(Properties attributes) {
		Phrase phrase = new Phrase();
		phrase.setFont(FontFactory.getFont(attributes));
        String value;
        value = attributes.getProperty(ElementTags.LEADING);
        if (value != null) {
            phrase.setLeading(Float.parseFloat(value + "f"));
        }
        value = attributes.getProperty(Markup.CSS_KEY_LINEHEIGHT);
        if (value != null) {
            phrase.setLeading(Markup.parseLength(value));
        }
        value = attributes.getProperty(ElementTags.ITEXT);
        if (value != null) {
            Chunk chunk = new Chunk(value);
            if ((value = attributes.getProperty(ElementTags.GENERICTAG)) != null) {
                chunk.setGenericTag(value);
            }
            phrase.add(chunk);
        }
        return phrase;
	}

	/**
	 * Creates an Anchor object based on a list of properties.
	 * @param attributes
	 * @return an Anchor
	 */
	public static Anchor getAnchor(Properties attributes) {
		Anchor anchor = new Anchor(getPhrase(attributes));
		String value;
        value = attributes.getProperty(ElementTags.NAME);
        if (value != null) {
            anchor.setName(value);
        }
        value = (String)attributes.remove(ElementTags.REFERENCE);
        if (value != null) {
            anchor.setReference(value);
        }
		return anchor;
	}

	/**
	 * Creates a Paragraph object based on a list of properties.
	 * @param attributes
	 * @return a Paragraph
	 */
	public static Paragraph getParagraph(Properties attributes) {
		Paragraph paragraph = new Paragraph(getPhrase(attributes));
        String value;
        value = attributes.getProperty(ElementTags.ALIGN);
        if (value != null) {
            paragraph.setAlignment(value);
        }
        value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
        if (value != null) {
            paragraph.setIndentationLeft(Float.parseFloat(value + "f"));
        }
        value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
        if (value != null) {
            paragraph.setIndentationRight(Float.parseFloat(value + "f"));
        }
		return paragraph;
	}

	/**
	 * Creates a ListItem object based on a list of properties.
	 * @param attributes
	 * @return a ListItem
	 */
	public static ListItem getListItem(Properties attributes) {
		ListItem item = new ListItem(getParagraph(attributes));
		return item;
	}

	/**
	 * Creates a List object based on a list of properties.
	 * @param attributes
	 * @return the List
	 */
	public static List getList(Properties attributes) {
		List list = new List();

		list.setNumbered(Utilities.checkTrueOrFalse(attributes, ElementTags.NUMBERED));
		list.setLettered(Utilities.checkTrueOrFalse(attributes, ElementTags.LETTERED));
		list.setLowercase(Utilities.checkTrueOrFalse(attributes, ElementTags.LOWERCASE));
		list.setAutoindent(Utilities.checkTrueOrFalse(attributes, ElementTags.AUTO_INDENT_ITEMS));
		list.setAlignindent(Utilities.checkTrueOrFalse(attributes, ElementTags.ALIGN_INDENTATION_ITEMS));
		
		String value;
		
        value = attributes.getProperty(ElementTags.FIRST);
        if (value != null) {
            char character = value.charAt(0);
            if (Character.isLetter(character) ) {
                list.setFirst(character);
            }
            else {
                list.setFirst(Integer.parseInt(value));
            }
        }
        
		value= attributes.getProperty(ElementTags.LISTSYMBOL);
		if (value != null) {
			list.setListSymbol(new Chunk(value, FontFactory.getFont(attributes)));
		}
        
        value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
        if (value != null) {
            list.setIndentationLeft(Float.parseFloat(value + "f"));
        }
        
        value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
        if (value != null) {
            list.setIndentationRight(Float.parseFloat(value + "f"));
        }
        
        value = attributes.getProperty(ElementTags.SYMBOLINDENT);
        if (value != null) {
            list.setSymbolIndent(Float.parseFloat(value));
        }
        
		return list;
	}

	/**
	 * Creates a Cell object based on a list of properties.
	 * @param attributes
	 * @return a Cell
	 */
	public static Cell getCell(Properties attributes) {
		Cell cell = new Cell();
		String value;

		cell.setHorizontalAlignment(attributes.getProperty(ElementTags.HORIZONTALALIGN));
		cell.setVerticalAlignment(attributes.getProperty(ElementTags.VERTICALALIGN));
		
		value = attributes.getProperty(ElementTags.WIDTH);
		if (value != null) {
			cell.setWidth(value);
		}
		value = attributes.getProperty(ElementTags.COLSPAN);
		if (value != null) {
			cell.setColspan(Integer.parseInt(value));
		}
		value = attributes.getProperty(ElementTags.ROWSPAN);
		if (value != null) {
			cell.setRowspan(Integer.parseInt(value));
		}
		value = attributes.getProperty(ElementTags.LEADING);
		if (value != null) {
			cell.setLeading(Float.parseFloat(value + "f"));
		}
		cell.setHeader(Utilities.checkTrueOrFalse(attributes, ElementTags.HEADER));
		if (Utilities.checkTrueOrFalse(attributes, ElementTags.NOWRAP)) {
			cell.setMaxLines(1);
		}
		setRectangleProperties(cell, attributes);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -