Docx4j 在页眉同时 添加logo图片和文字,logo图片显示在左边,文字显示在右边
2017-03-09 11:13
801 查看
Docx4j在页眉 同时加入图片和文字有两种实现方式,一种是用HTML写一个模板,通过模板一次加入,另一种是通过docx4j直接操作
一、Html方式 上代码
public class Docx4JSample {
public static
void main(String []args)
throws Exception{
Docx4JSample
docx4jSample = new Docx4JSample();
WordprocessingMLPackage
worMLPackage = docx4jSample.createWordpreocessMLPackage();
MainDocumentPart
mpart = worMLPackage.getMainDocumentPart();
ObjectFactory
factory = Context.getWmlObjectFactory();
Relationship
relationship1 = docx4jSample.createHeaderPart(worMLPackage,
mpart,
factory,
true, "3");
docx4jSample.createHeaderReference(worMLPackage,
mpart, factory,
relationship1);
docx4jSample.saveWordPackage(worMLPackage,
new File("./testpaper.docx"));
}
public void createHeaderReference(WordprocessingMLPackage
wordprocessingMLPackage,MainDocumentPart
t,
ObjectFactory
factory, Relationship relationship)
throws Exception{
List<SectionWrapper>
sections = wordprocessingMLPackage.getDocumentModel().getSections();
SectPr sectPr =sections.get(sections.size()-1).getSectPr();
if (sectPr ==
null ){
sectPr =
factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size()-1).setSectPr(sectPr);
}
HeaderReference
headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
//这里关联RelationShip
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
//创建页眉
public Relationship createHeaderPart(WordprocessingMLPackage
wordprocessingMLPackage,
MainDocumentPart
mpartDocumentPart , ObjectFactory factory ,
boolean isUnderLine ,
String underLineSize)
throws Exception{
HeaderPart headerPart =
new HeaderPart();
Relationship
relationship = mpartDocumentPart.addTargetPart(headerPart);
String
headerString="<html><body width=\"90%\"><table width=\"100%\">
<tr width=\"20%\">
"
+ "<th align=\"left\"><img width=40 height=40 src=\"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/"
+ "static/superman/img/logo/bd_logo1_31bdc765.png\"></th><th align=\"right\">"
+ "页眉右边的文字<br>这里还可以起一行</th>
</tr> </table></body></html>";
headerPart.addAltChunk(AltChunkType.Xhtml,
headerString.getBytes());
return
relationship;
}
public void saveWordPackage(WordprocessingMLPackage
wordPackage , File
file) throws Exception{
wordPackage.save(file);
}
}
结果如下:
另一种方式 见下一篇文章
一、Html方式 上代码
public class Docx4JSample {
public static
void main(String []args)
throws Exception{
Docx4JSample
docx4jSample = new Docx4JSample();
WordprocessingMLPackage
worMLPackage = docx4jSample.createWordpreocessMLPackage();
MainDocumentPart
mpart = worMLPackage.getMainDocumentPart();
ObjectFactory
factory = Context.getWmlObjectFactory();
Relationship
relationship1 = docx4jSample.createHeaderPart(worMLPackage,
mpart,
factory,
true, "3");
docx4jSample.createHeaderReference(worMLPackage,
mpart, factory,
relationship1);
docx4jSample.saveWordPackage(worMLPackage,
new File("./testpaper.docx"));
}
public void createHeaderReference(WordprocessingMLPackage
wordprocessingMLPackage,MainDocumentPart
t,
ObjectFactory
factory, Relationship relationship)
throws Exception{
List<SectionWrapper>
sections = wordprocessingMLPackage.getDocumentModel().getSections();
SectPr sectPr =sections.get(sections.size()-1).getSectPr();
if (sectPr ==
null ){
sectPr =
factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size()-1).setSectPr(sectPr);
}
HeaderReference
headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
//这里关联RelationShip
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
//创建页眉
public Relationship createHeaderPart(WordprocessingMLPackage
wordprocessingMLPackage,
MainDocumentPart
mpartDocumentPart , ObjectFactory factory ,
boolean isUnderLine ,
String underLineSize)
throws Exception{
HeaderPart headerPart =
new HeaderPart();
Relationship
relationship = mpartDocumentPart.addTargetPart(headerPart);
String
headerString="<html><body width=\"90%\"><table width=\"100%\">
<tr width=\"20%\">
"
+ "<th align=\"left\"><img width=40 height=40 src=\"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/"
+ "static/superman/img/logo/bd_logo1_31bdc765.png\"></th><th align=\"right\">"
+ "页眉右边的文字<br>这里还可以起一行</th>
</tr> </table></body></html>";
headerPart.addAltChunk(AltChunkType.Xhtml,
headerString.getBytes());
return
relationship;
}
public void saveWordPackage(WordprocessingMLPackage
wordPackage , File
file) throws Exception{
wordPackage.save(file);
}
}
结果如下:
另一种方式 见下一篇文章
相关文章推荐
- [置顶] Java 利用poi给word添加页眉页脚,页眉左边为公司logo图片,右为公司全称,页脚左为公司地址,右为页码总页数
- android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边
- Button 左边图片右边文字居中显示样式
- docx4j 在页眉同时增加图片和文字的方法
- Visual Basic 2005——如何在DataGridView控件的储存格中同时显示出文字与图片
- Visual Basic 2005——如何在DataGridView控件的储存格中同时显示出文字与图片
- css实现div自动添加滚动条(图片或文字等超出时显示)
- 在Flex的png图片中添加文字信息并透明显示
- android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法
- opencv学习笔记11 在图片中添加文字,点击鼠标显示位置坐标
- UIButton上同时显示图片和文字的方法
- Devexpress 中aspxhyperlink 同时显示图片和文字
- DIV+CSS一种简单的左边图片右边多行文字的布局
- Android自定义组件,实现图片和文字同时显示
- java导出word、pdf之添加页眉----页眉(指定格式,包括图片和文字)
- Visual Basic 2005——如何在DataGridView控件的储存格中同时显示出文字与图片
- 使用mmit进行移动开发,如何在一行同时显示图片和文字.
- 老大要求把Word文档加上公司的标识,公司名文字或者logo图片都可以,但要求是用电脑打开(电子文档时)能看见标识,如果打印出来(纸质文档)就不显示出来,如何实现?
- FlexViewer3.4中CompositeSymbol复合型标注中添加图片与文字后第一次点击后不显示图片,二次点击后才显示
- UIButton 同时添加图片和文字 防止图片变形