| 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.openpackaging.parts.WordprocessingML; |
|---|
| 22 | |
|---|
| 23 | //import java.io.IOException; |
|---|
| 24 | |
|---|
| 25 | //import javax.xml.bind.JAXBElement; |
|---|
| 26 | import java.io.IOException; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBException; |
|---|
| 29 | import javax.xml.bind.Unmarshaller; |
|---|
| 30 | |
|---|
| 31 | import org.apache.log4j.Logger; |
|---|
| 32 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 33 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 34 | import org.docx4j.openpackaging.parts.JaxbXmlPart; |
|---|
| 35 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 36 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 37 | import org.docx4j.wml.FontRel; |
|---|
| 38 | import org.docx4j.wml.Fonts; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | public final class FontTablePart extends JaxbXmlPart<Fonts> { |
|---|
| 42 | |
|---|
| 43 | private static Logger log = Logger.getLogger(FontTablePart.class); |
|---|
| 44 | |
|---|
| 45 | public FontTablePart(PartName partName) throws InvalidFormatException { |
|---|
| 46 | super(partName); |
|---|
| 47 | init(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public FontTablePart() throws InvalidFormatException { |
|---|
| 51 | super(new PartName("/word/fontTable.xml")); |
|---|
| 52 | init(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public void init() { |
|---|
| 56 | // Used if this Part is added to [Content_Types].xml |
|---|
| 57 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 58 | org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_FONTTABLE)); |
|---|
| 59 | |
|---|
| 60 | // Used when this Part is added to a rels |
|---|
| 61 | setRelationshipType(Namespaces.FONT_TABLE); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Unmarshal a default font table, useful when creating this |
|---|
| 66 | * part from scratch. |
|---|
| 67 | * |
|---|
| 68 | * @return the newly created root object of the java content tree |
|---|
| 69 | * |
|---|
| 70 | * @throws JAXBException |
|---|
| 71 | * If any unexpected errors occur while unmarshalling |
|---|
| 72 | */ |
|---|
| 73 | public Object unmarshalDefaultFonts() throws JAXBException { |
|---|
| 74 | |
|---|
| 75 | java.io.InputStream is = null; |
|---|
| 76 | try { |
|---|
| 77 | // Works in Eclipse - not absence of leading '/' |
|---|
| 78 | is = org.docx4j.utils.ResourceUtils.getResource("org/docx4j/openpackaging/parts/WordprocessingML/fontTable.xml"); |
|---|
| 79 | } catch (IOException e) { |
|---|
| 80 | // TODO Auto-generated catch block |
|---|
| 81 | e.printStackTrace(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return unmarshal( is ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public void processEmbeddings() { |
|---|
| 88 | |
|---|
| 89 | Fonts fonts = (org.docx4j.wml.Fonts)this.getJaxbElement(); |
|---|
| 90 | for (Fonts.Font font : fonts.getFont() ) { |
|---|
| 91 | String fontName = font.getName(); |
|---|
| 92 | |
|---|
| 93 | FontRel embedRegular = font.getEmbedRegular(); |
|---|
| 94 | FontRel embedBold = font.getEmbedBold(); |
|---|
| 95 | FontRel embedBoldItalic = font.getEmbedBoldItalic(); |
|---|
| 96 | FontRel embedItalic = font.getEmbedItalic(); |
|---|
| 97 | |
|---|
| 98 | getObfuscatedFontFromRelationship(fontName, embedRegular); |
|---|
| 99 | getObfuscatedFontFromRelationship(fontName, embedBold); |
|---|
| 100 | getObfuscatedFontFromRelationship(fontName, embedBoldItalic); |
|---|
| 101 | getObfuscatedFontFromRelationship(fontName, embedItalic); |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | private void getObfuscatedFontFromRelationship(String fontName, FontRel fontRel) { |
|---|
| 107 | |
|---|
| 108 | if (fontRel == null) { |
|---|
| 109 | //log.debug("fontRel not found for '" + fontName + "'"); |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | String id = fontRel.getId(); |
|---|
| 114 | String fontKey = fontRel.getFontKey(); |
|---|
| 115 | |
|---|
| 116 | ObfuscatedFontPart obfuscatedFont = (ObfuscatedFontPart)this.getRelationshipsPart().getPart(id); |
|---|
| 117 | if (obfuscatedFont != null) { |
|---|
| 118 | obfuscatedFont.deObfuscate(fontName, fontKey); |
|---|
| 119 | } else { |
|---|
| 120 | log.error("Couldn't find ObfuscatedFontPart with id: " + id); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public static void main(String[] args) throws Exception { |
|---|
| 125 | String filepath = System.getProperty("user.dir") + "/sample-docs/FontEmbedded.docx"; |
|---|
| 126 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filepath)); |
|---|
| 127 | |
|---|
| 128 | wordMLPackage.getMainDocumentPart().getFontTablePart().processEmbeddings(); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | } |
|---|