| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.samples; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import java.math.BigInteger; |
|---|
| 25 | |
|---|
| 26 | import javax.xml.bind.JAXBContext; |
|---|
| 27 | import javax.xml.bind.JAXBElement; |
|---|
| 28 | |
|---|
| 29 | import org.docx4j.XmlUtils; |
|---|
| 30 | import org.docx4j.jaxb.Context; |
|---|
| 31 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 32 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 33 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 34 | import org.docx4j.wml.CTBookmark; |
|---|
| 35 | import org.docx4j.wml.CTMarkupRange; |
|---|
| 36 | import org.docx4j.wml.ObjectFactory; |
|---|
| 37 | import org.docx4j.wml.P; |
|---|
| 38 | import org.docx4j.wml.P.Hyperlink; |
|---|
| 39 | import org.docx4j.wml.R; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | public class BookmarkAdd extends AbstractSample { |
|---|
| 43 | |
|---|
| 44 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * @param args |
|---|
| 48 | */ |
|---|
| 49 | public static void main(String[] args) throws Exception { |
|---|
| 50 | |
|---|
| 51 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 52 | |
|---|
| 53 | String outputfilepath = System.getProperty("user.dir") |
|---|
| 54 | + "/bookmarkAdd-OUT.docx";; |
|---|
| 55 | |
|---|
| 56 | wordMLPackage.getMainDocumentPart().addParagraphOfText("x"); |
|---|
| 57 | wordMLPackage.getMainDocumentPart().addParagraphOfText("x"); |
|---|
| 58 | wordMLPackage.getMainDocumentPart().addParagraphOfText("hello world"); |
|---|
| 59 | P p = (P)wordMLPackage.getMainDocumentPart().getContent().get(2); |
|---|
| 60 | R r = (R)p.getContent().get(0); |
|---|
| 61 | |
|---|
| 62 | String bookmarkName = "abcd"; |
|---|
| 63 | bookmarkRun(p,r, bookmarkName, 123); |
|---|
| 64 | |
|---|
| 65 | wordMLPackage.getMainDocumentPart().addParagraphOfText("x"); |
|---|
| 66 | wordMLPackage.getMainDocumentPart().addParagraphOfText("x"); |
|---|
| 67 | |
|---|
| 68 | // Now add an internal hyperlink to it |
|---|
| 69 | Hyperlink h = MainDocumentPart.hyperlinkToBookmark(bookmarkName, "link to bookmark"); |
|---|
| 70 | wordMLPackage.getMainDocumentPart().addParagraphOfText("some text").getContent().add(h); |
|---|
| 71 | |
|---|
| 72 | System.out.println( XmlUtils.marshaltoString(p, true) ); |
|---|
| 73 | |
|---|
| 74 | SaveToZipFile saver = new SaveToZipFile(wordMLPackage); |
|---|
| 75 | saver.save(outputfilepath); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Surround the specified r in the specified p |
|---|
| 81 | * with a bookmark (with specified name and id) |
|---|
| 82 | * @param p |
|---|
| 83 | * @param r |
|---|
| 84 | * @param name |
|---|
| 85 | * @param id |
|---|
| 86 | */ |
|---|
| 87 | public static void bookmarkRun(P p, R r, String name, int id) { |
|---|
| 88 | |
|---|
| 89 | // Find the index |
|---|
| 90 | int index = p.getParagraphContent().indexOf(r); |
|---|
| 91 | |
|---|
| 92 | if (index<0) { |
|---|
| 93 | System.out.println("P does not contain R!"); |
|---|
| 94 | return; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | ObjectFactory factory = Context.getWmlObjectFactory(); |
|---|
| 98 | BigInteger ID = BigInteger.valueOf(id); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | // Add bookmark end first |
|---|
| 102 | CTMarkupRange mr = factory.createCTMarkupRange(); |
|---|
| 103 | mr.setId(ID); |
|---|
| 104 | JAXBElement<CTMarkupRange> bmEnd = factory.createBodyBookmarkEnd(mr); |
|---|
| 105 | p.getParagraphContent().add(index+1, bmEnd); // from 2.7.0, use getContent() |
|---|
| 106 | |
|---|
| 107 | // Next, bookmark start |
|---|
| 108 | CTBookmark bm = factory.createCTBookmark(); |
|---|
| 109 | bm.setId(ID); |
|---|
| 110 | bm.setName(name); |
|---|
| 111 | JAXBElement<CTBookmark> bmStart = factory.createBodyBookmarkStart(bm); |
|---|
| 112 | p.getParagraphContent().add(index, bmStart); |
|---|
| 113 | |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|