我使用docx4j生成带有.html文件的.docx文件。 首先用jTidy在xhtml中转换html文件。 这个文件是我文件的正文。
我对标题做了同样的事情,我也有一个文件。
我可以生成一个标题并将其添加到我的文档中,但只能作为纯文本,而不是html。
以下是标题的代码:
//Header Part start
HeaderPart headerPart = new HeaderPart();
Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
String hdrXml = "<w:hdr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:p>"
+ "<w:pPr>"
//+ "<w:pStyle w:val=\"Header\"/>"
+ "<w:jc w:val=\"center\"/>"
+ "</w:pPr>"
+ "<w:r>"
+ "<w:t xml:space=\"preserve\">" + myFileContentInString + "</w:t>"
+ "</w:r>"
// + "<w:fldSimple w:instr=\" PAGE \\* MERGEFORMAT \">"
// + "<w:r>"
// + "<w:rPr>"
// + "<w:noProof/>"
// + "</w:rPr>"
// + "</w:r>"
// + "</w:fldSimple>"
+ "</w:p>"
+ "</w:hdr>";
Hdr hdr = (Hdr) XmlUtils.unmarshalString(hdrXml);
wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getFirstHeader();
headerPart.setJaxbElement(hdr);
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = objectFactory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(rel.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
//Header Part End
xhtml内容位于&#34; myFileContentInString&#34;。
我无法找到关于此事的内容,所以如果有人有任何想法?
编辑:在您的回答之后,我以这种方式更新了我的代码(这是完整的代码):
String inputfilepath = "Offers/" + param.getKey1() + "_" + param.getKey2() + "/c.xhtml";
String inputfilepath2 = "Offers/" + param.getKey1() + "_" + param.getKey2() + "/cc.xhtml";
// Create an empty docx package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
ObjectFactory objectFactory = Context.getWmlObjectFactory();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
xHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(inputfilepath), null));
//Header Part start
HeaderPart headerPart = new HeaderPart();
Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
Hdr hdr = Context.getWmlObjectFactory().createHdr();
hdr.getContent().addAll(xHTMLImporter.convert(new File(inputfilepath2), null));
wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getFirstHeader();
headerPart.setJaxbElement(hdr);
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = objectFactory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(rel.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
// Saving file
wordMLPackage.save(new java.io.File("Offers/" + param.getKey1() + "_" + param.getKey2() + "/html_output.docx"));
&#34; inputfilepath2&#34;包含我的标题xhtml文件的路径。 我试图在我的标题中插入一个简单的Hello World,但它似乎正在采用&#34; inputfilepath&#34;身体和头部。
答案 0 :(得分:0)
假设您有对正确headerPart的引用,例如:
...
CMD chown mysql:mysql -R /data/db/