您的问题似乎是关于add, change, or delete the background color in Word。
这包括两个设置。
首先,必须在文档设置中启用显示背景形状。
其次,文档必须设置有颜色的背景。这可能是白色的,但必须设置。
然后可以设置可选的图片参考。然后该图片被用作背景图块。 Microsoft Word 本身仍然使用 VML 将图片引用设置为背景。这比使用绘图要简单得多。虽然根据 Office Open XML 规范使用CTBackground 中的绘图是可能的,但直到现在我还没有在实际使用中看到过这种情况。
apache poi 的高级类层不支持这些。但可以使用低级别的org.openxmlformats.schemas.wordprocessingml.x2006.main.* 类来解决。
另一个挑战是获取XWPFSettings。没有getter方法。所以我们需要使用反射从XWPFDocument获取它。然后从XWPFSettings 获取CTSettings 也是如此。
以下完整示例显示了如何设置背景颜色和/或背景平铺图片。该代码使用当前的apache po 5.2.0 和poi-ooxml-full-5.2.0.jar 进行了测试和工作。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class CreateWordPageBackground {
static XWPFSettings getSettings(XWPFDocument document) throws Exception {
java.lang.reflect.Field settings = XWPFDocument.class.getDeclaredField("settings");
settings.setAccessible(true);
return (XWPFSettings)settings.get(document);
}
static void setDisplayBackgroundShape(XWPFSettings settings, boolean booleanOnOff) throws Exception {
java.lang.reflect.Field _ctSettings = XWPFSettings.class.getDeclaredField("ctSettings");
_ctSettings.setAccessible(true);
CTSettings ctSettings = (CTSettings )_ctSettings.get(settings);
CTOnOff onOff = CTOnOff.Factory.newInstance();
onOff.setVal(booleanOnOff);
ctSettings.setDisplayBackgroundShape(onOff);
}
static void setBackgroundPictureId(CTBackground background, String rId) throws Exception {
String vmlBackgroundXML = ""
+ "<v:background xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" "
+ "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" "
+ "id=\"_x0000_s1025\" o:bwmode=\"white\" o:targetscreensize=\"1024,768\">"
+ "<v:fill r:id=\"" + rId + "\" recolor=\"t\" type=\"frame\"/>"
+ "</v:background>"
;
org.apache.xmlbeans.XmlObject vmlBackgroundXmlObject = org.apache.xmlbeans.XmlObject.Factory.parse(vmlBackgroundXML);
background.set(vmlBackgroundXmlObject);
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// we need document settings to set display-background-shape
XWPFSettings settings = getSettings(document);
setDisplayBackgroundShape(settings, true);
// set a background color
CTBackground background = document.getDocument().addNewBackground();
background.setColor("FF0000");
// crate a picture reference in document
InputStream is = new FileInputStream("./logo.png");
String rId = document.addPictureData(is, Document.PICTURE_TYPE_PNG);
// set a background picture
setBackgroundPictureId(background, rId);
background.setColor("FFFFFF");
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Text in Body ...");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("./CreateWordPageBackground.docx");
document.write(out);
out.close();
document.close();
}
}
背景颜色和背景平铺图片仅在 Microsoft Word GUI 中可见。它不会被打印出来。
如果需要打印页面背景,那么这将通过页眉中的形状来解决,也称为水印。但这是另一个问题。