0

是否有可能使用 Apache POI 将任何形状插入到 word 文档中。据我所知,有多种方法可以在文档中插入图片。但是是否可以使用 POI 绘制我自己的框、圆或矩形。如果可能,请帮助我这样做。谢谢你。

4

1 回答 1

2

这是一个使用 docx4j 以编程方式创建文本框、椭圆和矩形的示例。

POI 将非常相似(它使用 Xml Bean,而不是 JAXB)。

您可以使用一大块 XML,而不是全部在代码中完成;pRectangleViaXML 方法显示了这种方法。

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.vml.CTOval;
import org.docx4j.vml.CTPath;
import org.docx4j.vml.CTRect;
import org.docx4j.vml.CTShape;
import org.docx4j.vml.CTShapetype;
import org.docx4j.vml.CTStroke;
import org.docx4j.vml.CTTextbox;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTLanguage;
import org.docx4j.wml.CTTxbxContent;
import org.docx4j.wml.P;
import org.docx4j.wml.PPr;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.Pict;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;
import org.docx4j.wml.Text;

public class PictDemo  {

    static org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();

    public static void main(String[] args) throws Exception {

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

        mdp.getContent().add(pTextBox());
        mdp.getContent().add(pOval());
//      mdp.getContent().add(pRectangle());
        mdp.getContent().add(pRectangleViaXML());

        // Pretty print the main document part
        System.out.println(
                XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true) );

        String filename = System.getProperty("user.dir") + "/OUT_PictDemo.docx";
        wordMLPackage.save(new java.io.File(filename) );

    }

    public static P pTextBox() {


        P p = wmlObjectFactory.createP(); 

            // Create object for r
            R r = wmlObjectFactory.createR(); 
            p.getContent().add( r); 
                // Create object for rPr
                RPr rpr = wmlObjectFactory.createRPr(); 
                r.setRPr(rpr); 
                    // Create object for noProof
                    BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue(); 
                    rpr.setNoProof(booleandefaulttrue); 
                // Create object for pict (wrapped in JAXBElement) 
                Pict pict = wmlObjectFactory.createPict(); 
                JAXBElement<org.docx4j.wml.Pict> pictWrapped = wmlObjectFactory.createRPict(pict); 
                r.getContent().add( pictWrapped); 
        org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
                    // Create object for shapetype (wrapped in JAXBElement) 
                    CTShapetype shapetype = vmlObjectFactory.createCTShapetype(); 
                    JAXBElement<org.docx4j.vml.CTShapetype> shapetypeWrapped = vmlObjectFactory.createShapetype(shapetype); 
                    pict.getAnyAndAny().add( shapetypeWrapped); 
                        // Create object for stroke (wrapped in JAXBElement) 
                        CTStroke stroke = vmlObjectFactory.createCTStroke(); 
                        JAXBElement<org.docx4j.vml.CTStroke> strokeWrapped = vmlObjectFactory.createStroke(stroke); 
                        shapetype.getEGShapeElements().add( strokeWrapped); 
                            stroke.setJoinstyle(org.docx4j.vml.STStrokeJoinStyle.MITER);
                        // Create object for path (wrapped in JAXBElement) 
                        CTPath path = vmlObjectFactory.createCTPath(); 
                        JAXBElement<org.docx4j.vml.CTPath> pathWrapped = vmlObjectFactory.createPath(path); 
                        shapetype.getEGShapeElements().add( pathWrapped); 
                            path.setGradientshapeok(org.docx4j.vml.STTrueFalse.T);
                            path.setConnecttype(org.docx4j.vml.officedrawing.STConnectType.RECT);
                        shapetype.setCoordsize( "21600,21600"); 
                        shapetype.setSpt( new Float(202.0) );
                        shapetype.setConnectortype(org.docx4j.vml.officedrawing.STConnectorType.STRAIGHT);
                        shapetype.setVmlId( "_x0000_t202"); 
                        shapetype.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
                        shapetype.setHralign(org.docx4j.vml.officedrawing.STHrAlign.LEFT);
                        shapetype.setPath( "m,l,21600r21600,l21600,xe"); 
                    // Create object for shape (wrapped in JAXBElement) 
                    CTShape shape = vmlObjectFactory.createCTShape(); 
                    JAXBElement<org.docx4j.vml.CTShape> shapeWrapped = vmlObjectFactory.createShape(shape); 
                    pict.getAnyAndAny().add( shapeWrapped); 
                        // Create object for textbox (wrapped in JAXBElement) 
                        CTTextbox textbox = vmlObjectFactory.createCTTextbox(); 
                        JAXBElement<org.docx4j.vml.CTTextbox> textboxWrapped = vmlObjectFactory.createTextbox(textbox); 
                        shape.getEGShapeElements().add( textboxWrapped); 
                            // Create object for txbxContent
                            CTTxbxContent txbxcontent = wmlObjectFactory.createCTTxbxContent(); 
                            textbox.setTxbxContent(txbxcontent); 
                                // Create object for p
                                P p2 = wmlObjectFactory.createP(); 
                                txbxcontent.getContent().add( p2); 
                                    // Create object for pPr
                                    PPr ppr2 = wmlObjectFactory.createPPr(); 
                                    p2.setPPr(ppr2); 
                                        // Create object for rPr
                                        ParaRPr pararpr2 = wmlObjectFactory.createParaRPr(); 
                                        ppr2.setRPr(pararpr2); 
                                            // Create object for lang
                                            CTLanguage language2 = wmlObjectFactory.createCTLanguage(); 
                                            pararpr2.setLang(language2); 
                                                language2.setVal( "en-AU"); 
                                    // Create object for r
                                    R r2 = wmlObjectFactory.createR(); 
                                    p2.getContent().add( r2); 
                                        // Create object for rPr
                                        RPr rpr2 = wmlObjectFactory.createRPr(); 
                                        r2.setRPr(rpr2); 
                                            // Create object for lang
                                            CTLanguage language3 = wmlObjectFactory.createCTLanguage(); 
                                            rpr2.setLang(language3); 
                                                language3.setVal( "en-AU"); 
                                        // Create object for t (wrapped in JAXBElement) 
                                        Text text = wmlObjectFactory.createText(); 
                                        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text); 
                                        r2.getContent().add( textWrapped); 
                                            text.setValue( "my text box"); 
                            textbox.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
                        shape.setStrokeweight( ".5pt"); 
                        shape.setFillcolor( "white [3201]"); 
                        shape.setConnectortype(org.docx4j.vml.officedrawing.STConnectorType.STRAIGHT);
                        shape.setVmlId( "Text Box 5"); 
                        shape.setStyle( "position:absolute;margin-left:39pt;margin-top:17.25pt;width:1in;height:1in;z-index:251659264;visibility:visible;mso-wrap-style:none;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:top"); 
                        shape.setSpid( "_x0000_s1026"); 
                        shape.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
                        shape.setHralign(org.docx4j.vml.officedrawing.STHrAlign.LEFT);
                        shape.setType( "#_x0000_t202"); 

        return p;
        }   

    public static P pOval() {

        P p = wmlObjectFactory.createP(); 

            // Create object for r
            R r = wmlObjectFactory.createR(); 
            p.getContent().add( r); 
                // Create object for rPr
                RPr rpr = wmlObjectFactory.createRPr(); 
                r.setRPr(rpr); 
                    // Create object for noProof
                    BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue(); 
                    rpr.setNoProof(booleandefaulttrue); 
                // Create object for pict (wrapped in JAXBElement) 
                Pict pict = wmlObjectFactory.createPict(); 
                JAXBElement<org.docx4j.wml.Pict> pictWrapped = wmlObjectFactory.createRPict(pict); 
                r.getContent().add( pictWrapped); 
        org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
                    // Create object for oval (wrapped in JAXBElement) 
                    CTOval oval = vmlObjectFactory.createCTOval(); 
                    JAXBElement<org.docx4j.vml.CTOval> ovalWrapped = vmlObjectFactory.createOval(oval); 
                    pict.getAnyAndAny().add( ovalWrapped); 
                        oval.setStrokecolor( "#243f60 [1604]"); 
                        oval.setStrokeweight( "2pt"); 
                        oval.setFillcolor( "#4f81bd [3204]"); 
                        oval.setConnectortype(org.docx4j.vml.officedrawing.STConnectorType.STRAIGHT);
                        oval.setVmlId( "Oval 6"); 
                        oval.setStyle( "position:absolute;margin-left:183.75pt;margin-top:19.75pt;width:103.5pt;height:105.75pt;z-index:251660288;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:middle"); 
                        oval.setSpid( "_x0000_s1026"); 
                        oval.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
                        oval.setHralign(org.docx4j.vml.officedrawing.STHrAlign.LEFT);

        return p;
        }

    public static P pRectangle() {


        P p = wmlObjectFactory.createP(); 
            // Create object for r
            R r = wmlObjectFactory.createR(); 
            p.getContent().add( r); 
                // Create object for rPr
                RPr rpr = wmlObjectFactory.createRPr(); 
                r.setRPr(rpr); 
                    // Create object for noProof
                    BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue(); 
                    rpr.setNoProof(booleandefaulttrue); 
                // Create object for pict (wrapped in JAXBElement) 
                Pict pict = wmlObjectFactory.createPict(); 
                JAXBElement<org.docx4j.wml.Pict> pictWrapped = wmlObjectFactory.createRPict(pict); 
                r.getContent().add( pictWrapped); 
        org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
                    // Create object for rect (wrapped in JAXBElement) 
                    CTRect rect = vmlObjectFactory.createCTRect(); 
                    JAXBElement<org.docx4j.vml.CTRect> rectWrapped = vmlObjectFactory.createRect(rect); 
                    pict.getAnyAndAny().add( rectWrapped); 
                        rect.setStrokecolor( "#243f60 [1604]"); 
                        rect.setStrokeweight( "2pt"); 
                        rect.setFillcolor( "#4f81bd [3204]"); 
                        rect.setConnectortype(org.docx4j.vml.officedrawing.STConnectorType.STRAIGHT);
                        rect.setVmlId( "Rectangle 8"); 
                        rect.setStyle( "position:absolute;margin-left:48.75pt;margin-top:13.85pt;width:115.5pt;height:54pt;z-index:251661312;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:middle"); 
                        rect.setSpid( "_x0000_s1026"); 
                        rect.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
                        rect.setHralign(org.docx4j.vml.officedrawing.STHrAlign.LEFT);

        return p;
        }   

    public static P pRectangleViaXML() throws JAXBException {

        String openXML = "<w:p xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">"
                + "<w:pPr>"
                    + "<w:rPr>"
                        + "<w:lang w:val=\"en-AU\"/>"
                    +"</w:rPr>"
                +"</w:pPr>"
                + "<w:r>"
                    + "<w:rPr>"
                        + "<w:noProof/>"
                    +"</w:rPr>"
                    + "<w:pict>"
                        + "<v:rect fillcolor=\"#4f81bd [3204]\" id=\"Rectangle 8\"  o:spid=\"_x0000_s1026\" strokecolor=\"#243f60 [1604]\" strokeweight=\"2pt\" style=\"position:absolute;margin-left:48.75pt;margin-top:13.85pt;width:115.5pt;height:54pt;z-index:251661312;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:middle\"/>"
                    +"</w:pict>"
                +"</w:r>"
            +"</w:p>";

        return (P)XmlUtils.unmarshalString(openXML);    
    }
}
于 2014-04-01T22:08:12.187 回答