elementfactory.java

来自「有关对pdf操作的代码」· Java 代码 · 共 608 行 · 第 1/2 页

JAVA
608
字号
		return cell;	}		/**	 * Creates an Table object based on a list of properties.	 * @param attributes	 * @return a Table	 */	public static Table getTable(Properties attributes) {		String value;		Table table;		try {			value = attributes.getProperty(ElementTags.WIDTHS);			if (value != null) {				StringTokenizer widthTokens = new StringTokenizer(value, ";");				ArrayList values = new ArrayList();				while (widthTokens.hasMoreTokens()) {					values.add(widthTokens.nextToken());				}				table = new Table(values.size());		        float[] widths = new float[table.getColumns()];				for (int i = 0; i < values.size(); i++) {					value = (String)values.get(i);					widths[i] = Float.parseFloat(value + "f");				}				table.setWidths(widths);			}			else {				value = attributes.getProperty(ElementTags.COLUMNS);				try {					table = new Table(Integer.parseInt(value));				}				catch(Exception e) {					table = new Table(1);				}			}				        table.setBorder(Table.BOX);	        table.setBorderWidth(1);	        table.getDefaultCell().setBorder(Table.BOX);	        	        value = attributes.getProperty(ElementTags.LASTHEADERROW);	        if (value != null) {	            table.setLastHeaderRow(Integer.parseInt(value));	        }	        value = attributes.getProperty(ElementTags.ALIGN);	        if (value != null) {	            table.setAlignment(value);	        }	        value = attributes.getProperty(ElementTags.CELLSPACING);	        if (value != null) {	            table.setSpacing(Float.parseFloat(value + "f"));	        }	        value = attributes.getProperty(ElementTags.CELLPADDING);	        if (value != null) {	            table.setPadding(Float.parseFloat(value + "f"));	        }	        value = attributes.getProperty(ElementTags.OFFSET);	        if (value != null) {	            table.setOffset(Float.parseFloat(value + "f"));	        }	        value = attributes.getProperty(ElementTags.WIDTH);	        if (value != null) {	            if (value.endsWith("%"))	                table.setWidth(Float.parseFloat(value.substring(0, value.length() - 1) + "f"));	            else {	            	table.setWidth(Float.parseFloat(value + "f"));	            	table.setLocked(true);	            }	        }	        table.setTableFitsPage(Utilities.checkTrueOrFalse(attributes, ElementTags.TABLEFITSPAGE));	        table.setCellsFitPage(Utilities.checkTrueOrFalse(attributes, ElementTags.CELLSFITPAGE));	        table.setConvert2pdfptable(Utilities.checkTrueOrFalse(attributes, ElementTags.CONVERT2PDFP));	        	        setRectangleProperties(table, attributes);			return table;		} catch (BadElementException e) {			throw new ExceptionConverter(e);		}	}	/**	 * Sets some Rectangle properties (for a Cell, Table,...).	 */	private static void setRectangleProperties(Rectangle rect, Properties attributes) {		String value;		value = attributes.getProperty(ElementTags.BORDERWIDTH);		if (value != null) {			rect.setBorderWidth(Float.parseFloat(value + "f"));		}		int border = 0;		if (Utilities.checkTrueOrFalse(attributes, ElementTags.LEFT)) {			border |= Rectangle.LEFT;		}		if (Utilities.checkTrueOrFalse(attributes, ElementTags.RIGHT)) {			border |= Rectangle.RIGHT;		}		if (Utilities.checkTrueOrFalse(attributes, ElementTags.TOP)) {			border |= Rectangle.TOP;		}		if (Utilities.checkTrueOrFalse(attributes, ElementTags.BOTTOM)) {			border |= Rectangle.BOTTOM;		}		rect.setBorder(border);				String r = attributes.getProperty(ElementTags.RED);		String g = attributes.getProperty(ElementTags.GREEN);		String b = attributes.getProperty(ElementTags.BLUE);		if (r != null || g != null || b != null) {			int red = 0;			int green = 0;			int blue = 0;			if (r != null) red = Integer.parseInt(r);			if (g != null) green = Integer.parseInt(g);			if (b != null) blue = Integer.parseInt(b);			rect.setBorderColor(new Color(red, green, blue));		}		else {			rect.setBorderColor(Markup.decodeColor(attributes.getProperty(ElementTags.BORDERCOLOR)));		}		r = (String)attributes.remove(ElementTags.BGRED);		g = (String)attributes.remove(ElementTags.BGGREEN);		b = (String)attributes.remove(ElementTags.BGBLUE);		value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);		if (r != null || g != null || b != null) {			int red = 0;			int green = 0;			int blue = 0;			if (r != null) red = Integer.parseInt(r);			if (g != null) green = Integer.parseInt(g);			if (b != null) blue = Integer.parseInt(b);			rect.setBackgroundColor(new Color(red, green, blue));		}		else if (value != null) {			rect.setBackgroundColor(Markup.decodeColor(value));		}		else {			value = attributes.getProperty(ElementTags.GRAYFILL);			if (value != null) {				rect.setGrayFill(Float.parseFloat(value + "f"));			}		}	}		/**	 * Creates a ChapterAutoNumber object based on a list of properties.	 * @param attributes	 * @return a Chapter	 */	public static ChapterAutoNumber getChapter(Properties attributes) {		ChapterAutoNumber chapter = new ChapterAutoNumber("");		setSectionParameters(chapter, attributes);		return chapter;	}	/**	 * Creates a Section object based on a list of properties.	 * @param attributes	 * @return a Section	 */	public static Section getSection(Section parent, Properties attributes) {		Section section = parent.addSection("");		setSectionParameters(section, attributes);		return section;	}	/**	 * Helper method to create a Chapter/Section object.	 * @param attributes	 */	private static void setSectionParameters(Section section, Properties attributes) {		String value;		value = attributes.getProperty(ElementTags.NUMBERDEPTH);		if (value != null) {			section.setNumberDepth(Integer.parseInt(value));		}		value = attributes.getProperty(ElementTags.INDENT);		if (value != null) {			section.setIndentation(Float.parseFloat(value + "f"));		}		value = attributes.getProperty(ElementTags.INDENTATIONLEFT);		if (value != null) {			section.setIndentationLeft(Float.parseFloat(value + "f"));		}		value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);		if (value != null) {			section.setIndentationRight(Float.parseFloat(value + "f"));		}	}	/**	 * Creates an Image object based on a list of properties.	 * @param attributes	 * @return an Image	 */	public static Image getImage(Properties attributes)			throws BadElementException, MalformedURLException, IOException {		String value;				value = attributes.getProperty(ElementTags.URL);		if (value == null)			throw new MalformedURLException("The URL of the image is missing.");		Image image = Image.getInstance(value);				value = attributes.getProperty(ElementTags.ALIGN);		int align = 0;		if (value != null) {			if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value))				align |= Image.LEFT;			else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value))				align |= Image.RIGHT;			else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value))				align |= Image.MIDDLE;		}		if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.UNDERLYING)))			align |= Image.UNDERLYING;		if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.TEXTWRAP)))			align |= Image.TEXTWRAP;		image.setAlignment(align);				value = attributes.getProperty(ElementTags.ALT);		if (value != null) {			image.setAlt(value);		}				String x = attributes.getProperty(ElementTags.ABSOLUTEX);		String y = attributes.getProperty(ElementTags.ABSOLUTEY);		if ((x != null)	&& (y != null)) {			image.setAbsolutePosition(Float.parseFloat(x + "f"),					Float.parseFloat(y + "f"));		}		value = attributes.getProperty(ElementTags.PLAINWIDTH);		if (value != null) {			image.scaleAbsoluteWidth(Float.parseFloat(value + "f"));		}		value = attributes.getProperty(ElementTags.PLAINHEIGHT);		if (value != null) {			image.scaleAbsoluteHeight(Float.parseFloat(value + "f"));		}		value = attributes.getProperty(ElementTags.ROTATION);		if (value != null) {			image.setRotation(Float.parseFloat(value + "f"));		}		return image;	}	/**	 * Creates an Annotation object based on a list of properties.	 * @param attributes	 * @return an Annotation	 */	public static Annotation getAnnotation(Properties attributes) {		float llx = 0, lly = 0, urx = 0, ury = 0;		String value;				value = attributes.getProperty(ElementTags.LLX);		if (value != null) {			llx = Float.parseFloat(value + "f");		}		value = attributes.getProperty(ElementTags.LLY);		if (value != null) {			lly = Float.parseFloat(value + "f");		}		value = attributes.getProperty(ElementTags.URX);		if (value != null) {			urx = Float.parseFloat(value + "f");		}		value = attributes.getProperty(ElementTags.URY);		if (value != null) {			ury = Float.parseFloat(value + "f");		}				String title = attributes.getProperty(ElementTags.TITLE);		String text = attributes.getProperty(ElementTags.CONTENT);		if (title != null || text != null) {			return new Annotation(title, text, llx, lly, urx, ury);		}		value = attributes.getProperty(ElementTags.URL);		if (value != null) {			return new Annotation(llx, lly, urx, ury, value);		}		value = attributes.getProperty(ElementTags.NAMED);		if (value != null) {			return new Annotation(llx, lly, urx, ury, Integer.parseInt(value));		}		String file = attributes.getProperty(ElementTags.FILE);		String destination = attributes.getProperty(ElementTags.DESTINATION);		String page = (String) attributes.remove(ElementTags.PAGE);		if (file != null) {			if (destination != null) {				return new Annotation(llx, lly, urx, ury, file, destination);			}			if (page != null) {				return new Annotation(llx, lly, urx, ury, file, Integer.parseInt(page));			}		}		if (title == null)			title = "";		if (text == null)			text = "";		return new Annotation(title, text, llx, lly, urx, ury);	}}

⌨️ 快捷键说明

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