java - How to add background image to docx document through apache poi? -


how can add background image docx document through apache poi or other java framework. have xml block, defined background, in result document that

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

assuming want add background element root of document, you'll need like:

xwpfdocument doc = new xwpfdocument(opcpackage.open("test.docx")); if (doc.getdocument().getbackground() == null) {    doc.getdocument.addnewbackground(); };  ctbackground bkgnd = doc.getdocument().getbackground(); bkgnd.setcolor("ffffff"); 

now, add new background backgrounds list, in different namespace, it's bit trickier. we'd like:

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

if in xwpfrun you'll see example of adding in xml different namespace. in .docx namespace ct objects, sadly yours complicated case...

if manual xml stuff bit fiddly you, try using poi process file added in word, , play around ctbackground object. might let work out xmlbeans object inner v:background xml, offer simpler way of doing it. if working, send in patch poi!


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -