From: Paolo Mottadelli Date: Mon, 28 Sep 2009 07:42:50 +0000 (+0000) Subject: Table of Contents first implementation X-Git-Tag: REL_3_6~120 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5eeaeff15383573bf258d8b3050db41b86fd8b13;p=poi.git Table of Contents first implementation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@819469 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java new file mode 100644 index 0000000000..123fa1165e --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java @@ -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); + + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 9081909b3f..7a43ff46d4 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -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 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(); + } + } + } + } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 58d10db401..85999d51a0 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -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 copy of the currently used CTPBrd, if none is used, return * a new instance. diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java new file mode 100644 index 0000000000..455dc6887c --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java @@ -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(); + } + +} diff --git a/test-data/document/heading123.docx b/test-data/document/heading123.docx new file mode 100644 index 0000000000..cf61183399 Binary files /dev/null and b/test-data/document/heading123.docx differ