Browse Source

REPORT-33028 无插件PDF导出-HTML解析:块元素支持margin、padding 属性

release/10.0
Hugh.C 4 years ago
parent
commit
836712194c
  1. 41
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java
  2. 62
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  3. 10
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java
  4. 3
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java

41
fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java

@ -53,7 +53,10 @@
package com.fr.third.v2.lowagie.text.html; package com.fr.third.v2.lowagie.text.html;
import java.awt.Color; import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer;
/** /**
* A class that contains all the possible tagnames and their attributes. * A class that contains all the possible tagnames and their attributes.
@ -510,4 +513,42 @@ public class Markup {
return result.toString(); return result.toString();
} }
/**
* 解析类似于 margin: 10px 5px 15px 20px中的值
* NESW:北东南西上右下左
*
* @param attr 如10px 5px 15px 20px
* @return list: 按上左下右的顺序存放 要么四个方位的值都存在要么都不存在返回null
*/
public static List<String> parseNESW(String attr) {
if (null == attr) {
return null;
}
List<String> list = new ArrayList<String>(4);
StringTokenizer st = new StringTokenizer(attr);
while (st.hasMoreElements()) {
list.add(st.nextToken());
}
switch (list.size()) {
case 1:
for (int i = 0; i < 3; i++) {
list.add(list.get(0));
}
break;
case 2:
list.add(list.get(0));
list.add(list.get(1));
break;
case 3:
list.add(list.get(1));
break;
case 4:
break;
default:
list = null;
break;
}
return list;
}
} }

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

@ -53,6 +53,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
@ -143,18 +144,9 @@ public class FactoryProperties {
if (props.hasProperty("noexist-attrid")) { if (props.hasProperty("noexist-attrid")) {
p.setAttribute("noexist-attrid", props.getProperty("noexist-attrid")); p.setAttribute("noexist-attrid", props.getProperty("noexist-attrid"));
} }
for(String margin : HtmlConstants.MARGIN){
if(props.hasPropertyInChain("div", margin)){ parseMarginAttr( p, props);
String ss = props.getPropertyFromChain("div", margin); parsePaddingAttr( p, props);
p.setAttribute(margin, ss);
}
}
for(String padding : HtmlConstants.PADDING){
if(props.hasPropertyInChain("div", padding)){
String ss = props.getPropertyFromChain("div", padding);
p.setAttribute(padding, ss);
}
}
if(props.hasPropertyInChain("div", "text-indent")){ if(props.hasPropertyInChain("div", "text-indent")){
String ss = props.getPropertyFromChain("div", "text-indent"); String ss = props.getPropertyFromChain("div", "text-indent");
@ -190,6 +182,52 @@ public class FactoryProperties {
} }
} }
/**
* 解析margin属性
*
* @param p
* @param props
*/
private static void parseMarginAttr(Paragraph p, ChainedProperties props) {
if (props.hasPropertyInChain("div", Markup.CSS_KEY_MARGIN)) {
List<String> 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));
}
}
}
for (String margin : HtmlConstants.MARGIN) {
if (props.hasPropertyInChain("div", margin)) {
String ss = props.getPropertyFromChain("div", margin);
p.setAttribute(margin, ss);
}
}
}
/**
* 解析padding属性
*
* @param p
* @param props
*/
private static void parsePaddingAttr(Paragraph p, ChainedProperties props) {
if (props.hasPropertyInChain("div", Markup.CSS_KEY_PADDING)) {
List<String> list = Markup.parseNESW(props.getPropertyFromChain("div", Markup.CSS_KEY_PADDING));
if (null != list) {
for (int i = 0; i < HtmlConstants.PADDING.size() && i < list.size(); i++) {
p.setAttribute(HtmlConstants.PADDING.get(i), list.get(i));
}
}
}
for (String padding : HtmlConstants.PADDING) {
if (props.hasPropertyInChain("div", padding)) {
String ss = props.getPropertyFromChain("div", padding);
p.setAttribute(padding, ss);
}
}
}
public static Paragraph createParagraph(ChainedProperties props) { public static Paragraph createParagraph(ChainedProperties props) {
Paragraph p = new Paragraph(); Paragraph p = new Paragraph();
createParagraph(p, props); createParagraph(p, props);

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

@ -33,13 +33,15 @@ public class HtmlConstants {
INLINE_ELEMENTS.add(HtmlTags.I); INLINE_ELEMENTS.add(HtmlTags.I);
INLINE_ELEMENTS.add(HtmlTags.U); INLINE_ELEMENTS.add(HtmlTags.U);
INLINE_ELEMENTS.add(HtmlTags.EM); INLINE_ELEMENTS.add(HtmlTags.EM);
PADDING.add(Markup.CSS_KEY_PADDINGLEFT);
PADDING.add(Markup.CSS_KEY_PADDINGRIGHT);
PADDING.add(Markup.CSS_KEY_PADDINGTOP); PADDING.add(Markup.CSS_KEY_PADDINGTOP);
PADDING.add(Markup.CSS_KEY_PADDINGRIGHT);
PADDING.add(Markup.CSS_KEY_PADDINGBOTTOM); PADDING.add(Markup.CSS_KEY_PADDINGBOTTOM);
MARGIN.add(Markup.CSS_KEY_MARGINLEFT); PADDING.add(Markup.CSS_KEY_PADDINGLEFT);
MARGIN.add(Markup.CSS_KEY_MARGINRIGHT);
MARGIN.add(Markup.CSS_KEY_MARGINTOP); MARGIN.add(Markup.CSS_KEY_MARGINTOP);
MARGIN.add(Markup.CSS_KEY_MARGINRIGHT);
MARGIN.add(Markup.CSS_KEY_MARGINBOTTOM); MARGIN.add(Markup.CSS_KEY_MARGINBOTTOM);
MARGIN.add(Markup.CSS_KEY_MARGINLEFT);
} }
} }

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

@ -872,6 +872,9 @@ public class PdfChunk {
if (font.isUnderlined()) { if (font.isUnderlined()) {
s.append("text-decoration :underline;"); s.append("text-decoration :underline;");
} }
if (font.isStrikethru()) {
s.append("text-decoration :line-through;");
}
} }
public String javaColor2JSColorWithAlpha(Color var0) { public String javaColor2JSColorWithAlpha(Color var0) {

Loading…
Cancel
Save