重新启动XWPFParagraph中的编号而不添加新级别

时间:2018-10-09 07:24:36

标签: java oracle apache-poi

我使用oracle java源从数据库中获取.docx模板,然后需要创建N个文档文本副本,并替换了某些文本。文档包含编号列表,当我再添加1张原始文本时,该列表将继续。 我已经尝试过this,但是它将新级别添加到列表中,我不知道如何在不添加更多级别的情况下重新开始编号。

我的代码:

while( rset.next() )
    {
      doc.getLastParagraph().createRun().addBreak( BreakType.PAGE );

      iCHK = "0";

      for( int ip = 0; ip < origBodyElsCnt; ++ip )
      {
        IBodyElement be = doc.getBodyElements().get(ip);

        if( be instanceof XWPFParagraph )
        {
          XWPFParagraph oldPara = (XWPFParagraph)be; 

          XWPFParagraph newPara = doc.createParagraph();

          cloneParagraph( doc, oldPara, newPara, rset, numbering, numberStyles, iCHK );


        }
        else if( be instanceof XWPFTable )
        {
          XWPFTable newTabl = doc.createTable();
          cloneTable( doc, (XWPFTable)be, newTabl, rset, numbering, numberStyles, iCHK );
        }


      }
    }

private static void cloneTable(XWPFDocument d, XWPFTable source, XWPFTable target, ResultSet rset, XWPFNumbering numbering, Map<String, XWPFAbstractNum> numberStyles, String iCHK )
throws java.sql.SQLException
{
    target.getCTTbl().setTblPr(source.getCTTbl().getTblPr());
    target.getCTTbl().setTblGrid(source.getCTTbl().getTblGrid());
    for (int r = 0; r<source.getRows().size(); r++) {
        XWPFTableRow targetRow = target.createRow();
        XWPFTableRow row = source.getRows().get(r);
        targetRow.getCtRow().setTrPr(row.getCtRow().getTrPr());
        for (int c=0; c<row.getTableCells().size(); c++) {
            //newly created row has 1 cell
            XWPFTableCell targetCell = c==0 ? targetRow.getTableCells().get(0) : targetRow.createCell();
            XWPFTableCell cell = row.getTableCells().get(c);
            targetCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());
            XmlCursor cursor = targetCell.getParagraphArray(0).getCTP().newCursor();
            for (int p = 0; p < cell.getBodyElements().size(); p++) {
                IBodyElement elem = cell.getBodyElements().get(p);
                if (elem instanceof XWPFParagraph) {
                    XWPFParagraph targetPar = targetCell.insertNewParagraph(cursor);
                    cursor.toNextToken();
                    XWPFParagraph par = (XWPFParagraph) elem;
                    cloneParagraph(d, par, targetPar, rset, numbering, numberStyles, iCHK);
                } else if (elem instanceof XWPFTable) {
                    XWPFTable targetTable = targetCell.insertNewTbl(cursor);
                    XWPFTable table = (XWPFTable) elem;
                    cloneTable(d, table, targetTable, rset, numbering, numberStyles, iCHK);
                    cursor.toNextToken();
                }
            }
            //newly created cell has one default paragraph we need to remove
            targetCell.removeParagraph(targetCell.getParagraphs().size()-1);
        }
    }
    //newly created table has one row by default. we need to remove the default row.
    target.removeRow(0);
}

private static void cloneParagraph(XWPFDocument d, XWPFParagraph source, XWPFParagraph target, ResultSet rset, XWPFNumbering numbering, Map<String, XWPFAbstractNum> numberStyles, String iCHK)
throws java.sql.SQLException
{
    glueAllPlaceholderPartsToFirstRun( source, '#', '#' );
    target.getCTP().setPPr(source.getCTP().getPPr());
    BigInteger bi = target.getNumID();
    //String iCHK = 1;

    if (bi != null)
    {
      String styleName = source.getStyle();
      XWPFRun rTEST = source.getRuns().get(0);
      String sTEST = rTEST.getText(0);
      // restart numbering
      if ( iCHK.equals("0") ){
        XWPFNum num = restartNumbering(styleName, numbering, numberStyles);
        // set numbering for paragraph
        target.setNumID(num.getCTNum().getNumId());
        CTNumPr numProp = target.getCTP().getPPr().getNumPr();
        numProp.addNewIlvl().setVal(BigInteger.ZERO);
        iCHK = "1";
      };

    };     

    #sql
      {
        begin
          PKG_TRACE.REGISTER($$PLSQL_UNIT,$$PLSQL_LINE,:IN iCHK);
        end;
      };

    for (int i=0; i<source.getRuns().size(); i++ ) {
        XWPFRun run = source.getRuns().get(i);
        XWPFRun targetRun = target.createRun();
        //copy formatting
        targetRun.getCTR().setRPr(run.getCTR().getRPr());
        //no images just copy text
        String txt = run.getText(0);

        if( txt != null )
        {
          txt = txt
              .replace("#sDOCDOG#",rset.getString("DAT"))
              .replace("#sDOCORD#",rset.getString("ORD"))
              .replace("#sDOCDOV#",rset.getString("DOV"))
              .replace("#sDOCUSL#",rset.getString("USL"))
              .replace("#sDOCDAT#",rset.getString("DDAT"))
              .replace("#sDOCFIO#",rset.getString("FIO"))
              .replace("#sDOCFAM#",rset.getString("IOF"))   
              ;
        }

        targetRun.setText(txt);
    }
}

/**
 * This creates a new num based upon the specified numberStyle
 * @param numberStyle
 * @return
 */
private static XWPFNum restartNumbering(String numberStyle, XWPFNumbering numbering, Map<String, XWPFAbstractNum> numberStyles) 
throws java.sql.SQLException{



    XWPFAbstractNum abstractNum = numberStyles.get("2"/*numberStyle*/);


    BigInteger numId = numbering.addNum(abstractNum.getAbstractNum().getAbstractNumId());  

    XWPFNum num = numbering.getNum(numId);
    CTNumLvl lvlOverride = num.getCTNum().addNewLvlOverride();
    lvlOverride.setIlvl(BigInteger.ZERO);
    CTDecimalNumber number = lvlOverride.addNewStartOverride();
    number.setVal(BigInteger.ONE);
    return num;
}

/**
* first discover all the numbering styles defined in the template.
*
*/
protected static void initNumberingStyles(XWPFDocument doc, XWPFNumbering numbering, Map<String, XWPFAbstractNum> numberStyles) 
throws java.sql.SQLException{
    //numbering = doc.getNumbering();

    // Use a custom wrapper class in order to access the protected fields.
    NumberingUtil util = new NumberingUtil(numbering);

    for (XWPFAbstractNum abstractNum : util.getAbstractNums()) {
        if (abstractNum != null) {
            CTString pStyle = abstractNum.getCTAbstractNum().getLvlArray(0).getPStyle();
            if (pStyle != null) {

                String s1 = pStyle.getVal();
                String s2 = abstractNum.toString();
                numberStyles.put(pStyle.getVal(), abstractNum);
            }
        }
    }

0 个答案:

没有答案