2

我如何通过 Apache POI 或其他 java 框架将背景图像添加到 docx 文档。我想在这样的结果文档中有一些 xml 块,其中定义了背景

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:background w:color="FFFFFF">
    <v:background id="_x0000_s1025" o:bwmode="white" o:targetscreensize="1024,768">
        <v:fill r:id="rId2" o:title="Alien 1" recolor="t" type="frame"/>
    </v:background>
</w:background>
<w:body>
      .....
</w:body></w:document>
4

2 回答 2

2

假设您想将背景元素添加到文档的根目录,您需要执行以下操作:

XWPFDocument doc = new XWPFDocument(OPCPackage.open("test.docx"));
if (doc.getDocument().getBackground() == null) {
   doc.getDocument.addNewBackground();
};

CTBackground bkgnd = doc.getDocument().getBackground();
bkgnd.setColor("FFFFFF");

现在,要将新背景添加到位于不同命名空间中的背景列表中,这有点棘手。我们会做类似的事情:

String xml = 
  "<v:background id=\"_x0000_s1025\" o:bwmode=\"white\" o:targetscreensize=\"1024,768\">" +
  "<v:fill r:id=\"rId2\" o:title=\"Alien 1\" recolor=\"t\" type=\"frame\"/>" +
  "</v:background>";
bkgnd.set(XmlToken.Factory.parse(xml));

如果您查看XWPFRun 之类的内容,您会看到一个从不同名称空间添加 xml 的示例。如果这一切都在 .docx 命名空间中,您可以使用 CT 对象完成所有操作,但遗憾的是,您的情况很复杂......

如果手动 XML 内容对您来说有点繁琐,请尝试使用 POI 处理 Word 添加的文件,然后使用该CTBackground对象。这可能会让您计算出内部v:backgroundxml 的 xmlbeans 对象,这将提供一种更简单的方法。如果你得到它的工作,发送补丁到 POI

于 2013-09-25T12:20:30.513 回答
1

使用 docx4j 的在线代码生成器:

方法一

import javax.xml.bind.JAXBElement;
import org.docx4j.vml.CTBackground;
import org.docx4j.vml.CTFill;
import org.docx4j.wml.CTBackground;


public class Foo { 
public CTBackground createBackground() {

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

CTBackground background = wmlObjectFactory.createCTBackground(); 
    background.setColor( "FFFFFF"); 
org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
    // Create object for background (wrapped in JAXBElement) 
    CTBackground background2 = vmlObjectFactory.createCTBackground(); 
    JAXBElement<org.docx4j.vml.CTBackground> backgroundWrapped = vmlObjectFactory.createBackground(background2); 
    background.getAnyAndAny().add( backgroundWrapped); 
        background2.setTargetscreensize( "1024,768"); 
        background2.setVmlId( "_x0000_s1025"); 
        background2.setBwmode(org.docx4j.vml.officedrawing.STBWMode.WHITE);
        // Create object for fill
        CTFill fill = vmlObjectFactory.createCTFill(); 
        background2.setFill(fill); 
            fill.setTitle( "Alien 1"); 
            fill.setId( "rId5"); 
            fill.setType(org.docx4j.vml.STFillType.FRAME);
            fill.setRecolor(org.docx4j.vml.STTrueFalse.T);

return background;
}
}

方法二

    String openXML = "<w:background w:color=\"FFFFFF\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">
                + "<v:background id=\"_x0000_s1025\" o:bwmode=\"white\" o:targetscreensize=\"1024,768\">
                      + "<v:fill o:title=\"Alien 1\" r:id=\"rId5\" recolor=\"t\" type=\"frame\"/>"

                +"</v:background>"

          +"</w:background>";
CTBackground background = (CTBackground)XmlUtils.unmarshalString(openXML);
于 2013-09-29T20:55:45.117 回答