From 1556abaf25838dae78e48f4b01b305f0aa42a80b Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Wed, 10 Jun 2020 13:32:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-33025=20=E6=97=A0=E6=8F=92=E4=BB=B6P?= =?UTF-8?q?DF=E5=AF=BC=E5=87=BA-HTML=E8=A7=A3=E6=9E=90=EF=BC=9A=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=A0=87=E7=AD=BE=E8=B6=85=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../html/simpleparser/ChainedProperties.java | 33 +++++++++++++++++-- .../html/simpleparser/FactoryProperties.java | 25 ++++++++++++++ .../third/v2/lowagie/text/pdf/PdfChunk.java | 24 ++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) 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("