| 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 | |
|---|
| 22 | package org.docx4j.samples; |
|---|
| 23 | |
|---|
| 24 | import java.io.File; |
|---|
| 25 | import java.io.IOException; |
|---|
| 26 | |
|---|
| 27 | import org.docx4j.dml.wordprocessingDrawing.Inline; |
|---|
| 28 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 29 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 30 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 31 | |
|---|
| 32 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 33 | import org.docx4j.openpackaging.contenttype.ContentType; |
|---|
| 34 | import org.docx4j.openpackaging.contenttype.ContentTypes; |
|---|
| 35 | import org.docx4j.relationships.Relationship; |
|---|
| 36 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; |
|---|
| 37 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Creates a WordprocessingML document from scratch. |
|---|
| 41 | * |
|---|
| 42 | * @author Jason Harrop |
|---|
| 43 | * @version 1.0 |
|---|
| 44 | */ |
|---|
| 45 | public class AddImage { |
|---|
| 46 | |
|---|
| 47 | public static void main(String[] args) throws Exception { |
|---|
| 48 | |
|---|
| 49 | System.out.println( "Creating package.."); |
|---|
| 50 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | File file = new File("/home/dev/lanl/testing/fig1.pdf" ); |
|---|
| 54 | //File file = new File("C:\\Documents and Settings\\Jason Harrop\\My Documents\\LANL\\fig1.pdf" ); |
|---|
| 55 | |
|---|
| 56 | // Our utility method wants that as a byte array |
|---|
| 57 | |
|---|
| 58 | java.io.InputStream is = new java.io.FileInputStream(file ); |
|---|
| 59 | long length = file.length(); |
|---|
| 60 | // You cannot create an array using a long type. |
|---|
| 61 | // It needs to be an int type. |
|---|
| 62 | if (length > Integer.MAX_VALUE) { |
|---|
| 63 | System.out.println("File too large!!"); |
|---|
| 64 | } |
|---|
| 65 | byte[] bytes = new byte[(int)length]; |
|---|
| 66 | int offset = 0; |
|---|
| 67 | int numRead = 0; |
|---|
| 68 | while (offset < bytes.length |
|---|
| 69 | && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
|---|
| 70 | offset += numRead; |
|---|
| 71 | } |
|---|
| 72 | // Ensure all the bytes have been read in |
|---|
| 73 | if (offset < bytes.length) { |
|---|
| 74 | System.out.println("Could not completely read file "+file.getName()); |
|---|
| 75 | } |
|---|
| 76 | is.close(); |
|---|
| 77 | |
|---|
| 78 | String filenameHint = null; |
|---|
| 79 | String altText = null; |
|---|
| 80 | int id1 = 0; |
|---|
| 81 | int id2 = 1; |
|---|
| 82 | |
|---|
| 83 | org.docx4j.wml.P p = newImage( wordMLPackage, bytes, |
|---|
| 84 | filenameHint, altText, |
|---|
| 85 | id1, id2 ); |
|---|
| 86 | |
|---|
| 87 | // Now add our p to the document |
|---|
| 88 | wordMLPackage.getMainDocumentPart().addObject(p); |
|---|
| 89 | |
|---|
| 90 | org.docx4j.wml.P p2 = newImage( wordMLPackage, bytes, |
|---|
| 91 | filenameHint, altText, |
|---|
| 92 | id1, id2, 3000 ); |
|---|
| 93 | |
|---|
| 94 | // Now add our p to the document |
|---|
| 95 | wordMLPackage.getMainDocumentPart().addObject(p2); |
|---|
| 96 | |
|---|
| 97 | org.docx4j.wml.P p3 = newImage( wordMLPackage, bytes, |
|---|
| 98 | filenameHint, altText, |
|---|
| 99 | id1, id2, 6000 ); |
|---|
| 100 | |
|---|
| 101 | // Now add our p to the document |
|---|
| 102 | wordMLPackage.getMainDocumentPart().addObject(p3); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | // Now save it |
|---|
| 106 | wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/result.docx") ); |
|---|
| 107 | |
|---|
| 108 | System.out.println("Done."); |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage, |
|---|
| 113 | byte[] bytes, |
|---|
| 114 | String filenameHint, String altText, |
|---|
| 115 | int id1, int id2) throws Exception { |
|---|
| 116 | |
|---|
| 117 | BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); |
|---|
| 118 | |
|---|
| 119 | Inline inline = imagePart.createImageInline( filenameHint, altText, |
|---|
| 120 | id1, id2); |
|---|
| 121 | |
|---|
| 122 | // Now add the inline in w:p/w:r/w:drawing |
|---|
| 123 | org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 124 | org.docx4j.wml.P p = factory.createP(); |
|---|
| 125 | org.docx4j.wml.R run = factory.createR(); |
|---|
| 126 | p.getParagraphContent().add(run); |
|---|
| 127 | org.docx4j.wml.Drawing drawing = factory.createDrawing(); |
|---|
| 128 | run.getRunContent().add(drawing); |
|---|
| 129 | drawing.getAnchorOrInline().add(inline); |
|---|
| 130 | |
|---|
| 131 | return p; |
|---|
| 132 | |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage, |
|---|
| 136 | byte[] bytes, |
|---|
| 137 | String filenameHint, String altText, |
|---|
| 138 | int id1, int id2, long cx) throws Exception { |
|---|
| 139 | |
|---|
| 140 | BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); |
|---|
| 141 | |
|---|
| 142 | Inline inline = imagePart.createImageInline( filenameHint, altText, |
|---|
| 143 | id1, id2, cx); |
|---|
| 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 | } |
|---|