问题描述
所以基本上我需要在某些内容/元素段落或表格传递到另一个页面时验证生成的 .doc,如果某些元素/内容单独存在于另一个页面上,我需要获取另一个元素/内容并放入它与单独的元素/内容
public voidinvestigarDoc(XWPFDocument doc){尝试 {creacionDeFooter(doc);//页脚创建方法XWPFParagraphcuerpoObservaciones = doc.createParagraph();//第1段cuerpoObservaciones.setAlignment(ParagraphAlignment.DISTRIBUTE);XWPFRun imprimeObservaciones =cuerpoObservaciones.createRun();seccionObservaciones(doc,imprimeObservaciones,cuerpoObservaciones);//表创建方法XWPFParagraphcuerpoFirma = doc.createParagraph();//第2段cuerpoFirma.setAlignment(ParagraphAlignment.CENTER);XWPFRun imprimeFirma =cuerpoFirma.createRun();seccionFirma(doc,imprimeFirma,cuerpoFirma);//签名创建方法doc.write(new FileOutputStream("C:\\test.doc"));} catch (IOException iox) {iox.printStackTrace();System.out.println("错误:IOException Verificar Rutas de Archivos o Fotos!");}}
//页脚方法
public void creacionDeFooter(XWPFDocument doc){//FOOTER METHOD尝试 {CTP ctp = CTP.Factory.newInstance();//这个添加页码增量ctp.addNewR().addNewPgNum();XWPFParagraph parrafoFotter = 新 XWPFParagraph(ctp, doc);XWPFParagraph[] 段落 = 新 XWPFParagraph[1];段落[0] = parrafoFotter;//数字的位置parrafoFotter.setAlignment(ParagraphAlignment.RIGHT);CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, 段落);} catch (IOException e) {e.printStackTrace();}}
//第一款的方法
public void seccionObservaciones(XWPFDocument doc, XWPFRun otrasObservaciones,XWPFParagraph observaciones){//TABLE METHODotrasObservaciones = observaciones.createRun();otrasObservaciones.setText(".");otrasObservaciones.addBreak();//创建表XWPFTable 表 = doc.createTable();//创建第一行XWPFTableRow tableRowOne = table.getRow(0);tableRowOne.getCell(0).setText("Otras Operaciones/Observaciones");//创建第二行XWPFTableRow tableRowTwo = table.createRow();tableRowTwo.getCell(0).getTableRow();tableRowTwo.getCell(0).setText("colfore, rowfore");}
//第2段的方法
public void seccionFirma(XWPFDocument doc, XWPFRun imprimeFirma,XWPFParagraph Firma){//SIGNATURE METHODimprimeFirma = 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不存储页数.
如果表格和签名段落中的一些在新页面上,我需要将表格和签名段落移到另一页.
我将非常感激,非常感谢!问候.
似乎您希望将行和段落放在一个页面上.这可以通过 Word
看到 https://support.office.com/en-us/article/Keep-text-together-af94e5b8-3a5a-4cb0-9c53-dea56b43d96d.
所以我们必须为每个段落设置属性KeepLines
和KeepNext
.也适用于表内的人.
凹陷的线条是我的补充.
public void seccionObservaciones(XWPFDocument doc, XWPFRun otrasObservaciones, XWPFParagraph observaciones){//TABLE METHODotrasObservaciones = observaciones.createRun();otrasObservaciones.setText(".");otrasObservaciones.addBreak();//创建表XWPFTable 表 = doc.createTable();//创建第一行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);}//创建第二行XWPFTableRow tableRowTwo = table.createRow();tableRowTwo.getCell(0).getTableRow();tableRowTwo.getCell(0).setText("colfore, rowfore");对于 (XWPFParagraph p : tableRowTwo.getCell(0).getParagraphs()) {p.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON);p.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);}}
//主要方法
...creacionDeFooter(doc);//页脚创建方法XWPFParagraphcuerpoObservaciones = doc.createParagraph();//第1段cuerpoObservaciones.setAlignment(ParagraphAlignment.DISTRIBUTE);cuerpoObservaciones.getCTP().getPPr().addNewKeepLines().setVal(STOnOff.ON);//已经通过setAlignment有一个CPPrcuerpoObservaciones.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON);XWPFRun imprimeObservaciones =cuerpoObservaciones.createRun();seccionObservaciones(doc,imprimeObservaciones,cuerpoObservaciones);//表创建方法XWPFParagraphcuerpoFirma = doc.createParagraph();//第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);//签名创建方法doc.write(new FileOutputStream("test.docx"));...org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff
需要 STOnOff.ON
.但是您已经在使用其他 org.openxmlformats.schemas.wordprocessingml.x2006.main.
对象.所以你会知道如何获得这个.
编辑
忘记说了.请不要将 XWPFDocument
保存为 *.doc
文件.*.doc
文件主要用于Word
到2003版本的二进制文件格式.XWPFDocument
是XML
> 基于并应保存为 *.docx
.
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.
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
.
这篇关于.doc 的 Apache POI 3.13 离线/偏移元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!