Apache POI 3.13 Offline/Offset elements for a .doc

删除回忆录丶 提交于 2019-12-31 05:29:28

问题


So basically I have a requirement to validate a generated .doc when some content/ element Paragraph or Table pass to another page, and if some element/content it's alone on the other page I need to take another element/content and put it with the alone element/content

public void investigarDoc(XWPFDocument doc){
    try {          

        creacionDeFooter(doc);//FOOTER CREATION METHOD

        XWPFParagraph cuerpoObservaciones = doc.createParagraph(); //PARAGRAPH 1
        cuerpoObservaciones.setAlignment(ParagraphAlignment.DISTRIBUTE);
        XWPFRun imprimeObservaciones = cuerpoObservaciones.createRun();            
        seccionObservaciones(doc,imprimeObservaciones,cuerpoObservaciones); //TABLE CREATION METHOD

        XWPFParagraph cuerpoFirma = doc.createParagraph();  //PARAGRAPH 2
        cuerpoFirma.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun imprimeFirma = cuerpoFirma.createRun();
        seccionFirma(doc,imprimeFirma,cuerpoFirma); //SIGNATURE CREATION METHOD

        doc.write(new FileOutputStream("C:\\test.doc"));

    } catch (IOException iox) {
        iox.printStackTrace();
        System.out.println("Error: IOException Verificar Rutas de Archivos o Fotos!");
    }
}

//Footer Method

public void creacionDeFooter(XWPFDocument doc){ //FOOTER METHOD
    try {
        CTP ctp = CTP.Factory.newInstance();
        //this add page number incremental
        ctp.addNewR().addNewPgNum();

        XWPFParagraph parrafoFotter = new XWPFParagraph(ctp, doc);
        XWPFParagraph[] paragraphs = new XWPFParagraph[1];
        paragraphs[0] = parrafoFotter;

        //position of number
        parrafoFotter.setAlignment(ParagraphAlignment.RIGHT);

        CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();

        XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);
        headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, paragraphs);

        } catch (IOException e) {
            e.printStackTrace();
        }
}

//Method of Paragraph 1

public void seccionObservaciones(XWPFDocument doc, XWPFRun otrasObservaciones,XWPFParagraph observaciones){ //TABLE METHOD

    otrasObservaciones = observaciones.createRun();
    otrasObservaciones.setText(".");
    otrasObservaciones.addBreak();
     //create table
    XWPFTable table = doc.createTable();
    //create first row
    XWPFTableRow tableRowOne = table.getRow(0);
    tableRowOne.getCell(0).setText("Otras Operaciones/Observaciones");

    //create second row
    XWPFTableRow tableRowTwo = table.createRow();
    tableRowTwo.getCell(0).getTableRow(); 
    tableRowTwo.getCell(0).setText("col fore, row fore");

 }

//Method of Paragraph 2

public void seccionFirma(XWPFDocument doc, XWPFRun imprimeFirma,XWPFParagraph firma){ //SIGNATURE METHOD
    imprimeFirma = firma.createRun();
    imprimeFirma.addBreak();
    imprimeFirma.setFontFamily("Arial");
    imprimeFirma.addBreak();
    imprimeFirma.setText("_________________________________________");
    imprimeFirma.addBreak();
    imprimeFirma.setText("NOMBRE PERSONA");
    imprimeFirma.addBreak();
    imprimeFirma.setText("PUESTO");
    imprimeFirma.addBreak();
    imprimeFirma.setText("GRUPO FINANCIERO BLABLA BLA");
    imprimeFirma.setText(".");

} 

Here is a pic of the final result, all fine with this pic:

But the problem is if something like this happen:

This is an example that most happen with the validation

I try in base of the number page to handle this problem, but it seems that poi does not store the page num.

I need than the table and the signature paragraph come to the other page if some of the 2 it's on an new page.

I will be really grateful, many thanks! Regards.


回答1:


Seems as if you want keep together the lines and paragraphs on one page. This is possible with Word see https://support.office.com/en-us/article/Keep-text-together-af94e5b8-3a5a-4cb0-9c53-dea56b43d96d.

So we must set the properties KeepLines and KeepNext for each paragraph. Also for those within the table.

The out-dented lines are my additions.

public void seccionObservaciones(XWPFDocument doc, XWPFRun otrasObservaciones, XWPFParagraph observaciones){ //TABLE METHOD

    otrasObservaciones = observaciones.createRun();
    otrasObservaciones.setText(".");
    otrasObservaciones.addBreak();
     //create table
    XWPFTable table = doc.createTable();
    //create first row
    XWPFTableRow tableRowOne = table.getRow(0);
    tableRowOne.getCell(0).setText("Otras Operaciones/Observaciones");

for (XWPFParagraph p : tableRowOne.getCell(0).getParagraphs()) {
 p.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON);
 p.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);
}

    //create second row
    XWPFTableRow tableRowTwo = table.createRow();
    tableRowTwo.getCell(0).getTableRow(); 
    tableRowTwo.getCell(0).setText("col fore, row fore");

for (XWPFParagraph p : tableRowTwo.getCell(0).getParagraphs()) {
 p.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON);
 p.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);
}

 }

//main method

...
        creacionDeFooter(doc);//FOOTER CREATION METHOD

        XWPFParagraph cuerpoObservaciones = doc.createParagraph(); //PARAGRAPH 1
        cuerpoObservaciones.setAlignment(ParagraphAlignment.DISTRIBUTE);

cuerpoObservaciones.getCTP().getPPr().addNewKeepLines().setVal(STOnOff.ON); //has already a CPPr through setAlignment
cuerpoObservaciones.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);

        XWPFRun imprimeObservaciones = cuerpoObservaciones.createRun();            
        seccionObservaciones(doc,imprimeObservaciones,cuerpoObservaciones); //TABLE CREATION METHOD

        XWPFParagraph cuerpoFirma = doc.createParagraph();  //PARAGRAPH 2

cuerpoFirma.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON);
cuerpoFirma.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);

        cuerpoFirma.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun imprimeFirma = cuerpoFirma.createRun();
        seccionFirma(doc,imprimeFirma,cuerpoFirma); //SIGNATURE CREATION METHOD

        doc.write(new FileOutputStream("test.docx"));
...

org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff is needed for STOnOff.ON. But you using other org.openxmlformats.schemas.wordprocessingml.x2006.main. objects already. So you will know how to get this.

Edit

Forgot to mention. Please do not save XWPFDocument as *.doc file. A *.doc file is mainly used for the binary file format of Word up to version 2003. XWPFDocument is XML based and should be saved as *.docx.



来源:https://stackoverflow.com/questions/34822029/apache-poi-3-13-offline-offset-elements-for-a-doc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!