java - Apache POI 3.13 Offline/Offset elements for a .doc -
so have requirement validate generated .doc when content/ element paragraph or table pass page, , if element/content it's alone on other page need take element/content , put 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 pic of final result, fine pic:
but problem if happen:
this example happen validation
i try in base of number page handle problem, seems poi not store page num.
i need table , signature paragraph come other page if of 2 it's on new page.
i grateful, many thanks! regards.
seems if want keep lines , paragraphs on 1 page. possible word
see https://support.office.com/en-us/article/keep-lines-and-paragraphs-together-d72af534-926f-4c4b-830a-abfc2daa3bfa. article word mac 2011
same in word
.
so must set properties keeplines
, keepnext
each paragraph. within table.
the out-dented lines 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"); (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"); (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 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
needed stonoff.on
. using other org.openxmlformats.schemas.wordprocessingml.x2006.main.
objects already. know how this.
edit
forgot mention. please not save xwpfdocument
*.doc
file. *.doc
file used binary file format of word
version 2003. xwpfdocument
xml
based , should saved *.docx
.
Comments
Post a Comment