【问题标题】:How can I set the complete page background for a Word page with Apache POI?如何使用 Apache POI 为 Word 页面设置完整的页面背景?
【发布时间】:2022-05-02 21:51:38
【问题描述】:

我今天已经搜索了一整天,但没有成功。首先,我创建了一个新的 Word 文档并尝试设置背景

 //Blank Document
    XWPFDocument doc = new XWPFDocument();

    //Write the Document in file system
    FileOutputStream out = new FileOutputStream(new File("test.docx"));

    File backgroundImage = new File("img.png");

doc.getDocument().addNewBackground().addNewDrawing().save(backgroundImage);

backgroundImage是我要设置的图片。

【问题讨论】:

  • 请提供最少的、可重现的代码。例如,这个方法getDocument() 在哪里声明?它有什么作用?在我看来,这不像 Apache POI。
  • //空白文档 XWPFDocument doc = new XWPFDocument(); //将Document写入文件系统 FileOutputStream out = new FileOutputStream(new File("test.docx"));文件 backgroundImage = new File("img.png"); doc.getDocument().addNewBackground().addNewDrawing(backgroundImage)
  • 不要将其添加为评论。使用该信息更新您的帖子。
  • 目标到底是什么?您将如何使用 Microsoft Word GUI 做到这一点?也许你混淆了页面背景和水印?

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


【解决方案1】:

您的问题似乎是关于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.0poi-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 中可见。它不会被打印出来。

如果需要打印页面背景,那么这将通过页眉中的形状来解决,也称为水印。但这是另一个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多