| 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.io.File; |
|---|
| 25 | import java.io.FileOutputStream; |
|---|
| 26 | import java.util.List; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBContext; |
|---|
| 29 | import javax.xml.bind.JAXBElement; |
|---|
| 30 | import javax.xml.bind.JAXBException; |
|---|
| 31 | import javax.xml.bind.Unmarshaller; |
|---|
| 32 | |
|---|
| 33 | import org.docx4j.dml.picture.Pic; |
|---|
| 34 | import org.docx4j.dml.wordprocessingDrawing.Anchor; |
|---|
| 35 | import org.docx4j.dml.wordprocessingDrawing.Inline; |
|---|
| 36 | import org.docx4j.openpackaging.io.LoadFromZipFile; |
|---|
| 37 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 38 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 39 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 40 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; |
|---|
| 41 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 42 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 43 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; |
|---|
| 44 | import org.docx4j.relationships.Relationship; |
|---|
| 45 | import org.docx4j.relationships.Relationships; |
|---|
| 46 | import org.docx4j.wml.Body; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Takes existing images internal to the package, |
|---|
| 51 | * and points at them with TargetMode="External", |
|---|
| 52 | * and r:link (instead of r:embed). |
|---|
| 53 | * |
|---|
| 54 | * Also saves the target part as a file. |
|---|
| 55 | * |
|---|
| 56 | * @author jharrop |
|---|
| 57 | * |
|---|
| 58 | */ |
|---|
| 59 | public class ConvertEmbeddedImageToLinked { |
|---|
| 60 | |
|---|
| 61 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 62 | |
|---|
| 63 | public String generateTargetUri( String username, String hash, String extension ) { |
|---|
| 64 | |
|---|
| 65 | // See spec 8.3.3 |
|---|
| 66 | |
|---|
| 67 | return null; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * @param args |
|---|
| 75 | */ |
|---|
| 76 | public static void main(String[] args) throws Exception { |
|---|
| 77 | |
|---|
| 78 | String BASE_DIR = System.getProperty("user.dir"); |
|---|
| 79 | |
|---|
| 80 | //String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/jpeg.docx"; |
|---|
| 81 | String inputfilepath = BASE_DIR + "/png1.docx"; |
|---|
| 82 | //String inputfilepath = System.getProperty("user.dir") + "/sample-docs/AutoOpen.docm"; |
|---|
| 83 | |
|---|
| 84 | boolean saveImages = true; |
|---|
| 85 | boolean saveResultingDoc = true; |
|---|
| 86 | |
|---|
| 87 | String outputfilepath = BASE_DIR + "/imageLinked.docx"; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | // Open a document from the file system |
|---|
| 91 | // Load the Package |
|---|
| 92 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | // Change the rels to TargetMode="External" |
|---|
| 96 | // Fetch rels part |
|---|
| 97 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 98 | RelationshipsPart relsPart = documentPart.getRelationshipsPart(); |
|---|
| 99 | Relationships rels = relsPart.getRelationships(); |
|---|
| 100 | List<Relationship> relsList = rels.getRelationship(); |
|---|
| 101 | |
|---|
| 102 | // For each image rel |
|---|
| 103 | for (Relationship r : relsList) { |
|---|
| 104 | |
|---|
| 105 | if ( r.getType().equals( Namespaces.IMAGE ) ) { |
|---|
| 106 | // .. externalise |
|---|
| 107 | r.setTargetMode("External"); |
|---|
| 108 | |
|---|
| 109 | String target = r.getTarget(); |
|---|
| 110 | System.out.println("target: " + target); |
|---|
| 111 | |
|---|
| 112 | // TODO .. change name to username_hash.type |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | if (saveImages) { |
|---|
| 116 | // Save the image as a file in the specified location |
|---|
| 117 | |
|---|
| 118 | File f = new File(BASE_DIR + "/word/" + target); |
|---|
| 119 | if (f.exists()) { |
|---|
| 120 | System.out.println("Overwriting existing object: " + f.getPath() ); |
|---|
| 121 | } else if ( !f.getParentFile().exists() ) { |
|---|
| 122 | f.getParentFile().mkdirs(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | Part p = relsPart.getPart(r); |
|---|
| 126 | FileOutputStream fos = new FileOutputStream( f ); |
|---|
| 127 | ((BinaryPart)p).writeDataToOutputStream(fos); |
|---|
| 128 | fos.close(); |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | // Change r:embed to r:link |
|---|
| 140 | org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement(); |
|---|
| 141 | Body body = wmlDocumentEl.getBody(); |
|---|
| 142 | |
|---|
| 143 | List <Object> bodyChildren = body.getEGBlockLevelElts(); |
|---|
| 144 | |
|---|
| 145 | walkJAXBElements(bodyChildren); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | // Save it |
|---|
| 149 | |
|---|
| 150 | if (saveResultingDoc) { |
|---|
| 151 | SaveToZipFile saver = new SaveToZipFile(wordMLPackage); |
|---|
| 152 | saver.save(outputfilepath); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | static void walkJAXBElements(List <Object> bodyChildren){ |
|---|
| 157 | |
|---|
| 158 | for (Object o : bodyChildren ) { |
|---|
| 159 | |
|---|
| 160 | if ( o instanceof javax.xml.bind.JAXBElement) { |
|---|
| 161 | |
|---|
| 162 | System.out.println( o.getClass().getName() ); |
|---|
| 163 | System.out.println( ((JAXBElement)o).getName() ); |
|---|
| 164 | System.out.println( ((JAXBElement)o).getDeclaredType().getName() + "\n\n"); |
|---|
| 165 | |
|---|
| 166 | } else if (o instanceof org.docx4j.wml.P) { |
|---|
| 167 | System.out.println( "Paragraph object: "); |
|---|
| 168 | |
|---|
| 169 | walkList( ((org.docx4j.wml.P)o).getParagraphContent()); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | static void walkList(List children){ |
|---|
| 175 | |
|---|
| 176 | for (Object o : children ) { |
|---|
| 177 | System.out.println(" " + o.getClass().getName() ); |
|---|
| 178 | if ( o instanceof javax.xml.bind.JAXBElement) { |
|---|
| 179 | System.out.println(" " + ((JAXBElement)o).getName() ); |
|---|
| 180 | System.out.println(" " + ((JAXBElement)o).getDeclaredType().getName()); |
|---|
| 181 | |
|---|
| 182 | // TODO - unmarshall directly to Text. |
|---|
| 183 | if ( ((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Text") ) { |
|---|
| 184 | org.docx4j.wml.Text t = (org.docx4j.wml.Text)((JAXBElement)o).getValue(); |
|---|
| 185 | System.out.println(" " + t.getValue() ); |
|---|
| 186 | |
|---|
| 187 | } else if ( ((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Drawing") ) { |
|---|
| 188 | convertLinkToEmbed( (org.docx4j.wml.Drawing)((JAXBElement)o).getValue() ); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | } else if (o instanceof org.w3c.dom.Node) { |
|---|
| 194 | System.out.println(" IGNORED " + ((org.w3c.dom.Node)o).getNodeName() ); |
|---|
| 195 | } else if ( o instanceof org.docx4j.wml.R) { |
|---|
| 196 | org.docx4j.wml.R run = (org.docx4j.wml.R)o; |
|---|
| 197 | walkList(run.getRunContent()); |
|---|
| 198 | |
|---|
| 199 | } else { |
|---|
| 200 | |
|---|
| 201 | System.out.println(" IGNORED " + o.getClass().getName() ); |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | // else if ( o instanceof org.docx4j.jaxb.document.Text) { |
|---|
| 205 | // org.docx4j.jaxb.document.Text t = (org.docx4j.jaxb.document.Text)o; |
|---|
| 206 | // System.out.println(" " + t.getValue() ); |
|---|
| 207 | // } |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | static void convertLinkToEmbed( org.docx4j.wml.Drawing d ) { |
|---|
| 213 | |
|---|
| 214 | System.out.println(" describeDrawing " ); |
|---|
| 215 | |
|---|
| 216 | if ( d.getAnchorOrInline().get(0) instanceof Anchor ) { |
|---|
| 217 | |
|---|
| 218 | System.out.println(" ENCOUNTERED w:drawing/wp:anchor " ); |
|---|
| 219 | // That's all for now... |
|---|
| 220 | |
|---|
| 221 | } else if ( d.getAnchorOrInline().get(0) instanceof Inline ) { |
|---|
| 222 | |
|---|
| 223 | // Extract w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed |
|---|
| 224 | |
|---|
| 225 | Inline inline = (Inline )d.getAnchorOrInline().get(0); |
|---|
| 226 | |
|---|
| 227 | Pic pic = inline.getGraphic().getGraphicData().getPic(); |
|---|
| 228 | |
|---|
| 229 | System.out.println( "*** image relationship: " + pic.getBlipFill().getBlip().getEmbed() ); |
|---|
| 230 | |
|---|
| 231 | if (pic.getBlipFill().getBlip().getEmbed()!=null) { |
|---|
| 232 | |
|---|
| 233 | String relId = pic.getBlipFill().getBlip().getEmbed(); |
|---|
| 234 | // Add r:link |
|---|
| 235 | pic.getBlipFill().getBlip().setLink(relId); |
|---|
| 236 | // Remove r:embed |
|---|
| 237 | pic.getBlipFill().getBlip().setEmbed(null); |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | } else { |
|---|
| 243 | |
|---|
| 244 | System.out.println(" Didn't get Inline :( How to handle " + d.getAnchorOrInline().get(0).getClass().getName() ); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | } |
|---|