aspose-words / Aspose.Words-for-Java

Aspose.Words for Java examples, plugins and showcases
https://products.aspose.com/words/java
MIT License
401 stars 206 forks source link

Insert html to Word,html forrmat is #85

Closed Wang0310 closed 2 years ago

Wang0310 commented 2 years ago

I want to convert HTML to doc format, but I find that the style doesn't work, including setting the line spacing of paragraphs code example: image

but in fact: image

Wang0310 commented 2 years ago

Sorry, the character format I wrote is wrong because it is not centered, but a lot of things in the HTML content do not take effect。 image image How should I write this style without right alignment as in the text?

WEIQ311 commented 2 years ago

是不是多了层\"

魏强

@.*** |

---- Replied Message ---- | From | @.> | | Date | 06/27/2022 12:39 | | To | @.> | | Cc | @.***> | | Subject | [aspose-words/Aspose.Words-for-Java] Insert html to Word,html forrmat is (Issue #85) |

I want to convert HTML to doc format, but I find that the style doesn't work, including setting the line spacing of paragraphs code example:

but in fact:

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

WEIQ311 commented 2 years ago
DocumentBuilder builder = new DocumentBuilder();
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setLineSpacingRule(LineSpacingRule.MULTIPLE);
paragraphFormat.setLineSpacing(24);
builder.writeln("word测试类型");
builder.writeln(LocalDateTime.now().toString());
builder.insertHtml("<a href='https://wwww.baid.com' _target='blank'>百度一下,都知道</a>");
builder.insertHtml("<p style='text-align:center;color:red;background-color:gray;'>文字居中显示</p>");
Document document = builder.getDocument();
document.save("./logs/test.docx");`
Wang0310 commented 2 years ago

是不是多了层\" | | 魏强 | | @. | ---- Replied Message ---- | From | @.> | | Date | 06/27/2022 12:39 | | To | @.> | | Cc | @.> | | Subject | [aspose-words/Aspose.Words-for-Java] Insert html to Word,html forrmat is (Issue #85) | I want to convert HTML to doc format, but I find that the style doesn't work, including setting the line spacing of paragraphs code example: but in fact: — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

嗯嗯,这个问题时多了一层。 已经找到原因了,现在第二个问题感觉不知道怎么设置,有些标签样式不生效了

Wang0310 commented 2 years ago

@WEIQ311 老哥好,你这个我试了下,跟我现在是一样的。我可能没有说清楚我的问题,我是有个业务场景会生成一段html页面,设置了一些样式,如附件里面的内容,我读取内容后插入生成word时,遇到的两个问题 1、配置的行距是不起作用,都是一个默认的行距,设置大小无效 2、有一些标签设置的样式,比如有个时间是靠右对齐,但生成word后不生效,想知道是怎么提出,替换标签还是有什么配置项是可以解决这个问题。 image

https://res.hwazhan.com/bg/upload/files/2022/6/1656329823006.txt?attname=html5.txt

AlexNosk commented 2 years ago

@Wang0310 @WEIQ311 The problem is that HTML document and MS Word document has quite different object model. And it is impossible to guaranty 100% fidelity after converting one to another. You can learn more about feature supported upon loading HTML from Aspose.Words documentation.

In your case, you can achieve what you need using table with two cells in your HTML.

Wang0310 commented 2 years ago

@AlexNosk ok, but I have another question. why can't the program set line spacing in HTML content take effect.

AlexNosk commented 2 years ago

@Wang0310 line-height style attribute corresponds Line Spacing in MS Word document. Aspose.Words take this attribute in account:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("<p style=\"line-height:20pt;\">test</p><p style=\"line-height:20pt;\">test</p>");
doc.save("C:\\Temp\\out.docx");

image

Wang0310 commented 2 years ago

@AlexNosk OK, I see. thank you very much.