【问题标题】:Apache POI 3.13 Offline/Offset elements for a .doc.doc 的 Apache POI 3.13 离线/偏移元素
【发布时间】:2017-11-17 21:32:16
【问题描述】:

所以基本上我需要在某些内容/元素段落或表格传递到另一个页面时验证生成的 .doc,如果某些元素/内容单独在另一个页面上,我需要获取另一个元素/内容并放入它与单独的元素/内容

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!");
    }
}

//页脚方法

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();
        }
}

//第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");

 }

//第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(".");

} 

这是最终结果的图片,这张图片很好:

但问题是如果发生这样的事情:

这是一个在验证中最常发生的例子

我尝试在数字页的基础上处理这个问题,但是poi似乎没有存储页码。

如果其中一些在新页面上,我需要将表格和签名段落转到另一页。

我将非常感激,非常感谢!问候。

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    似乎您想在一页上将行和段落放在一起。这可以通过Word 看到https://support.office.com/en-us/article/Keep-text-together-af94e5b8-3a5a-4cb0-9c53-dea56b43d96d

    所以我们必须为每个段落设置属性KeepLinesKeepNext。也适用于表内的人。

    凹痕线是我的补充。

    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);
    }
    
     }
    

    //主要方法

    ...
            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"));
    ...
    

    STOnOff.ON 需要org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff。但是您已经使用了其他 org.openxmlformats.schemas.wordprocessingml.x2006.main. 对象。这样你就知道如何获得了。

    编辑

    忘了说。请不要将XWPFDocument 保存为*.doc 文件。 *.doc文件主要用于Word到2003版本的二进制文件格式。XWPFDocument是基于XML的,应保存为*.docx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多