| 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 | |
|---|
| 22 | package org.docx4j.samples; |
|---|
| 23 | |
|---|
| 24 | import org.docx4j.jaxb.Context; |
|---|
| 25 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 26 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 27 | |
|---|
| 28 | import org.docx4j.XmlUtils; |
|---|
| 29 | import org.docx4j.wml.P; |
|---|
| 30 | import org.docx4j.wml.P.Hyperlink; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Fun with hyperlinks |
|---|
| 35 | * |
|---|
| 36 | * @author Jason Harrop |
|---|
| 37 | * @version 1.0 |
|---|
| 38 | */ |
|---|
| 39 | public class HyperlinkTest { |
|---|
| 40 | |
|---|
| 41 | /* |
|---|
| 42 | * <w:p> |
|---|
| 43 | * <w:r> |
|---|
| 44 | * <w:t xml:space="preserve">Here is an example of a </w:t> |
|---|
| 45 | * </w:r> |
|---|
| 46 | * <w:hyperlink r:id="rId4" w:history="1"> |
|---|
| 47 | * <w:r> |
|---|
| 48 | * <w:rPr> |
|---|
| 49 | * <w:rStyle w:val="Hyperlink"/> |
|---|
| 50 | * </w:rPr> |
|---|
| 51 | * <w:t>hyperlink</w:t> |
|---|
| 52 | * </w:r> |
|---|
| 53 | * </w:hyperlink> |
|---|
| 54 | * <w:r> |
|---|
| 55 | * <w:t xml:space="preserve">. </w:t> |
|---|
| 56 | * </w:r> |
|---|
| 57 | * </w:p> |
|---|
| 58 | * |
|---|
| 59 | * |
|---|
| 60 | * word/_rels/document.xml.rels contains: |
|---|
| 61 | * |
|---|
| 62 | * <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" |
|---|
| 63 | * Target="http://dev.plutext.org/" TargetMode="External"/> |
|---|
| 64 | |
|---|
| 65 | */ |
|---|
| 66 | |
|---|
| 67 | public static void main(String[] args) throws Exception { |
|---|
| 68 | |
|---|
| 69 | System.out.println( "Creating package.."); |
|---|
| 70 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 71 | |
|---|
| 72 | // Create hyperlink |
|---|
| 73 | Hyperlink link = createHyperlink(wordMLPackage, "http://slashdot.org"); |
|---|
| 74 | |
|---|
| 75 | // Add it to a paragraph |
|---|
| 76 | org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 77 | org.docx4j.wml.P paragraph = wmlFactory.createP(); |
|---|
| 78 | |
|---|
| 79 | paragraph.getParagraphContent().add( link ); |
|---|
| 80 | wordMLPackage.getMainDocumentPart().addObject(paragraph); |
|---|
| 81 | |
|---|
| 82 | // Now save it |
|---|
| 83 | wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/out-hyperlink.docx") ); |
|---|
| 84 | |
|---|
| 85 | System.out.println("Done."); |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public static Hyperlink createHyperlink(WordprocessingMLPackage wordMLPackage, String url) { |
|---|
| 90 | |
|---|
| 91 | try { |
|---|
| 92 | |
|---|
| 93 | // We need to add a relationship to word/_rels/document.xml.rels |
|---|
| 94 | // but since its external, we don't use the |
|---|
| 95 | // usual wordMLPackage.getMainDocumentPart().addTargetPart |
|---|
| 96 | // mechanism |
|---|
| 97 | org.docx4j.relationships.ObjectFactory factory = |
|---|
| 98 | new org.docx4j.relationships.ObjectFactory(); |
|---|
| 99 | |
|---|
| 100 | org.docx4j.relationships.Relationship rel = factory.createRelationship(); |
|---|
| 101 | rel.setType( Namespaces.HYPERLINK ); |
|---|
| 102 | rel.setTarget(url); |
|---|
| 103 | rel.setTargetMode("External"); |
|---|
| 104 | |
|---|
| 105 | wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel); |
|---|
| 106 | |
|---|
| 107 | // addRelationship sets the rel's @Id |
|---|
| 108 | |
|---|
| 109 | String hpl = "<w:hyperlink r:id=\"" + rel.getId() + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " + |
|---|
| 110 | "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" + |
|---|
| 111 | "<w:r>" + |
|---|
| 112 | "<w:rPr>" + |
|---|
| 113 | "<w:rStyle w:val=\"Hyperlink\" />" + // TODO: enable this style in the document! |
|---|
| 114 | "</w:rPr>" + |
|---|
| 115 | "<w:t>Link</w:t>" + |
|---|
| 116 | "</w:r>" + |
|---|
| 117 | "</w:hyperlink>"; |
|---|
| 118 | |
|---|
| 119 | // return (Hyperlink)XmlUtils.unmarshalString(hpl, Context.jc, P.Hyperlink.class); |
|---|
| 120 | return (Hyperlink)XmlUtils.unmarshalString(hpl); |
|---|
| 121 | |
|---|
| 122 | } catch (Exception e) { |
|---|
| 123 | // TODO Auto-generated catch block |
|---|
| 124 | e.printStackTrace(); |
|---|
| 125 | return null; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | |
|---|