2013-09-24 240 views
2

如何通过Apache POI或其他Java框架将背景图像添加到docx文档。 我想有一些XML块,其中定义的背景下,这样的如何通过apache poi添加背景图像到docx文件?

<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

你知道你想要把该文件吗?如果是这样,应该很容易抓住底层的CT对象并添加它。如果你明确了你想要的文档的位置,很容易就能告诉你如何做到这一点 – Gagravarr

+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"); 

现在,要将您的新背景添加到不同命名空间中的背景列表中,这有点棘手。如果你像XWPFRun,你会看到一个不同的命名空间在XML中添加的例子看

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

:我们会做这样的事情。如果所有这些都在.docx命名空间中,你可以使用CT对象完成所有操作,但遗憾的是你的设计很复杂......

如果手动XML东西对你来说有点烦,请尝试使用POI来处理与Word中添加的文件,并与CTBackground对象玩耍。这可以让你找出xmlbeans对象的内部v:background xml,这将提供一个更简单的方法。如果你得到它的工作,发送patch to POI

+0

好的,谢谢你,我会试着玩CTBackground,如果这样做,我会让你知道的。 –

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); 
+0

请注意,背景图像与图片水印不同。后者可能是你想要的。分别查看docx4j样本BackgroundImage和WatermarkPicture。 – JasonPlutext