| 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 | import java.io.OutputStream; |
|---|
| 24 | |
|---|
| 25 | import org.docx4j.convert.out.Containerization; |
|---|
| 26 | import org.docx4j.convert.out.html.AbstractHtmlExporter; |
|---|
| 27 | import org.docx4j.convert.out.html.HtmlExporterNG2; |
|---|
| 28 | import org.docx4j.convert.out.html.SdtWriter; |
|---|
| 29 | import org.docx4j.convert.out.html.TagClass; |
|---|
| 30 | import org.docx4j.convert.out.html.TagSingleBox; |
|---|
| 31 | import org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings; |
|---|
| 32 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * If the source docx contained a WMF, that |
|---|
| 36 | * will get converted to inline SVG. In order |
|---|
| 37 | * to see the SVG in your browser, you'll need |
|---|
| 38 | * to rename the file to .xml or serve |
|---|
| 39 | * it with MIME type application/xhtml+xml |
|---|
| 40 | * |
|---|
| 41 | */ |
|---|
| 42 | public class CreateHtml extends AbstractSample { |
|---|
| 43 | |
|---|
| 44 | public static void main(String[] args) |
|---|
| 45 | throws Exception { |
|---|
| 46 | |
|---|
| 47 | try { |
|---|
| 48 | getInputFilePath(args); |
|---|
| 49 | } catch (IllegalArgumentException e) { |
|---|
| 50 | //inputfilepath = System.getProperty("user.dir") + "/docs/Docx4j_GettingStarted.xml"; |
|---|
| 51 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml"; |
|---|
| 52 | } |
|---|
| 53 | System.out.println(inputfilepath); |
|---|
| 54 | |
|---|
| 55 | boolean save = true; |
|---|
| 56 | |
|---|
| 57 | // Load .docx or Flat OPC .xml |
|---|
| 58 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 59 | |
|---|
| 60 | AbstractHtmlExporter exporter = new HtmlExporterNG2(); |
|---|
| 61 | |
|---|
| 62 | HtmlSettings htmlSettings = new HtmlSettings(); |
|---|
| 63 | |
|---|
| 64 | htmlSettings.setImageDirPath(inputfilepath + "_files"); |
|---|
| 65 | htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath.lastIndexOf("/")+1) |
|---|
| 66 | + "_files"); |
|---|
| 67 | |
|---|
| 68 | htmlSettings.setUserBodyTop("<H1>TOP!</H1>"); |
|---|
| 69 | htmlSettings.setUserBodyTail("<H1>TAIL!</H1>"); |
|---|
| 70 | |
|---|
| 71 | // Sample sdt tag handler (tag handlers insert specific |
|---|
| 72 | // html depending on the contents of an sdt's tag). |
|---|
| 73 | // This will only have an effect if the sdt tag contains |
|---|
| 74 | // the string @class=XXX |
|---|
| 75 | // SdtWriter.registerTagHandler("@class", new TagClass() ); |
|---|
| 76 | |
|---|
| 77 | SdtWriter.registerTagHandler(Containerization.TAG_BORDERS, new TagSingleBox() ); |
|---|
| 78 | SdtWriter.registerTagHandler(Containerization.TAG_SHADING, new TagSingleBox() ); |
|---|
| 79 | |
|---|
| 80 | OutputStream os; |
|---|
| 81 | if (save) { |
|---|
| 82 | os = new java.io.FileOutputStream(inputfilepath + ".html"); |
|---|
| 83 | } else { |
|---|
| 84 | os = System.out; |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os); |
|---|
| 89 | exporter.html(wordMLPackage, result, htmlSettings); |
|---|
| 90 | if (save) { |
|---|
| 91 | System.out.println("Saved: " + inputfilepath + ".html using " + exporter.getClass().getName() ); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | } |
|---|