如何为Word文档(.doc或.DOCX)在Java中的背景颜色(页彩色)? [英] How to set background color (Page Color) for word document (.doc or .docx) in Java?

查看:1118
本文介绍了如何为Word文档(.doc或.DOCX)在Java中的背景颜色(页彩色)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过如 http://poi.apache.org 一些库,我们可以创建 word文档用任何文本颜色,但对于背景或文本的亮点,我没有找到任何解决方案。

页颜色以手动方式一句话:

<一个href=\"https://support.office.com/en-us/article/Change-the-background-or-color-of-a-document-6ce0b23e-b833-4421-b8c3-b3d637e62524\" rel=\"nofollow\">https://support.office.com/en-us/article/Change-the-background-or-color-of-a-document-6ce0b23e-b833-4421-b8c3-b3d637e62524

下面是我的主要code创建由poi.apache word文档

  //空白文档
        @燮pressWarnings(资源)
        XWPFDocument文档=新XWPFDocument();
        //写在文件系统中的文件
        FileOutputStream中出=新的FileOutputStream(新文件(file_address));
        //创建段落
        XWPFParagraph段= document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.RIGHT);        XWPFRun运行= paragraph.createRun();
        run.setFontFamily(font_name首);
        run.setFontSize(FONT_SIZE);
        //这个只设置文本颜色不是背景!
        run.setColor(hex_color);        对于(一个String:text_array){
            run.setText(多个);
            run.addCarriageReturn();
        }        文件撰写(出);
        out.close();


解决方案

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

有关的* .doc(即POI的HWPF分量):


  1. 文本高亮:
    看看<一个href=\"https://poi.apache.org/apidocs/org/apache/poi/hwpf/usermodel/CharacterRun.html#setHighlighted%28byte%29\"相对=nofollow> setHighlighted()


  2. 背景颜色:

    我想你指的是第(据我所知,Word还允许彩色整个页面是另当别论)的背景

    有<一个href=\"https://poi.apache.org/apidocs/org/apache/poi/hwpf/usermodel/Paragraph.html#setShading%28org.apache.poi.hwpf.usermodel.ShadingDescriptor%29\"相对=nofollow> setShading() 它允许您提供一个前景色和背景色(通过 setCvFore() setCvBack() SHDAbstractType )的一个段落。 IIRC,它是的前景的,你想,以你的色彩段进行设置。在背景的是仅适用于由两(交替)颜色阴影有关。

    底层的数据结构被命名为 Shd80 ([MS-DOC] 2.9.248)。也有反映的Word之前Word97的功能 SHDOperand ([MS-DOC] 2.9.249)。 [MS-DOC]是二进制字的文件格式规范是MSDN上免费提供的。


编辑:

下面是一些code来说明上述

  {尝试
        HWPFDocument文件= []; //来自某处
        范围范围= document.getRange();        //段落的背景底纹
        ParagraphProperties pprops =新ParagraphProperties();
        ShadingDescriptor SHD =新ShadingDescriptor();
        shd.setCvFore(Colorref.valueOfIco(0×07)); // 黄色; ICO
        shd.setIpat(0×0001); //纯色背景; IPAT
        pprops.setShading(SHD);
        第P1 = range.insertBefore(pprops,StyleSheet.NIL_STYLE);
        p1.insertBefore(阴影段落);        //单个字符的突出
        段P2 = range.insertBefore(新ParagraphProperties(),StyleSheet.NIL_STYLE);
        CharacterRun CR = p2.insertBefore(高亮文本\\ r);
        cr.setHighlighted((字节)0×06); //红色; ICO        的document.write([...]); //文件去的地方
    }赶上(IOException异常五){
        e.printStackTrace();
    }


  • ICO 是一个色彩结构

  • IPAT 是predefined底纹样式列表

By some libraries like http://poi.apache.org , we could create word document with any text color, but for background or highlight of the text, I didn't find any solution.

Page color for word in manual way!:

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

Here is my main code to create word document by poi.apache

        // 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();

解决方案

Update: XWPF is the newest way to create word document files, but setting background only possible by HWPF which is for old format version (.doc)

For *.doc (i.e. POI's HWPF component):

  1. Highlighting of text: Look into setHighlighted()

  2. Background color:

    I suppose you mean the background of a paragraph (AFAIK, Word also allows to color the entire page which is a different matter)

    There is setShading() which allows you to provide a foreground and background color (through setCvFore() and setCvBack() of SHDAbstractType) for a Paragraph. IIRC, it is the foreground that you would want to set in order to color your Paragraph. The background is only relevant for shadings which are composed of two (alternating) colors.

    The underlying data structure is named Shd80 ([MS-DOC], 2.9.248). There is also SHDOperand ([MS-DOC], 2.9.249) that reflects the functionality of Word prior to Word97. [MS-DOC] is the Binary Word File format specification which is freely available on MSDN.

Edit:

Here is some code to illustrate the above:

    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 is a color structure
  • IPAT is a list of predefined shading styles

这篇关于如何为Word文档(.doc或.DOCX)在Java中的背景颜色(页彩色)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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