diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java index eb709e343..8a4122ca6 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java @@ -76,8 +76,11 @@ public class ChainedProperties { return null; } - public String getLastChainProperty(String key){ - Object obj[] = (Object[]) chain.get(chain.size()-1); + public String getLastChainProperty(String key) { + if (0 == chain.size()) { + return null; + } + Object obj[] = (Object[]) chain.get(chain.size() - 1); HashMap prop = (HashMap) obj[1]; return (String) prop.get(key); } @@ -199,5 +202,31 @@ public class ChainedProperties { return false; } + /** + * 获取当前处理的标签 + * + * @return 标签名 + */ + public String getLastTag() { + if (chain.size() == 0) { + return null; + } + Object obj[] = (Object[]) chain.get(chain.size() - 1); + return (String) obj[0]; + } + /** + * 判断 LastChain 是否存在一对 Key - value + * + * @param key + * @param value + * @return + */ + public boolean hasKvInLastChain(String key, String value) { + String actualValue = getLastChainProperty(key); + if (null == actualValue && null == value) { + return true; + } + return null == value ? actualValue.equals(value) : value.equals(actualValue); + } } 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 0b29f4d01..820684d02 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 @@ -50,6 +50,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; +import com.fr.third.v2.lowagie.text.html.CSS; import java.awt.Color; import java.util.HashMap; import java.util.Iterator; @@ -85,6 +86,7 @@ public class FactoryProperties { public Chunk createChunk(String text, ChainedProperties props) { Font font = getFont(props); + dealHyperlinkfont( font, props); float size = font.getSize(); size /= 3; Chunk ck = new Chunk(text, font); @@ -104,6 +106,29 @@ public class FactoryProperties { } return ck; } + + /** + * 处理超链接的字体颜色和下划线 + * + * @param font + * @param props + */ + private static void dealHyperlinkfont(Font font, ChainedProperties props) { + String tagName = props.getLastTag(); + if (null == tagName || tagName.equals("") || !tagName.equals(HtmlTags.ANCHOR)) { + return; + } + int style = font.getStyle(); + if (props.hasKvInLastChain(CSS.Property.TEXT_DECORATION, CSS.Value.NONE)) { + font.setStyle(style & (~Font.UNDERLINE)); + } else { + font.setStyle(style | Font.UNDERLINE); + } + if (null == font.getColor() || (!font.getColor().equals(Color.BLUE) && null == props.getLastChainProperty(CSS.Property.COLOR))) { + font.setColor(Color.BLUE); + } + } + private static void setParagraphLeading(Paragraph p, String leading) { if (leading == null) { p.setLeading(0, 1.5f); diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java index c666a1093..78c76d50d 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java @@ -832,6 +832,21 @@ public class PdfChunk { public String toHtmlString() { StringBuffer htmlString = new StringBuffer(); + String hyperlink =getHyperlink(); + if (null != hyperlink && hyperlink.length() > 0) { + htmlString.append(""); + if (this.isImage()) { + htmlString.append(""); + } else { + htmlString.append(value); + } + htmlString.append(""); + return htmlString.toString(); + } if (this.isImage()) { htmlString.append("