docx4j正确放置段落

时间:2019-02-14 12:56:29

标签: java docx docx4j doc paragraph

当我绘制一个段落时,该段落偏移了打击,但是我以绝对位置绘制它,为什么会发生这种情况是因为段落间距? 此功能创建一个段落。

public static P pTextBox(List<String> lines, float boxHeight, float boxWidth, float top, float left, Integer font) {
    org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();
    P p = wmlObjectFactory.createP();

    Jc justification = wmlObjectFactory.createJc();
    justification.setVal(JcEnumeration.RIGHT);

    // 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 shape (wrapped in JAXBElement)
    org.docx4j.vml.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
    org.docx4j.wml.CTTxbxContent txbxcontent = wmlObjectFactory.createCTTxbxContent();
    textbox.setTxbxContent(txbxcontent);
    // Create object for p
    org.docx4j.wml.P p2 = wmlObjectFactory.createP();
    txbxcontent.getContent().add(p2);
    // Create object for pPr
    org.docx4j.wml.PPr ppr2 = wmlObjectFactory.createPPr();
    ppr2.setJc(justification);

    // SPACING
    PPrBase.Spacing pprbase = wmlObjectFactory.createPPrBaseSpacing();
    pprbase.setBefore(BigInteger.valueOf(0));
    pprbase.setAfter(BigInteger.valueOf(0));
    pprbase.setLine(BigInteger.valueOf(0));
    pprbase.setLineRule(STLineSpacingRule.AT_LEAST);
    ppr2.setSpacing(pprbase);

    p2.setPPr(ppr2);

    R r2 = wmlObjectFactory.createR();
    p2.getContent().add(r2);
    // Create object for rPr
    RPr rpr2 = wmlObjectFactory.createRPr();

    // // set font
    // if (font > 30)
    // font = 30;
    HpsMeasure m = new HpsMeasure();
    m.setVal(new BigInteger(font.toString()));

    rpr2.setSz(m);
    rpr2.setSzCs(m);

    r2.setRPr(rpr2);
    // Create object for lang
    CTLanguage language3 = wmlObjectFactory.createCTLanguage();
    language3.setVal("ar");
    rpr2.setLang(language3);
    rpr.setLang(language3);


    Text text = null;
    JAXBElement<org.docx4j.wml.Text> textWrapped = null;
    for (int i = 0; i < lines.size(); i++) {
        text = wmlObjectFactory.createText();
        text.setValue(lines.get(i));
        textWrapped = wmlObjectFactory.createRT(text);
        r2.getContent().add(textWrapped);
        r2.getContent().add(wmlObjectFactory.createBr());
    }
    // halamboha
    shape.setStyle(
            "position:absolute;font-size:3;margin-left: 0pt;margin-right: 0pt;margin-bottom: 0pt;margin-top: 0pt;border-style: none;width:"
                    + boxWidth * .24 + "pt;height:" + boxHeight * .24 + "pt;top:" + top * .24 + "pt;left:"
                    + left * .24 + "pt;");
    // org.docx4j.wml.STBorder.NONE;

    return p;
}

1 个答案:

答案 0 :(得分:0)

当您说“转移打击”时,我不明白您的意思,但是Word通常会写类似的内容:

<v:shape id="Text Box 2" o:spid="_x0000_s1026" type="#_x0000_t202" 
                       style="position:absolute;margin-left:0;margin-top:14.4pt;width:185.9pt;height:110.6pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal:center;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" >
                  <v:textbox style="mso-fit-shape-to-text:t"> 

您需要进行试验以查看是否遗漏了一些重要的东西,例如:mso-position-horizo​​ntal-relative:text; mso-position-vertical:absolute; mso-position-vertical-relative:text ;