Table of Contents first implementation

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@819469 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paolo Mottadelli 2009-09-28 07:42:50 +00:00
parent ecb5344937
commit 99f5c38440
5 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,106 @@
package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtEndPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabs;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTheme;
public class TOC {
CTSdtBlock block;
public TOC() {
this(CTSdtBlock.Factory.newInstance());
}
public TOC(CTSdtBlock block) {
this.block = block;
CTSdtPr sdtPr = block.addNewSdtPr();
CTDecimalNumber id = sdtPr.addNewId();
id.setVal(new BigInteger("4844945"));
sdtPr.addNewDocPartObj().addNewDocPartGallery().setVal("Table of contents");
CTSdtEndPr sdtEndPr = block.addNewSdtEndPr();
CTRPr rPr = sdtEndPr.addNewRPr();
CTFonts fonts = rPr.addNewRFonts();
fonts.setAsciiTheme(STTheme.MINOR_H_ANSI);
fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI);
fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI);
fonts.setCstheme(STTheme.MINOR_BIDI);
rPr.addNewB().setVal(STOnOff.OFF);
rPr.addNewBCs().setVal(STOnOff.OFF);
rPr.addNewColor().setVal("auto");
rPr.addNewSz().setVal(new BigInteger("24"));
rPr.addNewSzCs().setVal(new BigInteger("24"));
CTSdtContentBlock content = block.addNewSdtContent();
CTP p = content.addNewP();
p.setRsidR("00EF7E24".getBytes());
p.setRsidRDefault("00EF7E24".getBytes());
p.addNewPPr().addNewPStyle().setVal("TOCHeading");
p.addNewR().addNewT().set("Table of Contents");
}
public CTSdtBlock getBlock() {
return this.block;
}
public void addRow(int level, String title, int page, String bookmarkRef) {
CTSdtContentBlock contentBlock = this.block.getSdtContent();
CTP p = contentBlock.addNewP();
p.setRsidR("00EF7E24".getBytes());
p.setRsidRDefault("00EF7E24".getBytes());
CTPPr pPr = p.addNewPPr();
pPr.addNewPStyle().setVal("TOC" + level);
CTTabs tabs = pPr.addNewTabs();
CTTabStop tab = tabs.addNewTab();
tab.setVal(STTabJc.RIGHT);
tab.setLeader(STTabTlc.DOT);
tab.setPos(new BigInteger("8290"));
pPr.addNewRPr().addNewNoProof();
CTR run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewT().set(title);
run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewTab();
run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewFldChar().setFldCharType(STFldCharType.BEGIN);
// pageref run
run = p.addNewR();
run.addNewRPr().addNewNoProof();
CTText text = run.addNewInstrText();
text.setSpace(Space.PRESERVE);
// bookmark reference
text.set(" PAGEREF _Toc" + bookmarkRef + " \\h ");
p.addNewR().addNewRPr().addNewNoProof();
run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
// page number run
run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewT().set(new Integer(page).toString());
run = p.addNewR();
run.addNewRPr().addNewNoProof();
run.addNewFldChar().setFldCharType(STFldCharType.END);
}
}

View File

@ -371,5 +371,24 @@ public class XWPFDocument extends POIXMLDocument {
public XWPFTable createTable(int rows, int cols) {
return new XWPFTable(this, ctDocument.getBody().addNewTbl(), rows, cols);
}
public void createTOC() {
CTSdtBlock block = this.getDocument().getBody().addNewSdt();
TOC toc = new TOC(block);
int i = 1;
for (Iterator<XWPFParagraph> iterator = getParagraphsIterator() ; iterator.hasNext() ; ) {
XWPFParagraph par = iterator.next();
String parStyle = par.getStyle();
if (parStyle != null && parStyle.substring(0, 7).equals("Heading")) {
try {
int level = new Integer(parStyle.substring("Heading".length()));
toc.addRow(level, par.getText(), 1, "112723803");
}
catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -938,6 +938,27 @@ public class XWPFParagraph {
return false;
}
/**
* This method provides a style to the paragraph
* This is useful when, e.g. an Heading style has to be assigned
* @param newStyle
*/
public void setStyle(String newStyle) {
CTPPr pr = getCTPPr();
CTString style = pr.getPStyle() != null ? pr.getPStyle() : pr.addNewPStyle();
style.setVal(newStyle);
}
/**
* Gets the style of the paragraph
* @return
*/
public String getStyle() {
CTPPr pr = getCTPPr();
CTString style = pr.isSetPStyle() ? pr.getPStyle() : null;
return style != null ? style.getVal() : null;
}
/**
* Get a <b>copy</b> of the currently used CTPBrd, if none is used, return
* a new instance.

View File

@ -0,0 +1,44 @@
package org.apache.poi.xwpf.usermodel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
public class TestXWPFHeadings extends TestCase{
private static final String HEADING1 = "Heading1";
public void testSetParagraphStyle() throws IOException, XmlException, InvalidFormatException {
//new clean instance of paragraph
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx");
XWPFParagraph p = doc.createParagraph();
XWPFRun run = p.createRun();
run.setText("Heading 1");
CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
assertNull(p.getStyle());
p.setStyle(HEADING1);
assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
doc.createTOC();
// CTStyles styles = doc.getStyle();
// CTStyle style = styles.addNewStyle();
// style.setType(STStyleType.PARAGRAPH);
// style.setStyleId("Heading1");
File file = new File("/Users/paolomoz/Desktop/testHeaders.docx");
OutputStream out = new FileOutputStream(file);
doc.write(out);
out.close();
}
}

Binary file not shown.