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

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);
于 2013-09-29T20:55:45.117 に答える