Browse Source

Pull request #463: REPORT-33025 无插件PDF导出-HTML解析:支持<a>标签超链

Merge in CORE/base-third from ~HUGH.C/base-third:release/10.0 to release/10.0

* commit '8bbe7fceb667ea7b524b296f412b7e39bbf42150':
  REPORT-33025 加点注解
  REPORT-33025 无插件PDF导出-HTML解析:支持<a>标签超链
release/10.0
ju.ju 4 years ago
parent
commit
3b0ca68d87
  1. 29
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java
  2. 28
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  3. 24
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java

29
fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java

@ -77,6 +77,9 @@ public class ChainedProperties {
} }
public String getLastChainProperty(String key) { public String getLastChainProperty(String key) {
if (0 == chain.size()) {
return null;
}
Object obj[] = (Object[]) chain.get(chain.size() - 1); Object obj[] = (Object[]) chain.get(chain.size() - 1);
HashMap prop = (HashMap) obj[1]; HashMap prop = (HashMap) obj[1];
return (String) prop.get(key); return (String) prop.get(key);
@ -199,5 +202,31 @@ public class ChainedProperties {
return false; 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);
}
} }

28
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; package com.fr.third.v2.lowagie.text.html.simpleparser;
import com.fr.third.v2.lowagie.text.html.CSS;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -85,6 +86,7 @@ public class FactoryProperties {
public Chunk createChunk(String text, ChainedProperties props) { public Chunk createChunk(String text, ChainedProperties props) {
Font font = getFont(props); Font font = getFont(props);
dealHyperlinkfont( font, props);
float size = font.getSize(); float size = font.getSize();
size /= 3; size /= 3;
Chunk ck = new Chunk(text, font); Chunk ck = new Chunk(text, font);
@ -104,6 +106,32 @@ public class FactoryProperties {
} }
return ck; 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)) {
//含有 text-decoration 属性,且值为 none时,将下划线属性去掉
font.setStyle(style & (~Font.UNDERLINE));
} else {
//默认是有需要有下划线的
font.setStyle(style | Font.UNDERLINE);
}
//没有指定颜色 or 是继承的颜色(<a>标签不需要继承颜色,只使用自身的颜色)时,使用默认的蓝色(超链接)
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) { private static void setParagraphLeading(Paragraph p, String leading) {
if (leading == null) { if (leading == null) {
p.setLeading(0, 1.5f); p.setLeading(0, 1.5f);

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

@ -832,6 +832,21 @@ public class PdfChunk {
public String toHtmlString() { public String toHtmlString() {
StringBuffer htmlString = new StringBuffer(); StringBuffer htmlString = new StringBuffer();
String hyperlink =getHyperlink();
if (null != hyperlink && hyperlink.length() > 0) {
htmlString.append("<a style='");
htmlString.append(getStyleAttributes()).append("'");
htmlString.append(" href='").append(hyperlink).append("'>");
if (this.isImage()) {
htmlString.append("<img src='").append(image.getSrcString()).append("'").
append(" style='width:").append(image.getPlainWidth()).append("px").
append(" height:").append(image.getPlainHeight()).append("px'").append("/>");
} else {
htmlString.append(value);
}
htmlString.append("</a>");
return htmlString.toString();
}
if (this.isImage()) { if (this.isImage()) {
htmlString.append("<img src='").append(image.getSrcString()).append("'"). htmlString.append("<img src='").append(image.getSrcString()).append("'").
append(" style='width:").append(image.getPlainWidth()).append("px"). append(" style='width:").append(image.getPlainWidth()).append("px").
@ -847,6 +862,15 @@ public class PdfChunk {
return htmlString.toString(); return htmlString.toString();
} }
public String getHyperlink() {
Object obj = this.getAttribute(Chunk.ACTION);
if (null == obj || !(obj instanceof PdfAction)) {
return null;
}
PdfObject pdfObj = ((PdfAction) obj).get(PdfName.URI);
return null == pdfObj ? null : pdfObj.toString();
}
private String getStyleAttributes(){ private String getStyleAttributes(){
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
dealFontStyle(stringBuffer, font.getOriFont()); dealFontStyle(stringBuffer, font.getOriFont());

Loading…
Cancel
Save