diff --git a/fine-itext-old/src/main/java/com/fr/third/com/lowagie/text/xml/simpleparser/SimpleXMLParser.java b/fine-itext-old/src/main/java/com/fr/third/com/lowagie/text/xml/simpleparser/SimpleXMLParser.java index e2873f902..3bfbcf761 100644 --- a/fine-itext-old/src/main/java/com/fr/third/com/lowagie/text/xml/simpleparser/SimpleXMLParser.java +++ b/fine-itext-old/src/main/java/com/fr/third/com/lowagie/text/xml/simpleparser/SimpleXMLParser.java @@ -506,7 +506,7 @@ public final class SimpleXMLParser { if (c == -1) { return; } - if (c == '/' || HTMLWorker.tagsPrefixSupported.containsKey(c)) { + if (c == '/' || HTMLWorker.tagsPrefixSupported.containsKey(Character.toLowerCase(c))) { if (type == TEXT) { flush(); } diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java index 09cd4d8ac..63e3a3a04 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java @@ -52,6 +52,8 @@ package com.fr.third.v2.lowagie.text.html; +import com.fr.third.v2.lowagie.text.ElementTags; +import com.fr.third.v2.lowagie.text.html.simpleparser.ChainedProperties; import java.awt.Color; import java.util.ArrayList; import java.util.List; @@ -417,6 +419,21 @@ public class Markup { return f; } + /** + * 解析当前字号(可能是继承来的) + * + * @param cprops + * @return + */ + public static float parseDefaultFontSize(ChainedProperties cprops) { + if (null == cprops) { + return DEFAULT_FONT_SIZE; + } + String size = cprops.getProperty(ElementTags.SIZE); + return null == size ? Markup.DEFAULT_FONT_SIZE : Markup.parseLength(size, + Markup.DEFAULT_FONT_SIZE); + } + /** * Converts a Color into a HTML representation of this * Color. diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java index 5e68ef341..752d6bb2d 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java @@ -211,14 +211,14 @@ public class FactoryProperties { List list = Markup.parseNESW(props.getPropertyFromChain("div", Markup.CSS_KEY_MARGIN)); if (null != list && 4 == list.size()) { for (int i = 0; i < HtmlConstants.MARGIN.size() && i < list.size(); i++) { - p.setAttribute(HtmlConstants.MARGIN.get(i), list.get(i)); + p.setAttribute(HtmlConstants.MARGIN.get(i), String.valueOf(Markup.parseLength(list.get(i), Markup.parseDefaultFontSize(props)))); } } } for (String margin : HtmlConstants.MARGIN) { if (props.hasPropertyInChain("div", margin)) { String ss = props.getPropertyFromChain("div", margin); - p.setAttribute(margin, ss); + p.setAttribute(margin, String.valueOf(Markup.parseLength(ss, Markup.parseDefaultFontSize(props)))); } } } diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java index 2c5e436f2..c16dd77f8 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java @@ -50,6 +50,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; +import com.fr.third.v2.lowagie.text.html.utils.DefaultPropertiesHandleUtils; import com.fr.third.v2.lowagie.text.pdf.PdfFont; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; @@ -211,17 +212,7 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { return; try { style.applyStyle(tag, h); - if(tag.equals("p")){ - h.put(Markup.CSS_KEY_MARGINTOP, "16px"); - h.put(Markup.CSS_KEY_MARGINBOTTOM, "16px"); - } - if (tag.equals("th")) { - h.put("b", null); - } - if (tag.equals(HtmlTags.ANCHOR)) { - h.put("u", null); - h.put("color", "blue"); - } + DefaultPropertiesHandleUtils.fillDefaultProperties(tag, h); String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { HashMap prop = new HashMap(); @@ -736,7 +727,8 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { currentParagraph.add(chunk); return; } - if (content.trim().length() == 0 && content.indexOf(' ') < 0) { + + if (content.trim().length() == 0 && null == currentParagraph) { return; } diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/utils/DefaultPropertiesHandleUtils.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/utils/DefaultPropertiesHandleUtils.java new file mode 100644 index 000000000..58a9043f6 --- /dev/null +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/utils/DefaultPropertiesHandleUtils.java @@ -0,0 +1,90 @@ +package com.fr.third.v2.lowagie.text.html.utils; + +import com.fr.third.v2.lowagie.text.html.HtmlTags; +import com.fr.third.v2.lowagie.text.html.Markup; +import java.util.HashMap; +import java.util.Properties; + +/** + * 处理标签默认属性值 + * + * @author Hugh.C + * @version 1.0 + * Created by Hugh.C on 2020/7/3 + */ +public class DefaultPropertiesHandleUtils { + + //h1-h6 标签默认字体大小 + private static final HashMap H_TAG_DEFAULT_FONT_SIZE = new HashMap(6); + + //h1-h6 标签默认margin-top and bottom大小 + private static final HashMap H_TAG_DEFAULT_MARGIN = new HashMap(6); + + + static { + H_TAG_DEFAULT_FONT_SIZE.put("h1", "2em"); + H_TAG_DEFAULT_FONT_SIZE.put("h2", "1.5em"); + H_TAG_DEFAULT_FONT_SIZE.put("h3", "1.17em"); + H_TAG_DEFAULT_FONT_SIZE.put("h4", "1em"); + H_TAG_DEFAULT_FONT_SIZE.put("h5", "0.83em"); + H_TAG_DEFAULT_FONT_SIZE.put("h6", "0.67em"); + + H_TAG_DEFAULT_MARGIN.put("h1", "0.67em"); + H_TAG_DEFAULT_MARGIN.put("h2", "0.83em"); + H_TAG_DEFAULT_MARGIN.put("h3", "1em"); + H_TAG_DEFAULT_MARGIN.put("h4", "1.33em"); + H_TAG_DEFAULT_MARGIN.put("h5", "1.67em"); + H_TAG_DEFAULT_MARGIN.put("h6", "2.33em"); + } + + /** + * 填充默认属性值 + * + * @param tag + * @param h + */ + public static void fillDefaultProperties(String tag, HashMap h) { + if (null == tag || null == h) { + return; + } + if (HtmlTags.HEADERCELL.endsWith(tag)) { + h.put("b", null); + return; + } + if (HtmlTags.ANCHOR.equals(tag)) { + h.put("u", null); + h.put("color", "blue"); + return; + } + + String style = (String) h.get("style"); + if (HtmlTags.PARAGRAPH.equals(tag)) { + Properties props = Markup.parseAttributes(style); + if (!props.containsKey(Markup.CSS_KEY_MARGINTOP)) { + h.put(Markup.CSS_KEY_MARGINTOP, "1em"); + } + if (!props.containsKey(Markup.CSS_KEY_MARGINBOTTOM)) { + h.put(Markup.CSS_KEY_MARGINBOTTOM, "1em"); + } + return; + } + + if (H_TAG_DEFAULT_FONT_SIZE.containsKey(tag)) { + h.put("b", null); + Properties props = Markup.parseAttributes(style); + if (!props.containsKey(Markup.CSS_KEY_MARGINTOP)) { + h.put(Markup.CSS_KEY_MARGINTOP, H_TAG_DEFAULT_MARGIN.get(tag)); + } + if (!props.containsKey(Markup.CSS_KEY_MARGINBOTTOM)) { + h.put(Markup.CSS_KEY_MARGINBOTTOM, H_TAG_DEFAULT_MARGIN.get(tag)); + } + if (!props.containsKey(Markup.CSS_KEY_FONTSIZE)) { + h.put("style", new StringBuilder(null == style ? "" : style).append(";") + .append(Markup.CSS_KEY_FONTSIZE).append(":").append(H_TAG_DEFAULT_FONT_SIZE.get(tag)).append(";").toString()); + } + return; + } + + } + +} diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfLine.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfLine.java index 149aaa007..d33306984 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfLine.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfLine.java @@ -214,6 +214,7 @@ public class PdfLine { // if the chunk couldn't even be truncated, we add everything, so be it else if (overflow != null){ addToLine(overflow); + width = 0; return null; }else{ addToLine(chunk);