如何使用docx4j在文档中设置字体大小和字体系列 [英] How to set Font size and font family in document using docx4j

查看:2914
本文介绍了如何使用docx4j在文档中设置字体大小和字体系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileReader fr=new FileReader("E://HtmlToDoc//LETTER.html" );
BufferedReader br=new BufferedReader(fr); 
while( (s=br.readLine())!= null ){
    html=html+s;}

html="<html><body>"+html.substring(html.indexOf("<body>"));

/************************ Setting Page Size   **********************************/
Docx4jProperties.getProperties().setProperty("docx4j.PageSize", "B4JIS");
String papersize= Docx4jProperties.getProperties().getProperty("docx4j.PageSize", "B4JIS");

String landscapeString = Docx4jProperties.getProperties().getProperty("docx4j.PageOrientationLandscape", "true");
boolean landscape= Boolean.parseBoolean(landscapeString);

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf(papersize), landscape);

AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(html.getBytes());
//afiPart.setBinaryData(fileContent);

afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);

// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
wordMLPackage.getMainDocumentPart().addObject(ac);

// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
wordMLPackage.save(new java.io.File("E://HtmlToDoc//" + "test.docx"));

这是我的代码从HTML转换为Word文档。如何为这个word文档设置字体大小和字体系列。

This is my code converts from HTML to Word document. How to set font size and font family for this word document.

推荐答案

您将HTML内容拉入Word文档中一个 AltChunk 实例的集合,这意味着HTML作为单独的文件存在于docx'bundle'中,而不是作为实际Word文档流程的一部分。

You're pulling HTML content in to a Word document as a collection of AltChunk instances, which means the HTML exists in the docx 'bundle' as separate file, and not as part of the flow of the actual Word document.

如果您想将导入的内容作为原生MS Word内容进行操作,则需要导入源XHTML。这意味着docx4j会采用标记和(某些)相关样式,将它们转换为docx文件的各个组成部分(例如,表格,文本,运行和段落元素)。一旦你以这种方式导入了内容,就可以像对其他docx实体一样对其进行设置。

If you want to manipulate the imported content as native MS Word content, you need to import the source XHTML instead. This means that docx4j takes the mark-up and (some) related styles, converting them into the various constituent parts of a docx file (for example, table, text, run and paragraph elements). Once you have content imported in that way, you can style it as you would any other docx entities.

这篇关于如何使用docx4j在文档中设置字体大小和字体系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆