| 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 | |
|---|
| 25 | import java.io.FileOutputStream; |
|---|
| 26 | import java.io.FileInputStream; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBContext; |
|---|
| 29 | import javax.xml.bind.JAXBElement; |
|---|
| 30 | import javax.xml.bind.Marshaller; |
|---|
| 31 | import javax.xml.bind.Unmarshaller; |
|---|
| 32 | |
|---|
| 33 | import org.docx4j.jaxb.Context; |
|---|
| 34 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 35 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 36 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 37 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 38 | import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart; |
|---|
| 39 | |
|---|
| 40 | /* |
|---|
| 41 | * This is an example of loading a file from the pkg format Word 2007 |
|---|
| 42 | * can produce, and optionally, saving it as a docx zip file. |
|---|
| 43 | * |
|---|
| 44 | * This is a convenient format for loading certain test cases. |
|---|
| 45 | * |
|---|
| 46 | * */ |
|---|
| 47 | public class ImportFromPackageFormat extends AbstractSample { |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * @param args |
|---|
| 51 | */ |
|---|
| 52 | public static void main(String[] args) throws Exception { |
|---|
| 53 | |
|---|
| 54 | try { |
|---|
| 55 | getInputFilePath(args); |
|---|
| 56 | } catch (IllegalArgumentException e) { |
|---|
| 57 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml"; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | // Do we want to save output? |
|---|
| 61 | boolean save = true; |
|---|
| 62 | // If so, where to? |
|---|
| 63 | String outputfilepath = inputfilepath + ".docx"; |
|---|
| 64 | |
|---|
| 65 | try { |
|---|
| 66 | JAXBContext jc = Context.jcXmlPackage; |
|---|
| 67 | Unmarshaller u = jc.createUnmarshaller(); |
|---|
| 68 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); |
|---|
| 69 | |
|---|
| 70 | org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)((JAXBElement)u.unmarshal( |
|---|
| 71 | new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)))).getValue(); |
|---|
| 72 | |
|---|
| 73 | org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter( wmlPackageEl); |
|---|
| 74 | |
|---|
| 75 | WordprocessingMLPackage wmlPackage = (WordprocessingMLPackage)xmlPackage.get(); |
|---|
| 76 | |
|---|
| 77 | // Now that its loaded properly, lets just save it |
|---|
| 78 | |
|---|
| 79 | if (save) { |
|---|
| 80 | SaveToZipFile saver = new SaveToZipFile(wmlPackage); |
|---|
| 81 | saver.save(outputfilepath); |
|---|
| 82 | System.out.println( "\n\n .. written to " + outputfilepath); |
|---|
| 83 | } else { |
|---|
| 84 | // Display its contents |
|---|
| 85 | System.out.println( "\n\n..done " ); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | } catch (Exception exc) { |
|---|
| 90 | exc.printStackTrace(); |
|---|
| 91 | throw new RuntimeException(exc); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | } |
|---|