如何通过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>
答案 0 :(得分: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:background
xml计算xmlbeans对象,这将提供一种更简单的方法。如果你正常工作,请发送patch to POI!
答案 1 :(得分:1)
使用docx4j的在线代码生成器:
方法1
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;
}
}
方法2
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);