Browse Source

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

release/10.0
Hugh.C 5 years ago
parent
commit
1556abaf25
  1. 33
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java
  2. 25
      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

33
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);
}
}

25
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);

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() {
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()) {
htmlString.append("<img src='").append(image.getSrcString()).append("'").
append(" style='width:").append(image.getPlainWidth()).append("px").
@ -847,6 +862,15 @@ public class PdfChunk {
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(){
StringBuffer stringBuffer = new StringBuffer();
dealFontStyle(stringBuffer, font.getOriFont());

Loading…
Cancel
Save