java - Apache POI: How to set font style for a field (PAGE, PAGENUM, PAGEREF...) -
from (answered) question learned how add page counter word document. in addition need set font family style (color, bold, italic, underline...) on field (i.e. page counter). how can achieved?
ctsimplefield ctsimplefield = paragraph.getctp().addnewfldsimple(); ctsimplefield not provide methods directly set these attributes.
original question: how add page numbers in format x of y while creating word document using apache poi api?
import java.io.*; import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.model.xwpfheaderfooterpolicy; public class createwordheaderfooter { public static void main(string[] args) throws exception { xwpfdocument doc= new xwpfdocument(); // body content xwpfparagraph paragraph = doc.createparagraph(); xwpfrun run=paragraph.createrun(); run.settext("the body:"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.settext("lorem ipsum.... page 1"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.addbreak(breaktype.page); run.settext("lorem ipsum.... page 2"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.addbreak(breaktype.page); run.settext("lorem ipsum.... page 3"); // create header-footer xwpfheaderfooterpolicy headerfooterpolicy = doc.getheaderfooterpolicy(); if (headerfooterpolicy == null) headerfooterpolicy = doc.createheaderfooterpolicy(); // create header start xwpfheader header = headerfooterpolicy.createheader(xwpfheaderfooterpolicy.default); paragraph = header.getparagrapharray(0); if (paragraph == null) paragraph = header.createparagraph(); paragraph.setalignment(paragraphalignment.left); run = paragraph.createrun(); run.settext("the header:"); // create footer start xwpffooter footer = headerfooterpolicy.createfooter(xwpfheaderfooterpolicy.default); paragraph = footer.getparagrapharray(0); if (paragraph == null) paragraph = footer.createparagraph(); paragraph.setalignment(paragraphalignment.center); run = paragraph.createrun(); run.settext("page "); // adds page counter paragraph.getctp().addnewfldsimple().setinstr("page \\* mergeformat"); run = paragraph.createrun(); run.settext(" of "); // adds page total number paragraph.getctp().addnewfldsimple().setinstr("numpages \\* mergeformat"); doc.write(new fileoutputstream("createwordheaderfooter.docx")); } }
to able formatting, need runs in word. create fields using runs need 3 runs each field. 1 run mark fldchar start, 1 run mark fields instrtext , third run mark fldchar end. , each run can, must, formatted needed.
example:
import java.io.*; import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.model.xwpfheaderfooterpolicy; public class createwordheaderfooter { public static void main(string[] args) throws exception { xwpfdocument doc= new xwpfdocument(); // body content xwpfparagraph paragraph = doc.createparagraph(); xwpfrun run=paragraph.createrun(); run.settext("the body:"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.settext("lorem ipsum.... page 1"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.addbreak(breaktype.page); run.settext("lorem ipsum.... page 2"); paragraph = doc.createparagraph(); run=paragraph.createrun(); run.addbreak(breaktype.page); run.settext("lorem ipsum.... page 3"); // create header-footer xwpfheaderfooterpolicy headerfooterpolicy = doc.getheaderfooterpolicy(); if (headerfooterpolicy == null) headerfooterpolicy = doc.createheaderfooterpolicy(); // create header start xwpfheader header = headerfooterpolicy.createheader(xwpfheaderfooterpolicy.default); paragraph = header.getparagrapharray(0); if (paragraph == null) paragraph = header.createparagraph(); paragraph.setalignment(paragraphalignment.left); run = paragraph.createrun(); run.settext("the header:"); // create footer start xwpffooter footer = headerfooterpolicy.createfooter(xwpfheaderfooterpolicy.default); paragraph = footer.getparagrapharray(0); if (paragraph == null) paragraph = footer.createparagraph(); paragraph.setalignment(paragraphalignment.center); run = paragraph.createrun(); run.setbold(true); run.settext("page "); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewfldchar().setfldchartype(org.openxmlformats.schemas.wordprocessingml.x2006.main.stfldchartype.begin); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewinstrtext().setstringvalue("page \\* mergeformat"); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewfldchar().setfldchartype(org.openxmlformats.schemas.wordprocessingml.x2006.main.stfldchartype.end); run = paragraph.createrun(); run.setbold(true); run.settext(" of "); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewfldchar().setfldchartype(org.openxmlformats.schemas.wordprocessingml.x2006.main.stfldchartype.begin); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewinstrtext().setstringvalue("numpages \\* mergeformat"); run = paragraph.createrun(); run.setbold(true); run.getctr().addnewfldchar().setfldchartype(org.openxmlformats.schemas.wordprocessingml.x2006.main.stfldchartype.end); doc.write(new fileoutputstream("createwordheaderfooter.docx")); } }
Comments
Post a Comment