doc4j doc zhuan html,Unable to resize the image while converting html to doc using doc4j

本文探讨了使用docx4j将HTML转换为DOCX文件时遇到的样式问题,包括图片引入导致样式变化、重复内容及文档兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

I am trying to generate the document from html using docx4j 3.3.1. I am facing below issue can some one help me on these?

HTML Code:

MonthlyReport

table

{

border:double #000;

table-layout: fixed;

vertical-align: top;

border-collapse: collapse;

width: 7.25in;

}

.main-title

{

margin-top:25%;

font-size:48pt;

font-family:Century Gothic;

color:#2F5897;

text-align:center;

font-weight:300;

}

.year

{

padding-top:10%;

text-align:center;

font-size:18px;

}

.other-detail

{

padding-top:10%;

padding-bottom:10%;

font-weight:bold;

text-align:center;

font-size:18px;

}

.cont

{

font-family:Palatino Linotype;

margin:0 auto;

margin-top:10px;

}

.cont-table

{

border:double #000;

table-layout: fixed;

vertical-align: top;

border-collapse: collapse;

width: 7.25in;

}

.cont h1

{

font-family:Century Gothic;

color:#2F5897;

font-weight:400;

margin-left:.15in;

}

.cont h4

{

font-family:Century Gothic;

font-weight:400;

margin-left:.15in;

}

.cont .school-detail

{

margin-left:.1in;

margin-right:.1in;

}

.cont ol

{

margin-left:1in;

margin-right:1in;

}

.cont li

{

margin-bottom:.25in;

}

.cont-heading

{

margin-left:.5in;

font-weight:bold;

text-decoration:underline;

margin-top:.35in;

}

span

{

display:block;

padding-bottom:10px;

}

.rheight

{

height: 3.6in;

}

img

{

width:100%;

}

.img-table

{

width:5in;

}

.img-table td

{

width:5in;

}

.cont-img

{

width:4in;

}

 
Monthly report
2016-2017
MONTH: January

CITY: TEST_CITY
FACILITATOR:

TEST_PERSON

 

Monthly Report

2016-2017

TEST SCHOOL

  1. Test Question


    TEST_ANSWER

Photos

Issue 1:

On Converting html without any image to document i am getting proper document style but if i use image in html styles are completely changed.

Code is given below

`private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException {

//Word Processing Package

WordprocessingMLPackage wordMLPackage = getWordMLPackage();

NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();

wordMLPackage.getMainDocumentPart().addTargetPart(ndp);

ndp.unmarshalDefaultNumbering();

//Saving the Document

wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes());

wordMLPackage.save(file);

}`

Issue 2:

On Using XHTMLImporterImpl Doc is generated with same content twice and styles are not proper.

Code is given below

private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException {

//Word Processing Package

WordprocessingMLPackage wordMLPackage = getWordMLPackage();

NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();

wordMLPackage.getMainDocumentPart().addTargetPart(ndp);

ndp.unmarshalDefaultNumbering();

// Convert the XHTML, and add it into the empty docx we made

XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

XHTMLImporter.setHyperlinkStyle("Hyperlink");

String baseurl = file.getPath();

baseurl = baseurl.substring(0, baseurl.lastIndexOf("\\"));

wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(html, baseurl));

//Saving the Document

wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes());

wordMLPackage.save(file);

}

Issue 3:

Generated document cannot be viewed in any application except MS Word.

回答1:

Regarding issue 1: addAltChunk just adds an altChunk (aka AlternativeFormatInput part), and since you just save the docx, you are relying on Word to convert the content, so your behaviour is Word specific.

Regarding issue 2: you're are getting duplicate content, since you do both XHTMLImporter.convert and addAltChunk.

来源:https://stackoverflow.com/questions/41583022/unable-to-resize-the-image-while-converting-html-to-doc-using-doc4j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值