blob: 8602d9b92f758d7030e0be5b1cfb2496959ba429 [file] [log] [blame]
<strong><a href="../index.html">Documents</a></strong> > <strong><a href="index.html">Cookbook</a></strong> ><strong><a href="Text Document.html">Text Document</a></strong>
<div class="navigation">
<ul>
<li><a href="Manipulate Metadata.html">previous</a></li>
<li><a href="Presentation Document.html">next</a></li>
</ul>
</div>
<br/>
<br/><strong><a href="#Overview" >Overview</a></strong>
<div class="bodytext">
Till now, we support high level APIs to manipulate text document, paragraph, section, list, footer, header, textbox and span.
</div>
<br/><strong><a href="#Generate TextDocument" >Generate TextDocument</a></strong>
<div class="bodytext">
There are typically four kinds of Text Documents with different modes can be generated: text document, master document, template document and web document.
</div>
<div class="bodytext">
The following codes generates an empty text document.
</div>
<br/><pre class='code' id="code0">
<span class='javaclass'>TextDocument</span> document1=<span class='javaclass'>TextDocument</span>.newTextDocument(); <br/>
document1.save(filePath);<br/>
</pre>
<br/><div class="bodytext">
There are two methods to generate a new document, one with a parameter of OdfMediaType, and the other one just use the corresponding new function.
</div>
<div class="bodytext">
The following two lines of codes generate the same kinds of document(Master document). The other three kinds of document are similar.
</div>
<br/><pre class='code' id="code1">
<span class='javaclass'>TextDocument</span> documentMedia1=<span class='javaclass'>TextDocument</span>.newTextDocument(<span class='javaclass'>OdfMediaType</span>.TEXT_MASTER);<br/>
<span class='javaclass'>TextDocument</span> documentMedia2=<span class='javaclass'>TextDocument</span>.newTextMasterDocument();<br/>
</pre>
<br/><br/><strong><a href="#Get TextDocument" >Get TextDocument</a></strong>
<div class="bodytext">
We can get the existing Text Document by using the loadDocument function like follows. The loadDocument function has four kinds of parameters: File, InputStream, OdfPackageDocument and String. Here the one with string parameter is used as an example.
</div>
<br/><pre class='code' id="code2">
<span class='javaclass'>TextDocument</span> document2=(<span class='javaclass'>TextDocument</span>)<span class='javaclass'>TextDocument</span>.loadDocument("textdocument.odt");<br/>
</pre>
<br/><div class="bodytext">
Also we can change the mode of one text document by using the following codes:
</div>
<br/><pre class='code' id="code3">
document2.changeMode(<span class='javaclass'>OdfMediaType</span>.TEXT_WEB);<br/>
</pre>
<br/><br/><strong><a href="#Insert TextDocument" >Insert TextDocument</a></strong>
<div class="bodytext">
From version 0.8, you can copy contents from a source TextDocument and then insert them to the current TextDocument. The insert position is relative to a reference Paragraph, before or after. You can also choose to copy the styles with the contents or not.
</div>
<br/><pre class='code' id="code54">
<span class='javaclass'>TextDocument</span> src = <span class='javaclass'>TextDocument</span>.loadDocument("SourceDocument.odt");
<span class='javaclass'>TextDocument</span> target = <span class='javaclass'>TextDocument</span>.loadDocument("TargetDocument.odt");
<span class='javaclass'>Paragraph</span> p1 = target.getParagraphByIndex(0, <span class='basic'>true</span>);
<span class='comments'> // insert contents before p1 and copy styles</span>
target.insertContentFromDocumentBefore(src, p1, <span class='basic'>true</span>);
<span class='comments'> // insert contents after p1 and don't copy styles</span>
target.insertContentFromDocumentAfter(src, p1, <span class='basic'>false</span>);
</pre>
<br/><br/><strong><a href="#Paragraph" >Paragraph</a></strong>
<div class="bodytext">
To add a new paragraph to a text document, the following codes can be used, the first one add an empty paragraph to the end of the document, and the second one add a new paragraph with the corresponding texts to the document.
</div>
<br/><pre class='code' id="code4">
document1.addParagraph(<span class='basic'>null</span>);<br/>
document1.addParagraph("test newParagraph function in textDocument");<br/>
</pre>
<br/><div class="bodytext">
The first one will get the second paragraph of this document. The second statement will get the last paragraph with text content. The paragraph without text content will be ignored if the second parameter is true.
</div>
<div class="bodytext">
The following codes can be used to append text to the end of a paragrah.
</div>
<br/><pre class='code' id="code5">
para1.appendTextContent("test addText function in textDocument");<br/>
</pre>
<br/><div class="bodytext">
The following codes are to get and set the horizontal alignment of a paragraph.
</div>
<br/><pre class='code' id="code6">
<span class='javaclass'>HorizontalAlignmentType</span> align = para1.getHorizontalAlignment();<br/>
para1.setHorizontalAlignment(<span class='javaclass'>HorizontalAlignmentType</span>.<span class='javaclass'>CENTER</span>);<br/>
</pre>
<br/><div class="bodytext">
The following codes are to get and set the font of a paragraph.
</div>
<br/><pre class='code' id="code7">
<span class='javaclass'>Font</span> font = para1.getFont();<br/>
font.setFontStyle(<span class='javaclass'>StyleTypeDefinitions</span>.<span class='javaclass'>FontStyle</span>.<span class='javaclass'>ITALIC</span>);<br/>
para2.setFont(font);<br/>
</pre>
<br/><div class="bodytext">
The following code is to apply a hyperlink to this paragraph, and append a text with a hyperlink to this paragraph.
</div>
<br/><pre class='code' id="code8">
para2.applyHyperlink(<span class='modifier'>new</span> <span class='javaclass'>URI</span>("mailto:daisy@odftoolkit.org"));<br/>
para2.appendHyperlink("mail to me",new <span class='javaclass'>URI</span>("mailto:daisy@odftoolkit.org"));<br/>
</pre>
<br/><div class="bodytext">
The following code is to add a comment at the front of the paragraph.
</div>
<br/><pre class='code' id="code9">
para2.addComment("<span class='javaclass'>This</span> is a comment for para2", "<span class='javaclass'>Simple</span> <span class='javaclass'>ODF</span> <span class='javaclass'>Commenter</span>");<br/>
</pre>
<br/><div class="bodytext">
From version 0.6.5, we support heading feature. You can test whether a paragraph is heading, apply a plain text paragraph as heading, and get heading level.
</div>
<br/><pre class='code' id="code10">
<span class='comments'> // isHeading() and getHeadingLevel();</span>
<span class='control'>if</span> (para2.isHeading()) {<br/>
<span class='basic'>int</span> headingLevel = para2.getHeadingLevel();<br/>
<span class='javaclass'>System</span>.out.println("para2 is a heading, its level is " + headingLevel + ".");<br/>
}<br/>
<span class='comments'> // applyHeading(), default level is 1.</span>
para1.applyHeading();<br/>
<span class='comments'> // applyHeading(), heading level is 3.</span>
para1.applyHeading(<span class='basic'>true</span>, 3);<br/>
</pre>
<br/><br/><strong><a href="#Section" >Section</a></strong>
<div class="bodytext">
From version 0.4, we support high level APIs for section. You can use the following code to get a section by name:
</div>
<br/><pre class='code' id="code11">
<span class='javaclass'>Section</span> aSection = document1.getSectionByName("<span class='javaclass'>ImageSection</span>");<br/>
</pre>
<br/><div class="bodytext">
or use the following code to get an iterator of sections. All sections in the document would be returned, including sections in footer and header.
</div>
<br/><pre class='code' id="code12">
<span class='javaclass'>Iterator</span>&lt;<span class='javaclass'>Section</span>&gt; sections = document1.getSectionIterator();<br/>
<span class='control'>while</span> (sections.hasNext()) {<br/>
<span class='javaclass'>Section</span> theSection = sections.next();<br/>
}<br/>
</pre>
<br/><div class="bodytext">
After you get the object of section, you can use the following codes to set and get the name of the section.
</div>
<br/><pre class='code' id="code13">
<span class='javaclass'>String</span> name = aSection.getName();<br/>
aSection.setName("<span class='javaclass'>NewName</span>");<br/>
</pre>
<br/><div class="bodytext">
You can use the following code to copy and append a section at the end of a document. Copying from a foreign document is supported. You don't need to care whether or not the document is the owner of the source section.
</div>
<br/><pre class='code' id="code14">
document1.appendSection(aSection, <span class='basic'>true</span>);<br/>
</pre>
<br/><div class="bodytext">
If you want to copy and append a section within a same document, you can specify with the second parameter of "copyAppendSection(Section section, boolean isResourceCopied)" whether the linked resources to this section are copied or not. The following code means to copy a section and append it at the end, but the linked resources are shared between the source section and the copied section.
</div>
<br/><pre class='code' id="code15">
document1.appendSection(aSection, <span class='basic'>false</span>);<br/>
</pre>
<br/><div class="bodytext">
You can remove this Section from the document content, all the resources that are only linked with this section would be removed too.
</div>
<br/><pre class='code' id="code16">
aSection.remove();<br/>
</pre>
<br/><div class="bodytext">
From version 0.8, we support high level APIs to protect a section. You can use the following code to set the protection without a password:
</div>
<br/><pre class='code' id="code17">
aSection.setProtected(<span class='basic'>true</span>);<br/>
</pre>
<br/><div class="bodytext">
If you want to protect a section with a password, you can use the following code.
</div>
<br/><pre class='code' id="code54">
aSection.setProtectedWithPassword("<span class='javaclass'>myPassword</span>");<br/>
</pre>
<br/><br/><strong><a href="#List" >List</a></strong>
<div class="bodytext">
From version 0.4, we support high level APIs for list. You can use the following code to create a list. The two methods are same.
</div>
<br/><pre class='code' id="code55">
<span class='javaclass'>List</span> newList1 = document1.addList();<br/>
<span class='javaclass'>List</span> newList2 = <span class='modifier'>new</span> <span class='javaclass'>List</span>(document1);<br/>
</pre>
<br/><div class="bodytext">
Actually, you can append list to table cell, list item, presentation slide and notes. They all have the same ability with text document for list. We call them ListContainer, which can append, remove and get the iterator of list.
</div>
<br/><pre class='code' id="code18">
<span class='javaclass'>Iterator</span>&lt;<span class='javaclass'>List</span>&gt; lists = document1.getListIterator();<br/>
<span class='javaclass'>List</span> list =<span class='basic'>null</span>;<br/>
<span class='control'>while</span> (lists.hasNext()) {<br/>
list = lists.next();<br/>
}<br/>
</pre>
<br/><div class="bodytext">
After you get the object of list, you can use the following methods to set and get the header of the list.
</div>
<br/><pre class='code' id="code19">
<span class='javaclass'>String</span> header = list.getHeader();<br/>
list.setHeader("<span class='javaclass'>NewHeader</span>");<br/>
</pre>
<br/><div class="bodytext">
You can get all of the existing items in this list. They are returned as java.util.List.
</div>
<br/><pre class='code' id="code20">
java.util.<span class='javaclass'>List</span>&lt;<span class='javaclass'>ListItem</span>&gt; items = list.getItems();<br/>
</pre>
<br/><div class="bodytext">
If you only want to get the item at a specific location, you can use the following code:
</div>
<br/><pre class='code' id="code21">
<span class='basic'>int</span> location = 2;<br/>
<span class='javaclass'>ListItem</span> item = list.getItem(location);<br/>
</pre>
<br/><div class="bodytext">
There are several ways to help you append item(s) to the list. Please reference the following code:
</div>
<br/><pre class='code' id="code22">
<span class='comments'> // add single item</span>
<span class='javaclass'>ListItem</span> newItem1 = list.addItem(item);<br/>
<span class='javaclass'>ListItem</span> newItem2 = list.addItem("itemContent");<br/>
<span class='comments'> // add clone items</span>
<span class='javaclass'>ListItem</span>[] newItems = <span class='modifier'>new</span> <span class='javaclass'>ListItem</span>[]{newItem1, newItem2};<br/>
list.addItems(newItems);<br/>
<span class='comments'> // add string items</span>
<span class='javaclass'>String</span>[] newItemContents = <span class='modifier'>new</span> <span class='javaclass'>String</span>[]{"itemContent1", "itemContent2"};<br/>
list.addItems(newItemContents);<br/>
</pre>
<br/><div class="bodytext">
You can specify a location to insert new item(s).
</div>
<br/><pre class='code' id="code23">
newItem1 = list.addItem(location, item);<br/>
newItem2 = list.addItem(location, "itemContent");<br/>
list.addItems(location, newItems);<br/>
list.addItems(location, newItemContents);<br/>
</pre>
<br/><div class="bodytext">
If you want to replace a list item with a new one, you can use the following code.
</div>
<br/><pre class='code' id="code24">
item = list.set(location, newItem1);<br/>
item = list.set(location, "itemContent");<br/>
</pre>
<br/><div class="bodytext">
You can use the following methods to remove item(s).
</div>
<br/><pre class='code' id="code25">
list.removeItem(item);<br/>
<span class='comments'> //item in specific location.</span>
list.removeItem(location);<br/>
<span class='comments'> //item in specific collection.</span>
list.removeItems(items);<br/>
</pre>
<br/><div class="bodytext">
You can specify with the following methods whether the numbering of the previous list is continued by this list:
</div>
<br/><pre class='code' id="code26">
<span class='comments'> // the numbering of the proximate list is continued</span>
newList1.setContinueNumbering(<span class='basic'>true</span>);<br/>
<span class='comments'> // the numbering of the specified list is continued by this list</span>
newList2.setContinueList(list);<br/>
</pre>
<br/><div class="bodytext">
You can use the following method to know whether the list is a number list.
</div>
<br/><pre class='code' id="code27">
<span class='javaclass'>ListType</span> type = list.getType();<br/>
</pre>
<br/><div class="bodytext">
Now, List API support 3 types of lists, ListType.BULLET, ListType.NUMBER and ListType.IMAGE. The default created list is ListType.BULLET, bullet list. How to create a number list or a image list then? ListDecorator has powerful functions that can help you. ListDecorator is an interface which decides how to decorate a list and its list items. We supply 4 implementations of this interface, BulletDecorator, NumberDecorator, ImageDecorator and OutlineDecorator.
</div>
<br/><pre class='code' id="code28">
<span class='comments'> // create a number list.</span>
<span class='javaclass'>ListDecorator</span> numberDecorator = <span class='modifier'>new</span> <span class='javaclass'>NumberDecorator</span>(document1);<br/>
<span class='javaclass'>List</span> numberList1 = document1.addList(numberDecorator);<br/>
<span class='javaclass'>List</span> numberList2 = <span class='modifier'>new</span> <span class='javaclass'>List</span>(document1, numberDecorator);<br/>
</pre>
<br/><div class="bodytext">
You can implement your own ListDecorator as need, or extend the default four implementations.
</div>
<div class="bodytext">
You can remove a list from the document, the following two methods are same.
</div>
<br/><pre class='code' id="code29">
list.remove();<br/>
document1.removeList(list);<br/>
</pre>
<br/><div class="bodytext">
We also provide funtions to manipulate list items. You can use the following methods to get and set the text content of a item.
</div>
<br/><pre class='code' id="code30">
<span class='javaclass'>String</span> itemContent = item.getTextContent();<br/>
item.setTextContent("new item content");<br/>
</pre>
<br/><div class="bodytext">
You can get the item index and its owner list.
</div>
<br/><pre class='code' id="code31">
<span class='basic'>int</span> index = item.getIndex();<br/>
<span class='javaclass'>List</span> ownerList = item.getOwnerList();<br/>
</pre>
<br/><div class="bodytext">
When you want to remove an item, besides List.removeItem(ListItem), you can use the following method directly.
</div>
<br/><pre class='code' id="code32">
item.remove();<br/>
</pre>
<br/><div class="bodytext">
With the help of the following code, the start number of list item can be set.
</div>
<br/><pre class='code' id="code33">
item.setStartNumber(3);<br/>
</pre>
<br/><div class="bodytext">
Since list item is also a ListContainer, you can add sub list to an item.
</div>
<br/><pre class='code' id="code34">
item.addList();<br/>
</pre>
<br/><br/><strong><a href="#Header and Footer" >Header and Footer</a></strong>
<div class="bodytext">
From version 0.4.5, we support high level APIs for footer and header. You can use the following code to get the header and footer.
</div>
<br/><pre class='code' id="code35">
<span class='javaclass'>Header</span> docHeader = doc.getHeader();<br/>
<span class='javaclass'>Footer</span> footer = doc.getFooter();<br/>
</pre>
<br/><div class="bodytext">
In order to add contents to header and arrange these contents in a good layout, I suggest you to use table. It's easy to add a none-border table to header. Below codes show how to add a table with 2 columns and 1 row to header, and then add some text content to the left cell, and add an image to the right cell.
</div>
<br/><pre class='code' id="code36">
<span class='javaclass'>Table</span> table = docHeader.addTable(1,2);<br/>
table.getCellByPosition(0, 0).setStringValue("header table cell");<br/>
<span class='javaclass'>Cell</span> cell = table.getCellByPosition(1, 0);<br/>
<span class='javaclass'>Image</span> image1 = cell.setImage(<span class='modifier'>new</span> <span class='javaclass'>URI</span>("file:/c:/image.jpg"));<br/>
</pre>
<br/><div class="bodytext">
Below codes show how to add a table with 1 column and 1 row to footer, and then add some text content to the cell. You can easily set the style of these text content. The codes describe how to put the text content in the center, set the font of the text content, and set the background of the cell.
</div>
<br/><pre class='code' id="code37">
table = footer.addTable(1, 1);<br/>
<span class='javaclass'>Cell</span> cellByPosition = table.getCellByPosition(0, 0);<br/>
cellByPosition.setStringValue("footer table cell");<br/>
cellByPosition.setHorizontalAlignment(<span class='javaclass'>HorizontalAlignmentType</span>.<span class='javaclass'>CENTER</span>);<br/>
<span class='javaclass'>Font</span> myFont = <span class='modifier'>new</span> <span class='javaclass'>Font</span>("<span class='javaclass'>Arial</span>", <span class='javaclass'>StyleTypeDefinitions</span>.<span class='javaclass'>FontStyle</span>.<span class='javaclass'>ITALIC</span>, 12, <span class='javaclass'>Color</span>.<span class='javaclass'>BLUE</span>);<br/>
cellByPosition.setFont(myFont);<br/>
cellByPosition.setCellBackgroundColor(<span class='javaclass'>Color</span>.<span class='javaclass'>YELLOW</span>);<br/>
</pre>
<br/><br/><strong><a href="#Text Box" >Text Box</a></strong>
<div class="bodytext">
From version 0.5, we support high level APIs for text box. Below codes will create a text box and add it to the text document with a paragraph as the anchor position. The FrameRectangle specifies an area and the position of this text box.
</div>
<br/><pre class='code' id="code38">
<span class='javaclass'>Paragraph</span> paragraph = doc.getParagraphByIndex(0, <span class='basic'>false</span>);<br/>
<span class='javaclass'>Textbox</span> box = paragraph.addTextbox(<span class='modifier'>new</span> <span class='javaclass'>FrameRectangle</span>(1,1,2,1,<span class='javaclass'>SupportedLinearMeasure</span>.<span class='javaclass'>IN</span>));<br/>
</pre>
<br/><div class="bodytext">
Below codes is to set the text content and name of this text box;
</div>
<br/><pre class='code' id="code39">
box.setTextContent("this is a text box");<br/>
box.setName("<span class='javaclass'>MyTextbox</span>");<br/>
</pre>
<br/><div class="bodytext">
Below codes is to get a text box by name or get an iterator of text box contained by a paragraph.
</div>
<br/><pre class='code' id="code40">
<span class='javaclass'>Textbox</span> myBox=paragraph.getTextboxByName("<span class='javaclass'>MyTextbox</span>");<br/>
<span class='javaclass'>Iterator</span>&lt;<span class='javaclass'>Textbox</span>&gt; boxIter = paragraph.getTextboxIterator();<br/>
</pre>
<br/><div class="bodytext">
Below code is to set the background color of a text box.
</div>
<br/><pre class='code' id="code41">
myBox.setBackgroundColor(<span class='javaclass'>Color</span>.<span class='javaclass'>BLUE</span>);<br/>
</pre>
<br/><div class="bodytext">
Below code is to remove a text box;
</div>
<br/><pre class='code' id="code42">
paragraph.removeTextbox(myBox);<br/>
</pre>
<br/><br/><strong><a href="#Image" >Image</a></strong>
<div class="bodytext">
From version 0.5.5, we support high level APIs for images. Below codes will create a image and add it to a document with a paragraph as the anchor position.
</div>
<br/><pre class='code' id="code43">
<span class='javaclass'>Paragraph</span> para = doc.getParagraphByIndex(1, <span class='basic'>false</span>);<br/>
<span class='javaclass'>Image</span> image = <span class='javaclass'>Image</span>.newImage(para, <span class='modifier'>new</span> <span class='javaclass'>URI</span>("file:/c:/image.jpg"));<br/>
</pre>
<br/><div class="bodytext">
Below codes can set the properties of image, and even the vertical (or horizontal) alignment.
</div>
<br/><pre class='code' id="code44">
image.setTitle("<span class='javaclass'>Image</span> title");<br/>
image.setDescription("<span class='javaclass'>This</span> is a sample image");<br/>
image.setVerticalPosition(<span class='javaclass'>FrameVerticalPosition</span>.<span class='javaclass'>TOP</span>);<br/>
image.setHorizontalPosition(<span class='javaclass'>FrameHorizontalPosition</span>.<span class='javaclass'>RIGHT</span>);<br/>
</pre>
<br/><div class="bodytext">
If you want to add a hyperlink to your image, you can use the following code:
</div>
<br/><pre class='code' id="code45">
image.setHyperlink(<span class='modifier'>new</span> <span class='javaclass'>URI</span>("http://odftoolkit.org"));<br/>
</pre>
<br/><div class="bodytext">
If you want to handle more style settings of image, you can try FrameStyleHandler.
</div>
<br/><pre class='code' id="code46">
<span class='javaclass'>FrameStyleHandler</span> handler = image.getStyleHandler();<br/>
handler.setAchorType(<span class='javaclass'>AnchorType</span>.AS_CHARACTER);<br/>
handler.setHorizontalRelative(<span class='javaclass'>HorizontalRelative</span>.<span class='javaclass'>PAGE</span>);<br/>
handler.setVerticalRelative(<span class='javaclass'>VerticalRelative</span>.<span class='javaclass'>PAGE</span>);<br/>
</pre>
<br/><br/><strong><a href="#Span" >Span</a></strong>
<div class="bodytext">
From version 0.5.5, we support high level APIs for span. You can create a span based on a text selection. With span, you can set a different style to this small unit.
</div>
<br/><pre class='code' id="code47">
<span class='javaclass'>TextNavigation</span> navigation = <span class='modifier'>new</span> <span class='javaclass'>TextNavigation</span>("test", doc);<br/>
<span class='javaclass'>TextSelection</span> sel = (<span class='javaclass'>TextSelection</span>) navigation.nextSelection();<br/>
<span class='javaclass'>Span</span> span = <span class='javaclass'>Span</span>.newSpan(sel);<br/>
<span class='javaclass'>DefaultStyleHandler</span> styleHandler = span.getStyleHandler();<br/>
<span class='javaclass'>Font</span> font1Base = <span class='modifier'>new</span> <span class='javaclass'>Font</span>("<span class='javaclass'>Arial</span>", <span class='javaclass'>FontStyle</span>.<span class='javaclass'>ITALIC</span>, 10, <span class='javaclass'>Color</span>.<span class='javaclass'>BLACK</span>, <span class='javaclass'>TextLinePosition</span>.<span class='javaclass'>THROUGH</span>);<br/>
styleHandler.getTextPropertiesForWrite().setFont(font1Base);<br/>
</pre>
<br/><div class="bodytext">
The following code is to apply a hyperlink to this span.
</div>
<br/><pre class='code' id="code48">
span.applyHyperlink(<span class='modifier'>new</span> <span class='javaclass'>URI</span>("ftp://ftpserver"));<br/>
</pre>
<br/><br/><strong><a href="#Page Break" >Page Break</a></strong>
<div class="bodytext">
From version 0.6.5, we support page break feature. You can add page break at the end of text document or after a reference paragraph. It may help you well format your document.
</div>
<br/><pre class='code' id="code49">
<span class='javaclass'>TextDocument</span> newDoc = <span class='javaclass'>TextDocument</span>.newTextDocument();<br/>
<span class='comments'> //add a page break at the end of document.</span>
newDoc.addPageBreak();<br/>
<span class='comments'> //add a page break at the end of reference paragraph.</span>
<span class='javaclass'>Paragraph</span> refParagraph = newDoc.addParagraph("before page break");<br/>
newDoc.addPageBreak(refParagraph);<br/>
</pre>
<div class="bodytext">
From version 0.8, we support to appoint the page style for the page that follows the added page break. You can get a page style by the name and use it as one parameter when adding a page break.
</div>
<br/><pre class='code' id="code50">
<span class='javaclass'>TextDocument</span> newDoc = <span class='javaclass'>TextDocument</span>.newTextDocument();<br/>
<span class='javaclass'>MasterPage</span> master = (<span class='javaclass'>MasterPage</span>) getOrCreateMasterPage(newDoc, "<span class='javaclass'>Envelope</span>");<br/>
<span class='javaclass'>Paragraph</span> refParagraph = newDoc.addParagraph("before page break");<br/>
newDoc.addPageBreak(refParagraph, master);<br/>
</pre>
<div class="bodytext">
You can also create a customized page style and use it with a page break.
</div>
<br/><pre class='code' id="code51">
<span class='javaclass'>TextDocument</span> newDoc = <span class='javaclass'>TextDocument</span>.newTextDocument();<br/>
<span class='javaclass'>MasterPage</span> master = (<span class='javaclass'>MasterPage</span>) getOrCreateMasterPage(newDoc, "<span class='javaclass'>Landscape</span>");<br/>
<span class='comments'> //create a customized page style with specified width, height, margins and other properties.</span>
maste.setPageWidth(279.4);<br/>
master.setPageHeight(215.9);<br/>
master.setNumberFormat(<span class='javaclass'>NumberFormat.HINDU_ARABIC_NUMBER</span>.toString());<br/>
master.setPrintOrientation(<span class='javaclass'>PrintOrientation.LANDSCAPE</span>);<br/>
master.setFootnoteMaxHeight(0);<br/>
master.setWritingMode(<span class='javaclass'>StyleTypeDefinitions.WritingMode.LRTB</span>);<br/>
master.setMargins(20, 20, 20, 20);<br/>
master.setFootnoteSepProperties(<span class='javaclass'>AdjustmentStyle.LEFT</span>, <span class='javaclass'>Color</span>.valueOf("#000000"), 1, 1, null, <span class='javaclass'>Percent</span>.valueOf("25%"), 0.18);<br/>
<span class='javaclass'>Paragraph</span> refParagraph = newDoc.addParagraph("before page break");<br/>
newDoc.addPageBreak(refParagraph, master);<br/>
</pre>
<br/><br/><strong><a href="#Table of Content" >Table of Content</a></strong>
<div class="bodytext">
From version 0.8, we support high level APIs to add table of content (TOC). You can add TOC before/after a reference paragraph by following code. The first parameter is the reference paragraph, the second parameter specifies where to insert the TOC, "true" means before the reference paragraph, "false" means after the reference paragraph.
</div>
<br/><pre class='code' id="code52">
<span class='javaclass'>TextTableOfContentElement</span> textTableOfContentElement = doc.createDefaultTOC(paragraph1,<span class='basic'>false</span>);
</pre>
<div class="bodytext">
You can also specify the additional paragraph styles applied on the created TOC. The following codes shows how to generate a TOC before a reference paragraph with customized styles.
</div>
<br/><pre class='code' id="code53">
<span class='javaclass'>TOCStyle</span> tocstyle = <span class='modifier'>new</span> <span class='javaclass'>TOCStyle</span>();
tocstyle.addStyle("User_20_Index_20_1", 1);
tocstyle.addStyle("User_20_Index_20_2", 2);
tocstyle.addStyle("User_20_Index_20_3", 3);
tocstyle.addStyle("User_20_Index_20_4", 4);
tocstyle.addStyle("User_20_Index_20_5", 5);
tocstyle.addStyle("User_20_Index_20_6", 6);
tocstyle.addStyle("User_20_Index_20_7", 7);
tocstyle.addStyle("User_20_Index_20_8", 8);
tocstyle.addStyle("User_20_Index_20_9", 9);
tocstyle.addStyle("User_20_Index_20_10", 10);
<span class='javaclass'>TextTableOfContentElement</span> textTableOfContentElement = doc.createTOCwithStyle(paragraph1, tocstyle, <span class='basic'>true</span>);
</pre>
<br/><br/>
<div class="navigation">
<ul>
<li><a href="Manipulate Metadata.html">previous</a></li>
<li><a href="Presentation Document.html">next</a></li>
<li><a href="#">top</a></li>
</ul>
</div>
<link type="text/css" rel="stylesheet" href="cookbook.css"/>