Browse Source

REPORT-7478 && REPORT-7479 html解析之背景图形/颜色; 直接绘制Itext对象, 而不是通过RichText转换

10.0
kerry 7 years ago
parent
commit
e4c29e6c4b
  1. 16
      fine-itext/src/com/fr/third/v2/lowagie/text/Chunk.java
  2. 14
      fine-itext/src/com/fr/third/v2/lowagie/text/factories/ElementFactory.java
  3. 284
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java
  4. 2
      fine-itext/src/com/fr/third/v2/lowagie/text/html/WebColors.java
  5. 14
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  6. 63
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  7. 2
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BaseFont.java
  8. 28
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  9. 22
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java

16
fine-itext/src/com/fr/third/v2/lowagie/text/Chunk.java

@ -54,10 +54,10 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import com.fr.third.v2.lowagie.text.pdf.PdfAction;
import com.fr.third.v2.lowagie.text.pdf.PdfContentByte;
import com.fr.third.v2.lowagie.text.pdf.HyphenationEvent; import com.fr.third.v2.lowagie.text.pdf.HyphenationEvent;
import com.fr.third.v2.lowagie.text.pdf.PdfAction;
import com.fr.third.v2.lowagie.text.pdf.PdfAnnotation; import com.fr.third.v2.lowagie.text.pdf.PdfAnnotation;
import com.fr.third.v2.lowagie.text.pdf.PdfContentByte;
import com.fr.third.v2.lowagie.text.pdf.draw.DrawInterface; import com.fr.third.v2.lowagie.text.pdf.draw.DrawInterface;
/** /**
@ -590,18 +590,18 @@ public class Chunk implements Element {
/** /**
* Sets the color of the background <CODE>Chunk</CODE>. * Sets the color of the background <CODE>Chunk</CODE>.
* *
* @param color * @param background
* the color of the background * the color of the background
* @return this <CODE>Chunk</CODE> * @return this <CODE>Chunk</CODE>
*/ */
public Chunk setBackground(Color color) { public Chunk setBackground(String background) {
return setBackground(color, 0, 0, 0, 0); return setBackground(background, 0, 0, 0, 0);
} }
/** /**
* Sets the color and the size of the background <CODE>Chunk</CODE>. * Sets the color and the size of the background <CODE>Chunk</CODE>.
* *
* @param color * @param background
* the color of the background * the color of the background
* @param extraLeft * @param extraLeft
* increase the size of the rectangle in the left * increase the size of the rectangle in the left
@ -613,9 +613,9 @@ public class Chunk implements Element {
* increase the size of the rectangle in the top * increase the size of the rectangle in the top
* @return this <CODE>Chunk</CODE> * @return this <CODE>Chunk</CODE>
*/ */
public Chunk setBackground(Color color, float extraLeft, float extraBottom, public Chunk setBackground(String background, float extraLeft, float extraBottom,
float extraRight, float extraTop) { float extraRight, float extraTop) {
return setAttribute(BACKGROUND, new Object[] { color, return setAttribute(BACKGROUND, new Object[] { background,
new float[] { extraLeft, extraBottom, extraRight, extraTop } }); new float[] { extraLeft, extraBottom, extraRight, extraTop } });
} }

14
fine-itext/src/com/fr/third/v2/lowagie/text/factories/ElementFactory.java

@ -58,25 +58,25 @@ import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Annotation; import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.ExceptionConverter;
import com.fr.third.v2.lowagie.text.FontFactory;
import com.fr.third.v2.lowagie.text.List;
import com.fr.third.v2.lowagie.text.html.Markup;
import com.fr.third.v2.lowagie.text.Anchor; import com.fr.third.v2.lowagie.text.Anchor;
import com.fr.third.v2.lowagie.text.Annotation;
import com.fr.third.v2.lowagie.text.BadElementException; import com.fr.third.v2.lowagie.text.BadElementException;
import com.fr.third.v2.lowagie.text.Cell; import com.fr.third.v2.lowagie.text.Cell;
import com.fr.third.v2.lowagie.text.ChapterAutoNumber; import com.fr.third.v2.lowagie.text.ChapterAutoNumber;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.ElementTags; import com.fr.third.v2.lowagie.text.ElementTags;
import com.fr.third.v2.lowagie.text.ExceptionConverter;
import com.fr.third.v2.lowagie.text.FontFactory;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import com.fr.third.v2.lowagie.text.List;
import com.fr.third.v2.lowagie.text.ListItem; import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Paragraph; import com.fr.third.v2.lowagie.text.Paragraph;
import com.fr.third.v2.lowagie.text.Phrase; import com.fr.third.v2.lowagie.text.Phrase;
import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.Section; import com.fr.third.v2.lowagie.text.Section;
import com.fr.third.v2.lowagie.text.Table; import com.fr.third.v2.lowagie.text.Table;
import com.fr.third.v2.lowagie.text.Utilities; import com.fr.third.v2.lowagie.text.Utilities;
import com.fr.third.v2.lowagie.text.html.Markup;
/** /**
* This class is able to create Element objects based on a list of properties. * This class is able to create Element objects based on a list of properties.
@ -135,7 +135,7 @@ public class ElementFactory {
} }
value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR); value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);
if (value != null) { if (value != null) {
chunk.setBackground(Markup.decodeColor(value)); chunk.setBackground(value);
} }
return chunk; return chunk;
} }

284
fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java

@ -0,0 +1,284 @@
package com.fr.third.v2.lowagie.text.html;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author kerry
* @date 2018/4/9
*/
public class CSSUtils {
public class CSS{
public final class Value {
private Value(){};
public static final String THIN = "thin";
public static final String MEDIUM = "medium";
public static final String THICK = "thick";
public static final String NONE = "none";
public static final String HIDDEN = "hidden";
public static final String DOTTED = "dotted";
public static final String DASHED = "dashed";
public static final String SOLID = "solid";
public static final String DOUBLE = "double";
public static final String GROOVE = "groove";
public static final String RIDGE = "ridge";
public static final String INSET = "inset";
public static final String OUTSET = "outset";
public static final String LEFT = "left";
public static final String CENTER = "center";
public static final String JUSTIFY = "justify";
public static final String BOTTOM = "bottom";
public static final String TOP = "top";
public static final String RIGHT = "right";
public static final String REPEAT = "repeat";
public static final String NO_REPEAT = "no-repeat";
public static final String REPEAT_X = "repeat-x";
public static final String REPEAT_Y = "repeat-y";
public static final String FIXED = "fixed";
public static final String SCROLL = "scroll";
public static final String DISC = "disc";
public static final String SQUARE = "square";
public static final String CIRCLE = "circle";
public static final String DECIMAL = "decimal";
public static final String LOWER_ROMAN = "lower-roman";
public static final String UPPER_ROMAN = "upper-roman";
public static final String LOWER_GREEK = "lower-greek";
public static final String UPPER_GREEK = "upper-greek";
public static final String LOWER_ALPHA = "lower-alpha";
public static final String UPPER_ALPHA = "upper-alpha";
public static final String LOWER_LATIN = "lower-latin";
public static final String UPPER_LATIN = "upper-latin";
public static final String INSIDE = "inside";
public static final String OUTSIDE = "outside";
public static final String INHERIT = "inherit";
public static final String UNDERLINE = "underline";
public static final String BOLD = "bold";
public static final String ITALIC = "italic";
public static final String OBLIQUE = "oblique";
public static final String SUPER = "super";
public static final String SUB = "sub";
public static final String TEXT_TOP = "text-top";
public static final String TEXT_BOTTOM = "text-bottom";
public static final String LINE_THROUGH = "line-through";
public static final String RELATIVE = "relative";
public static final String HIDE = "hide";
public static final String XX_SMALL = "xx-small";
public static final String X_SMALL = "x-small";
public static final String SMALL = "small";
public static final String LARGE = "large";
public static final String X_LARGE = "x-large";
public static final String XX_LARGE = "xx-large";
public static final String SMALLER = "smaller";
public static final String LARGER = "larger";
public static final String PX = "px";
public static final String IN = "in";
public static final String CM = "cm";
public static final String MM = "mm";
public static final String PT = "pt";
public static final String PC = "pc";
public static final String PERCENTAGE = "%";
public static final String EM = "em";
public static final String EX = "ex";
public static final String ALWAYS = "always";
public static final String AVOID = "avoid";
public static final String ABSOLUTE = "absolute";
public static final String AUTO = "auto";
public static final String INLINE = "inline";
public static final String BLOCK = "block";
public static final String SEPARATE = "separate";
public static final String COLLAPSE = "collapse";
public static final String RTL = "rtl";
public static final String LTR = "ltr";
public static final String INLINE_BLOCK = "inline-block";
public static final String INLINE_TABLE = "inline-table";
public static final String LIST_ITEM = "list-item";
public static final String RUN_IN = "run-in";
public static final String TABLE = "table";
public static final String TABLE_CAPTION = "table-caption";
public static final String TABLE_CELL = "table-cell";
public static final String TABLE_COLUMN_GROUP = "table-column-group";
public static final String TABLE_COLUMN = "table-column";
public static final String TABLE_FOOTER_GROUP = "table-footer-group";
public static final String TABLE_HEADER_GROUP = "table-header-group";
public static final String TABLE_ROW = "table-row";
public static final String TABLE_ROW_GROUP = "table-row-group";
}
public final class Property {
private Property() {};
public static final String BACKGROUND = "background";
public static final String BACKGROUND_IMAGE = "background-image";
public static final String BACKGROUND_REPEAT = "background-repeat";
public static final String BACKGROUND_ATTACHMENT = "background-attachment";
public static final String BACKGROUND_POSITION = "background-position";
public static final String BACKGROUND_COLOR = "background-color";
public static final String LIST_STYLE = "list-style";
public static final String LIST_STYLE_TYPE = "list-style-type";
public static final String LIST_STYLE_POSITION = "list-style-position";
public static final String LIST_STYLE_IMAGE = "list-style-image";
public static final String MARGIN = "margin";
public static final String TOP = "top";
public static final String MARGIN_LEFT = "margin-left";
public static final String MARGIN_RIGHT = "margin-right";
public static final String MARGIN_TOP = "margin-top";
public static final String MARGIN_BOTTOM = "margin-bottom";
public static final String BORDER = "border";
public static final String BORDER_LEFT = "border-left";
public static final String BORDER_TOP = "border-top";
public static final String BORDER_RIGHT = "border-right";
public static final String BORDER_BOTTOM = "border-bottom";
public static final String BORDER_WIDTH = "border-width";
public static final String BORDER_STYLE = "border-style";
public static final String BORDER_COLOR = "border-color";
public static final String BORDER_COLLAPSE = "border-collapse";
public static final String BORDER_SPACING = "border-spacing";
public static final String BORDER_TOP_WIDTH = "border-top-width";
public static final String BORDER_BOTTOM_WIDTH = "border-bottom-width";
public static final String BORDER_LEFT_WIDTH = "border-left-width";
public static final String BORDER_RIGHT_WIDTH = "border-right-width";
public static final String BORDER_TOP_COLOR = "border-top-color";
public static final String BORDER_BOTTOM_COLOR = "border-bottom-color";
public static final String BORDER_LEFT_COLOR = "border-left-color";
public static final String BORDER_RIGHT_COLOR = "border-right-color";
public static final String BORDER_TOP_STYLE = "border-top-style";
public static final String BORDER_BOTTOM_STYLE = "border-bottom-style";
public static final String BORDER_LEFT_STYLE = "border-left-style";
public static final String BORDER_RIGHT_STYLE = "border-right-style";
public static final String PADDING = "padding";
public static final String PADDING_TOP = "padding-top";
public static final String PADDING_BOTTOM = "padding-bottom";
public static final String PADDING_LEFT = "padding-left";
public static final String PADDING_RIGHT = "padding-right";
public static final String FONT = "font";
public static final String FONT_WEIGHT = "font-weight";
public static final String FONT_SIZE = "font-size";
public static final String FONT_STYLE = "font-style";
public static final String FONT_FAMILY = "font-family";
public static final String TEXT_DECORATION = "text-decoration";
public static final String COLOR = "color";
public static final String TAB_INTERVAL = "tab-interval";
public static final String XFA_TAB_COUNT = "xfa-tab-count";
public static final String XFA_FONT_HORIZONTAL_SCALE = "xfa-font-horizontal-scale";
public static final String XFA_FONT_VERTICAL_SCALE = "xfa-font-vertical-scale";
public static final String BEFORE = "before";
public static final String AFTER = "after";
public static final String HEIGHT = "height";
public static final String WIDTH = "width";
public static final String LETTER_SPACING = "letter-spacing";
public static final String VERTICAL_ALIGN = "vertical-align";
public static final String LINE_HEIGHT = "line-height";
public static final String TEXT_ALIGN = "text-align";
public static final String TEXT_VALIGN = "text-valign";
public static final String TEXT_INDENT = "text-indent";
public static final String POSITION = "position";
public static final String EMPTY_CELLS = "empty-cells";
public static final String CELLPADDING = "cellpadding";
//deprecated
public static final String CELLPADDING_LEFT = "cellpadding-left";
public static final String CELLPADDING_TOP = "cellpadding-top";
public static final String CELLPADDING_RIGHT = "cellpadding-right";
public static final String CELLPADDING_BOTTOM = "cellpadding-bottom";
public static final String CAPTION_SIDE = "caption-side";
public static final String TAB_STOPS = "tab-stops";
public static final String XFA_TAB_STOPS = "xfa-tab-stops";
public static final String PAGE_BREAK_BEFORE = "page-break-before";
public static final String PAGE_BREAK_INSIDE = "page-break-inside";
public static final String PAGE_BREAK_AFTER = "page-break-after";
public static final String REPEAT_HEADER = "repeat-header";
public static final String REPEAT_FOOTER = "repeat-footer";
public static final String LEFT = "left";
public static final String DISPLAY = "display";
public static final String MIN_WIDTH = "min-width";
public static final String MAX_WIDTH = "max-width";
public static final String MIN_HEIGHT = "min-height";
public static final String MAX_HEIGHT = "max-height";
public static final String RIGHT = "right";
public static final String BOTTOM = "bottom";
public static final String FLOAT = "float";
public static final String DIRECTION = "direction";
}
}
private static final Set<String> backgroundPositions = new HashSet<String>(
Arrays.asList(new String[] { CSS.Value.LEFT, CSS.Value.CENTER, CSS.Value.BOTTOM, CSS.Value.TOP, CSS.Value.RIGHT }));
public static Map<String, String> processBackground(final String background) {
Map<String, String> rules = new HashMap<String, String>();
String[] styles = splitComplexCssStyle(background);
for(String style : styles) {
if (style.contains("url(")) {
rules.put(CSS.Property.BACKGROUND_IMAGE, style);
} else if (style.equalsIgnoreCase(CSS.Value.REPEAT)
|| style.equalsIgnoreCase(CSS.Value.NO_REPEAT)
|| style.equalsIgnoreCase(CSS.Value.REPEAT_X)
|| style.equalsIgnoreCase(CSS.Value.REPEAT_Y)) {
rules.put(CSS.Property.BACKGROUND_REPEAT, style);
} else if (style.equalsIgnoreCase(CSS.Value.FIXED) || style.equalsIgnoreCase(CSS.Value.SCROLL)) {
rules.put(CSS.Property.BACKGROUND_ATTACHMENT, style);
} else if (backgroundPositions.contains(style)) {
if(rules.get(CSS.Property.BACKGROUND_POSITION) == null) {
rules.put(CSS.Property.BACKGROUND_POSITION, style);
} else {
style = style.concat(" "+rules.get(CSS.Property.BACKGROUND_POSITION));
rules.put(CSS.Property.BACKGROUND_POSITION, style);
}
} else if (isNumericValue(style) || isMetricValue(style) || isRelativeValue(style)) {
if(rules.get(CSS.Property.BACKGROUND_POSITION) == null) {
rules.put(CSS.Property.BACKGROUND_POSITION, style);
} else {
style = style.concat(" "+rules.get(CSS.Property.BACKGROUND_POSITION));
rules.put(CSS.Property.BACKGROUND_POSITION, style);
}
} else if(style.contains("rgb(") || style.contains("rgba(") || style.contains("#") || WebColors.NAMES.containsKey(style.toLowerCase())) {
rules.put(CSS.Property.BACKGROUND_COLOR, style);
}
}
return rules;
}
public static String[] splitComplexCssStyle(String s) {
s = s.replaceAll("\\s*,\\s*", ",") ;
return s.split("\\s");
}
public static boolean isMetricValue(final String value) {
return value.contains(CSS.Value.PX) || value.contains(CSS.Value.IN) || value.contains(CSS.Value.CM)
|| value.contains(CSS.Value.MM) || value.contains(CSS.Value.PC) || value.contains(CSS.Value.PT);
}
public static boolean isRelativeValue(final String value) {
return value.contains(CSS.Value.PERCENTAGE) || value.contains(CSS.Value.EM) || value.contains(CSS.Value.EX);
}
public static boolean isNumericValue(final String value) {
return value.matches("^-?\\d\\d*\\.\\d*$") || value.matches("^-?\\d\\d*$") || value.matches("^-?\\.\\d\\d*$");
}
public static String extractUrl(final String url) {
String str = null;
if (url.startsWith("url")) {
String urlString = url.substring(3).trim().replace("(", "").replace(")", "").trim();
if (urlString.startsWith("'") && urlString.endsWith("'")) {
str = urlString.substring(urlString.indexOf("'")+1, urlString.lastIndexOf("'"));
} else if ( urlString.startsWith("\"") && urlString.endsWith("\"") ) {
str = urlString.substring(urlString.indexOf('"')+1, urlString.lastIndexOf('"'));
} else {
str = urlString;
}
} else {
str = url;
}
return str;
}
}

2
fine-itext/src/com/fr/third/v2/lowagie/text/html/WebColors.java

@ -258,6 +258,6 @@ public class WebColors extends HashMap {
throw new IllegalArgumentException("Color '" + name throw new IllegalArgumentException("Color '" + name
+ "' not found."); + "' not found.");
c = (int[]) NAMES.get(name); c = (int[]) NAMES.get(name);
return new Color(c[0], c[1], c[2], c[3]); return new Color(c[0], c[1], c[2], 255);
} }
} }

14
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java

@ -55,20 +55,19 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Font;
import com.fr.third.v2.lowagie.text.FontFactory;
import com.fr.third.v2.lowagie.text.html.HtmlTags;
import com.fr.third.v2.lowagie.text.html.Markup;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.Element; import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.ElementTags; import com.fr.third.v2.lowagie.text.ElementTags;
import com.fr.third.v2.lowagie.text.Font;
import com.fr.third.v2.lowagie.text.FontFactory;
import com.fr.third.v2.lowagie.text.FontFactoryImp; import com.fr.third.v2.lowagie.text.FontFactoryImp;
import com.fr.third.v2.lowagie.text.ListItem; import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Paragraph; import com.fr.third.v2.lowagie.text.Paragraph;
import com.fr.third.v2.lowagie.text.html.HtmlTags;
import com.fr.third.v2.lowagie.text.pdf.BaseFont; import com.fr.third.v2.lowagie.text.pdf.BaseFont;
import com.fr.third.v2.lowagie.text.pdf.HyphenationAuto; import com.fr.third.v2.lowagie.text.pdf.HyphenationAuto;
import com.fr.third.v2.lowagie.text.pdf.HyphenationEvent; import com.fr.third.v2.lowagie.text.pdf.HyphenationEvent;
import com.fr.third.v2.lowagie.text.html.Markup;
/** /**
* *
@ -92,9 +91,12 @@ public class FactoryProperties {
else if (props.hasProperty("sup")) else if (props.hasProperty("sup"))
ck.setTextRise(size); ck.setTextRise(size);
ck.setHyphenation(getHyphenation(props)); ck.setHyphenation(getHyphenation(props));
//chunk添加背景属性
if (props.hasProperty("background")) {
ck.setBackground(props.getProperty("background"));
}
return ck; return ck;
} }
private static void setParagraphLeading(Paragraph p, String leading) { private static void setParagraphLeading(Paragraph p, String leading) {
if (leading == null) { if (leading == null) {
p.setLeading(0, 1.5f); p.setLeading(0, 1.5f);

63
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java

@ -55,30 +55,32 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.DocumentException;
import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.ExceptionConverter; import com.fr.third.v2.lowagie.text.ExceptionConverter;
import com.fr.third.v2.lowagie.text.FontFactoryImp;
import com.fr.third.v2.lowagie.text.Image;
import com.fr.third.v2.lowagie.text.List; import com.fr.third.v2.lowagie.text.List;
import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.TextElementArray;
import com.fr.third.v2.lowagie.text.html.HtmlTags; import com.fr.third.v2.lowagie.text.html.HtmlTags;
import com.fr.third.v2.lowagie.text.html.Markup;
import com.fr.third.v2.lowagie.text.pdf.draw.LineSeparator; import com.fr.third.v2.lowagie.text.pdf.draw.LineSeparator;
import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLParser; import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
import com.fr.third.v2.lowagie.text.html.Markup;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.DocListener; import com.fr.third.v2.lowagie.text.DocListener;
import com.fr.third.v2.lowagie.text.DocumentException;
import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.ElementTags; import com.fr.third.v2.lowagie.text.ElementTags;
import com.fr.third.v2.lowagie.text.FontFactoryImp;
import com.fr.third.v2.lowagie.text.HeaderFooter; import com.fr.third.v2.lowagie.text.HeaderFooter;
import com.fr.third.v2.lowagie.text.Image;
import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Paragraph; import com.fr.third.v2.lowagie.text.Paragraph;
import com.fr.third.v2.lowagie.text.Phrase; import com.fr.third.v2.lowagie.text.Phrase;
import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.TextElementArray;
import com.fr.third.v2.lowagie.text.pdf.PdfPTable; import com.fr.third.v2.lowagie.text.pdf.PdfPTable;
import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLDocHandler; import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLParser;
public class HTMLWorker implements SimpleXMLDocHandler, DocListener { public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
@ -177,11 +179,52 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
cprops.addToChain("body", h); cprops.addToChain("body", h);
} }
private void dealStyleAttribute(HashMap h){
String styleAtt = (String) h.get("style");
if (null != styleAtt && styleAtt.length() > 0) {
Map<String, String> tagAttrCss = new LinkedHashMap<String, String>();
String[] styles = styleAtt.split(";");
for (String s : styles) {
String[] part = s.split(":",2);
if (part.length == 2) {
String key = stripDoubleSpacesAndTrim(part[0]).toLowerCase();
String value = stripDoubleSpacesAndTrim(part[1]);
tagAttrCss.put(key, value);
}
}
for (Map.Entry<String, String> e : tagAttrCss.entrySet()) {
h.put(e.getKey(), e.getValue());
}
}
}
public String stripDoubleSpacesAndTrim(final String str) {
char[] charArray = str.toCharArray();
if (str.contains(" ")) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (c != ' ') {
builder.append(c);
} else {
if (i + 1 < charArray.length && charArray[i + 1] != ' ') {
builder.append(' ');
}
}
}
return builder.toString().trim();
} else {
return String.valueOf(charArray).trim();
}
}
public void startElement(String tag, HashMap h) { public void startElement(String tag, HashMap h) {
if (!tagsSupported.containsKey(tag)) if (!tagsSupported.containsKey(tag))
return; return;
try { try {
style.applyStyle(tag, h); style.applyStyle(tag, h);
//解析Style的样式属性
dealStyleAttribute(h);
String follow = (String) FactoryProperties.followTags.get(tag); String follow = (String) FactoryProperties.followTags.get(tag);
if (follow != null) { if (follow != null) {
HashMap prop = new HashMap(); HashMap prop = new HashMap();

2
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BaseFont.java

@ -234,7 +234,7 @@ public abstract class BaseFont {
public static final boolean NOT_CACHED = false; public static final boolean NOT_CACHED = false;
/** The path to the font resources. */ /** The path to the font resources. */
public static final String RESOURCE_PATH = "com/fr/third/v2/text/pdf/fonts/"; public static final String RESOURCE_PATH = "com/fr/third/v2/lowagie/text/pdf/fonts/";
/** The fake CID code that represents a newline. */ /** The fake CID code that represents a newline. */
public static final char CID_NEWLINE = '\u7fff'; public static final char CID_NEWLINE = '\u7fff';

28
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java

@ -54,10 +54,12 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import com.fr.third.v2.lowagie.text.SplitCharacter;
import com.fr.third.v2.lowagie.text.Utilities;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Font;
import com.fr.third.v2.lowagie.text.SplitCharacter;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import com.fr.third.v2.lowagie.text.SplitCharacter;
import com.fr.third.v2.lowagie.text.Utilities; import com.fr.third.v2.lowagie.text.Utilities;
/** /**
@ -78,9 +80,19 @@ public class PdfChunk {
private static final float ITALIC_ANGLE = 0.21256f; private static final float ITALIC_ANGLE = 0.21256f;
/** The allowed attributes in variable <CODE>attributes</CODE>. */ /** The allowed attributes in variable <CODE>attributes</CODE>. */
private static final HashMap keysAttributes = new HashMap(); private static final HashMap keysAttributes = new HashMap();
/** The allowed attributes in variable <CODE>noStroke</CODE>. */ public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
/** The allowed attributes in variable <CODE>noStroke</CODE>. */
private static final HashMap keysNoStroke = new HashMap(); private static final HashMap keysNoStroke = new HashMap();
private float height = 0;
static { static {
keysAttributes.put(Chunk.ACTION, null); keysAttributes.put(Chunk.ACTION, null);
@ -187,7 +199,7 @@ public class PdfChunk {
* @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor</CODE> * @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor</CODE>
*/ */
PdfChunk(Chunk chunk, PdfAction action) { public PdfChunk(Chunk chunk, PdfAction action) {
thisChunk[0] = this; thisChunk[0] = this;
value = chunk.getContent(); value = chunk.getContent();
@ -213,6 +225,10 @@ public class PdfChunk {
attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE}); attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE});
} }
font = new PdfFont(baseFont, size); font = new PdfFont(baseFont, size);
float fontSize = font.size();
float ascender = font.getFont().getFontDescriptor(BaseFont.ASCENT, fontSize);
float descender = font.getFont().getFontDescriptor(BaseFont.DESCENT, fontSize);
height = ascender - descender;
// other style possibilities // other style possibilities
HashMap attr = chunk.getAttributes(); HashMap attr = chunk.getAttributes();
if (attr != null) { if (attr != null) {
@ -524,7 +540,7 @@ public class PdfChunk {
* @return a width * @return a width
*/ */
float width() { public float width() {
return font.width(value); return font.width(value);
} }
@ -617,7 +633,7 @@ public class PdfChunk {
* @return the attribute value or null if not found * @return the attribute value or null if not found
*/ */
Object getAttribute(String name) public Object getAttribute(String name)
{ {
if (attributes.containsKey(name)) if (attributes.containsKey(name))
return attributes.get(name); return attributes.get(name);

22
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java

@ -52,9 +52,9 @@ package com.fr.third.v2.lowagie.text.pdf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.Element; import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.ListItem; import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Chunk;
/** /**
* <CODE>PdfLine</CODE> defines an array with <CODE>PdfChunk</CODE>-objects * <CODE>PdfLine</CODE> defines an array with <CODE>PdfChunk</CODE>-objects
@ -72,7 +72,7 @@ public class PdfLine {
protected float left; protected float left;
/** The width of the line. */ /** The width of the line. */
protected float width; public float width;
/** The alignment of the line. */ /** The alignment of the line. */
protected int alignment; protected int alignment;
@ -93,7 +93,14 @@ public class PdfLine {
protected float originalWidth; protected float originalWidth;
protected boolean isRTL = false; protected boolean isRTL = false;
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
// constructors // constructors
/** /**
@ -105,7 +112,7 @@ public class PdfLine {
* @param height the height of the line * @param height the height of the line
*/ */
PdfLine(float left, float right, int alignment, float height) { public PdfLine(float left, float right, int alignment, float height) {
this.left = left; this.left = left;
this.width = right - left; this.width = right - left;
this.originalWidth = this.width; this.originalWidth = this.width;
@ -145,7 +152,7 @@ public class PdfLine {
* not be added is returned * not be added is returned
*/ */
PdfChunk add(PdfChunk chunk) { public PdfChunk add(PdfChunk chunk) {
// nothing happens if the chunk is null. // nothing happens if the chunk is null.
if (chunk == null || chunk.toString().equals("")) { if (chunk == null || chunk.toString().equals("")) {
return null; return null;
@ -198,9 +205,12 @@ public class PdfLine {
} }
private void addToLine(PdfChunk chunk) { private void addToLine(PdfChunk chunk) {
//添加到line上处理计算line高度
if (chunk.changeLeading && chunk.isImage()) { if (chunk.changeLeading && chunk.isImage()) {
float f = chunk.getImage().getScaledHeight() + chunk.getImageOffsetY() + chunk.getImage().getBorderWidthTop(); float f = chunk.getImage().getScaledHeight() + chunk.getImageOffsetY() + chunk.getImage().getBorderWidthTop();
if (f > height) height = f; if (f > height) height = f;
}else{
height = Math.max(height, chunk.getHeight());
} }
line.add(chunk); line.add(chunk);
} }
@ -233,7 +243,7 @@ public class PdfLine {
* @return a value * @return a value
*/ */
float height() { public float height() {
return height; return height;
} }

Loading…
Cancel
Save