Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修几个问题 #115

Open
wants to merge 1 commit into
base: poi-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/main/java/org/ddr/poi/html/HtmlRenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import java.net.URI;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -1236,7 +1237,7 @@ public void renderNode(Node node) {
if (isElement) {
Element element = ((Element) node);
renderElement(element);
} else if (node instanceof TextNode) {
} else if (node instanceof TextNode && !"tr".equals(node.parent().nodeName())) {
renderText(((TextNode) node).getWholeText());
}
}
Expand Down Expand Up @@ -1281,8 +1282,8 @@ public void renderElement(Element element) {
XWPFTable xwpfTable = container.insertNewTbl(globalCursor);
globalCursor.pop();
if (dedupeParagraph != null && !numberingContext.contains(dedupeParagraph)) {
if (!dedupeParagraph.equals(getRun().getParent()) && isEmptyParagraph(dedupeParagraph)) {
removeParagraph(container, dedupeParagraph);
if (!dedupeParagraph.equals(getRun().getParent())) {
tryRemoveParagraph(container);
}
unmarkDedupe();
}
Expand Down Expand Up @@ -1328,17 +1329,25 @@ public void renderElement(Element element) {
renderElementEnd(element, this, elementRenderer, blocked);
}

private boolean isEmptyParagraph(XWPFParagraph paragraph) {
for (XWPFRun run : paragraph.getRuns()) {
if (StringUtils.isNotBlank(run.text())) {
return false;
}
if (!run.getEmbeddedPictures().isEmpty()) {
return false;
private void tryRemoveParagraph(IBody container) {
System.out.println("tryRemoveParagraph");
List<XWPFRun> runs = dedupeParagraph.getRuns();
int size = runs.size();
if (size == 0) {
this.removeParagraph(container, dedupeParagraph);
return;
}
int index = runs.size() - 1;
XWPFRun run = runs.get(index);
String text = run.text();
if ((text.isEmpty() || "\n".equals(text)) && run.getEmbeddedPictures().isEmpty()) {
CTP ctp = dedupeParagraph.getCTP();
if (size == 1 && ctp.sizeOfOMathArray() == 0 && ctp.sizeOfOMathParaArray() == 0) {
this.removeParagraph(container, dedupeParagraph);
} else {
dedupeParagraph.removeRun(index);
}
}
CTP ctp = paragraph.getCTP();
return ctp.sizeOfOMathArray() == 0 && ctp.sizeOfOMathParaArray() == 0;
}

private void removeParagraph(IBody container, XWPFParagraph paragraph) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/ddr/poi/html/tag/ImageRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean renderStart(Element element, HtmlRenderContext context) {
* @param context 渲染上下文
* @param src 数据
*/
private void handleData(Element element, HtmlRenderContext context, String src) {
protected void handleData(Element element, HtmlRenderContext context, String src) {
int index = src.indexOf(HtmlConstants.COMMA.charAt(0));
String data = src.substring(index + 1);
String declaration = src.substring(0, index);
Expand Down Expand Up @@ -185,7 +185,7 @@ protected ImageType typeOf(BufferedImage image) {
* @param context 渲染上下文
* @param src 图片链接地址
*/
private void handleRemoteImage(Element element, HtmlRenderContext context, String src) {
protected void handleRemoteImage(Element element, HtmlRenderContext context, String src) {
HttpURLConnection connect = null;
try {
connect = HttpURLConnectionUtils.connect(src);
Expand All @@ -210,7 +210,7 @@ private void handleRemoteImage(Element element, HtmlRenderContext context, Strin
}
}

private ImageInfo analyzeImage(ByteArrayInputStream inputStream, boolean svg) throws IOException, InvalidFormatException {
protected ImageInfo analyzeImage(ByteArrayInputStream inputStream, boolean svg) throws IOException, InvalidFormatException {
final long length = inputStream.available();
// actual image data stream
ByteArrayInputStream stream = inputStream;
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/ddr/poi/html/HtmlRenderTableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 - 2021 Draco, https://github.com/draco1023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ddr.poi.html;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import org.ddr.poi.FileReader;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

class HtmlRenderTableTest {

@Test
void doRender() throws IOException {
HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
Configure configure = Configure.builder()
.bind("text", htmlRenderPolicy)
.build();
Map<String, Object> data = new HashMap<>();
data.put("text", FileReader.readFile("/4.html"));

try (InputStream inputStream = HtmlRenderPolicyTest.class.getResourceAsStream("/4.docx")) {
XWPFTemplate.compile(inputStream, configure).render(data).writeToFile("4_out.docx");
}
}

}
Binary file added src/test/resources/4.docx
Binary file not shown.
9 changes: 9 additions & 0 deletions src/test/resources/4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test
<table><tbody>
<tr><td></td>
这里出现任何文字都会影响下一行的解析
</tr>
<tr><td>
test2
</td></tr>
</tbody></table>