ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何在Java中为word文档(.doc或.docx)设置背景颜色(页面颜色)?

2019-10-07 17:02:10  阅读:502  来源: 互联网

标签:java background-color ms-word apache-poi


通过像http://poi.apache.org这样的库,我们可以用任何文本颜色创建word文档,但是对于文本的背景或突出显示,我没有找到任何解决方案.

手动方式的单词页面颜色!:

https://support.office.com/en-us/article/Change-the-background-or-color-of-a-document-6ce0b23e-b833-4421-b8c3-b3d637e62524

这是我通过poi.apache创建word文档的主要代码

        // Blank Document
        @SuppressWarnings("resource")
        XWPFDocument document = new XWPFDocument();
        // Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File(file_address));
        // create Paragraph
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.RIGHT);

        XWPFRun run = paragraph.createRun();
        run.setFontFamily(font_name);
        run.setFontSize(font_size);
        // This only set text color not background!
        run.setColor(hex_color);

        for (String s : text_array) {
            run.setText(s);
            run.addCarriageReturn();
        }

        document.write(out);
        out.close();

解决方法:

更新:XWPF是创建word文档文件的最新方式,但只能通过旧格式版本(.doc)的HWPF设置背景

对于* .doc(即POI的HWPF组件):

>突出显示文字:
看看setHighlighted()
>背景颜色:

我想你的意思是段落的背景(AFAIK,Word也允许为整个页面着色,这是另一回事)

有一个setShading()允许您为段落提供前景色和背景色(通过SHDAbstractType的setCvFore()和setCvBack()). IIRC,它是您想要设置的前景,以便为您的段落着色.背景仅与由两种(交替)颜色组成的阴影相关.

基础数据结构名为Shd80([MS-DOC],2.9.248).还有SHDOperand([MS-DOC],2.9.249),它反映了Word97之前Word的功能. [MS-DOC]是二进制Word文件格式规范,可在MSDN上免费获得.

编辑:

以下是一些代码来说明以上内容:

    try {
        HWPFDocument document = [...]; // comes from somewhere
        Range range = document.getRange();

        // Background shading of a paragraph
        ParagraphProperties pprops = new ParagraphProperties();
        ShadingDescriptor shd = new ShadingDescriptor();
        shd.setCvFore(Colorref.valueOfIco(0x07)); // yellow; ICO
        shd.setIpat(0x0001); // solid background; IPAT
        pprops.setShading(shd);
        Paragraph p1 = range.insertBefore(pprops, StyleSheet.NIL_STYLE);
        p1.insertBefore("shaded paragraph");

        // Highlighting of individual characters
        Paragraph p2 = range.insertBefore(new ParagraphProperties(), StyleSheet.NIL_STYLE);     
        CharacterRun cr = p2.insertBefore("highlighted text\r");
        cr.setHighlighted((byte) 0x06); // red; ICO

        document.write([...]); // document goes to somewhere
    } catch (IOException e) {   
        e.printStackTrace();
    }

> ICO是一种颜色结构
> IPAT是预定义着色样式的列表

标签:java,background-color,ms-word,apache-poi
来源: https://codeday.me/bug/20191007/1868175.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有