Browse Source

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

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

* commit '4e834caf0f6c9c6a332caaae6556a2c9e5863333':
  REPORT-33028 review 改进
  REPORT-33028 无插件PDF导出-HTML解析:块元素支持margin、padding 属性
release/10.0
Hugh.C 4 years ago
parent
commit
8b79473cf6
  1. 46
      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

46
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;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
/**
* A class that contains all the possible tagnames and their attributes.
@ -510,4 +513,47 @@ public class Markup {
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:
//如 margin:1px ,代表上下左右 的margin值都是 1px
for (int i = 0; i < 3; i++) {
list.add(list.get(0));
}
break;
case 2:
//如 margin:1px 2px ,代表上下方位的margin值为1px, 左右方位的margin值为 2px
list.add(list.get(0));
list.add(list.get(1));
break;
case 3:
//如 margin:1px 2px 3px ,代表上下方位的margin值分别为1px、3px, 左右方位的margin值为 2px
list.add(list.get(1));
break;
case 4:
//如 margin:1px 2px 3px 4px,就不用管了
break;
default:
//如 margin:1px 2px 3px 4px 5px,超了就违反了规则,在浏览器中该属性失效
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.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Chunk;
@ -143,18 +144,9 @@ public class FactoryProperties {
if (props.hasProperty("noexist-attrid")) {
p.setAttribute("noexist-attrid", props.getProperty("noexist-attrid"));
}
for(String margin : HtmlConstants.MARGIN){
if(props.hasPropertyInChain("div", margin)){
String ss = props.getPropertyFromChain("div", margin);
p.setAttribute(margin, ss);
}
}
for(String padding : HtmlConstants.PADDING){
if(props.hasPropertyInChain("div", padding)){
String ss = props.getPropertyFromChain("div", padding);
p.setAttribute(padding, ss);
}
}
parseMarginAttr( p, props);
parsePaddingAttr( p, props);
if(props.hasPropertyInChain("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 && 4 == list.size()) {
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) {
Paragraph p = new Paragraph();
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.U);
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_PADDINGRIGHT);
PADDING.add(Markup.CSS_KEY_PADDINGBOTTOM);
MARGIN.add(Markup.CSS_KEY_MARGINLEFT);
MARGIN.add(Markup.CSS_KEY_MARGINRIGHT);
PADDING.add(Markup.CSS_KEY_PADDINGLEFT);
MARGIN.add(Markup.CSS_KEY_MARGINTOP);
MARGIN.add(Markup.CSS_KEY_MARGINRIGHT);
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()) {
s.append("text-decoration :underline;");
}
if (font.isStrikethru()) {
s.append("text-decoration :line-through;");
}
}
public String javaColor2JSColorWithAlpha(Color var0) {

Loading…
Cancel
Save