| 1 | /* |
|---|
| 2 | * Copyright 2007-2010, 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.File; |
|---|
| 24 | import java.util.List; |
|---|
| 25 | |
|---|
| 26 | import org.docx4j.dml.wordprocessingDrawing.Inline; |
|---|
| 27 | import org.docx4j.model.structure.SectionWrapper; |
|---|
| 28 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 29 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 30 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 31 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; |
|---|
| 32 | import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; |
|---|
| 33 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 34 | import org.docx4j.relationships.Relationship; |
|---|
| 35 | import org.docx4j.wml.Hdr; |
|---|
| 36 | import org.docx4j.wml.HdrFtrRef; |
|---|
| 37 | import org.docx4j.wml.HeaderReference; |
|---|
| 38 | import org.docx4j.wml.ObjectFactory; |
|---|
| 39 | import org.docx4j.wml.SectPr; |
|---|
| 40 | |
|---|
| 41 | public class HeaderFooterCreate { |
|---|
| 42 | |
|---|
| 43 | private static ObjectFactory objectFactory = new ObjectFactory(); |
|---|
| 44 | |
|---|
| 45 | public static void main(String[] args) throws Exception { |
|---|
| 46 | |
|---|
| 47 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage |
|---|
| 48 | .createPackage(); |
|---|
| 49 | |
|---|
| 50 | MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 51 | |
|---|
| 52 | Relationship relationship = createHeaderPart(wordMLPackage); |
|---|
| 53 | |
|---|
| 54 | createHeaderReference(wordMLPackage, relationship); |
|---|
| 55 | |
|---|
| 56 | wordMLPackage.save(new File(System.getProperty("user.dir"), |
|---|
| 57 | "headerFooter.docx")); |
|---|
| 58 | // mainDocumentPart.marshal(new FileOutputStream(new File(System |
|---|
| 59 | // .getProperty("user.dir"), "headerfooter.xml"))); |
|---|
| 60 | |
|---|
| 61 | System.out.println("done"); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public static Relationship createHeaderPart( |
|---|
| 65 | WordprocessingMLPackage wordprocessingMLPackage) |
|---|
| 66 | throws Exception { |
|---|
| 67 | |
|---|
| 68 | HeaderPart headerPart = new HeaderPart(); |
|---|
| 69 | |
|---|
| 70 | // Have to do this so that the next line can |
|---|
| 71 | // add an image |
|---|
| 72 | headerPart.setPackage(wordprocessingMLPackage); |
|---|
| 73 | |
|---|
| 74 | headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart)); |
|---|
| 75 | return wordprocessingMLPackage.getMainDocumentPart() |
|---|
| 76 | .addTargetPart(headerPart); |
|---|
| 77 | |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage, |
|---|
| 81 | Part sourcePart) throws Exception { |
|---|
| 82 | |
|---|
| 83 | Hdr hdr = objectFactory.createHdr(); |
|---|
| 84 | hdr.getEGBlockLevelElts().add( |
|---|
| 85 | newImage(wordprocessingMLPackage, |
|---|
| 86 | sourcePart, getBytes(), "filename", "alttext", 1, 2 |
|---|
| 87 | ) |
|---|
| 88 | ); |
|---|
| 89 | return hdr; |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public static byte[] getBytes() throws Exception { |
|---|
| 94 | |
|---|
| 95 | File file = new File(System.getProperty("user.dir") + "/tmp/imagex.wmf" ); |
|---|
| 96 | |
|---|
| 97 | java.io.InputStream is = new java.io.FileInputStream(file ); |
|---|
| 98 | long length = file.length(); |
|---|
| 99 | // You cannot create an array using a long type. |
|---|
| 100 | // It needs to be an int type. |
|---|
| 101 | if (length > Integer.MAX_VALUE) { |
|---|
| 102 | System.out.println("File too large!!"); |
|---|
| 103 | } |
|---|
| 104 | byte[] bytes = new byte[(int)length]; |
|---|
| 105 | int offset = 0; |
|---|
| 106 | int numRead = 0; |
|---|
| 107 | while (offset < bytes.length |
|---|
| 108 | && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
|---|
| 109 | offset += numRead; |
|---|
| 110 | } |
|---|
| 111 | // Ensure all the bytes have been read in |
|---|
| 112 | if (offset < bytes.length) { |
|---|
| 113 | System.out.println("Could not completely read file "+file.getName()); |
|---|
| 114 | } |
|---|
| 115 | is.close(); |
|---|
| 116 | |
|---|
| 117 | return bytes; |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | // public static P getP() { |
|---|
| 124 | // P headerP = objectFactory.createP(); |
|---|
| 125 | // R run1 = objectFactory.createR(); |
|---|
| 126 | // Text text = objectFactory.createText(); |
|---|
| 127 | // text.setValue("123head123"); |
|---|
| 128 | // run1.getRunContent().add(text); |
|---|
| 129 | // headerP.getParagraphContent().add(run1); |
|---|
| 130 | // return headerP; |
|---|
| 131 | // } |
|---|
| 132 | |
|---|
| 133 | public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage, |
|---|
| 134 | Part sourcePart, |
|---|
| 135 | byte[] bytes, |
|---|
| 136 | String filenameHint, String altText, |
|---|
| 137 | int id1, int id2) throws Exception { |
|---|
| 138 | |
|---|
| 139 | BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, |
|---|
| 140 | sourcePart, bytes); |
|---|
| 141 | |
|---|
| 142 | Inline inline = imagePart.createImageInline( filenameHint, altText, |
|---|
| 143 | id1, id2); |
|---|
| 144 | |
|---|
| 145 | // Now add the inline in w:p/w:r/w:drawing |
|---|
| 146 | org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 147 | org.docx4j.wml.P p = factory.createP(); |
|---|
| 148 | org.docx4j.wml.R run = factory.createR(); |
|---|
| 149 | p.getParagraphContent().add(run); |
|---|
| 150 | org.docx4j.wml.Drawing drawing = factory.createDrawing(); |
|---|
| 151 | run.getRunContent().add(drawing); |
|---|
| 152 | drawing.getAnchorOrInline().add(inline); |
|---|
| 153 | |
|---|
| 154 | return p; |
|---|
| 155 | |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | public static void createHeaderReference( |
|---|
| 160 | WordprocessingMLPackage wordprocessingMLPackage, |
|---|
| 161 | Relationship relationship ) |
|---|
| 162 | throws InvalidFormatException { |
|---|
| 163 | |
|---|
| 164 | List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections(); |
|---|
| 165 | |
|---|
| 166 | SectPr sectPr = sections.get(sections.size() - 1).getSectPr(); |
|---|
| 167 | // There is always a section wrapper, but it might not contain a sectPr |
|---|
| 168 | if (sectPr==null ) { |
|---|
| 169 | sectPr = objectFactory.createSectPr(); |
|---|
| 170 | wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr); |
|---|
| 171 | sections.get(sections.size() - 1).setSectPr(sectPr); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | HeaderReference headerReference = objectFactory.createHeaderReference(); |
|---|
| 175 | headerReference.setId(relationship.getId()); |
|---|
| 176 | headerReference.setType(HdrFtrRef.DEFAULT); |
|---|
| 177 | sectPr.getEGHdrFtrReferences().add(headerReference);// add header or |
|---|
| 178 | // footer references |
|---|
| 179 | |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | } |
|---|