2
* $Id: RtfImage.java,v 1.23 2005/12/24 13:14:28 hallm Exp $
5
* Copyright 2001, 2002, 2003, 2004 by Mark Hall
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* (the "License"); you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the License.
15
* The Original Code is 'iText, a free JAVA-PDF library'.
17
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19
* All Rights Reserved.
20
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23
* Contributor(s): all the names of the contributors are added in the source code
26
* Alternatively, the contents of this file may be used under the terms of the
27
* LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28
* provisions of LGPL are applicable instead of those above. If you wish to
29
* allow use of your version of this file only under the terms of the LGPL
30
* License and not to allow others to use your version of this file under
31
* the MPL, indicate your decision by deleting the provisions above and
32
* replace them with the notice and other provisions required by the LGPL.
33
* If you do not delete the provisions above, a recipient may use your version
34
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
* This library is free software; you can redistribute it and/or modify it
37
* under the terms of the MPL as stated above or under the terms of the GNU
38
* Library General Public License as published by the Free Software Foundation;
39
* either version 2 of the License, or any later version.
41
* This library is distributed in the hope that it will be useful, but WITHOUT
42
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46
* If you didn't download this code from the following link, you should check if
47
* you aren't using an obsolete version:
48
* http://www.lowagie.com/iText/
51
package com.lowagie.text.rtf.graphic;
53
import java.io.ByteArrayInputStream;
54
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
56
import java.io.InputStream;
58
import com.lowagie.text.DocumentException;
59
import com.lowagie.text.Element;
60
import com.lowagie.text.Image;
61
import com.lowagie.text.pdf.codec.wmf.MetaDo;
62
import com.lowagie.text.rtf.RtfElement;
63
import com.lowagie.text.rtf.document.RtfDocument;
64
import com.lowagie.text.rtf.style.RtfParagraphStyle;
65
import com.lowagie.text.rtf.text.RtfParagraph;
69
* The RtfImage contains one image. Supported image types are jpeg, png, wmf, bmp.
71
* @version $Revision: 1.23 $
72
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
73
* @author Paulo Soares
75
public class RtfImage extends RtfElement {
78
* Constant for the shape/picture group
80
private static final byte[] PICTURE_GROUP = "\\*\\shppict".getBytes();
82
* Constant for a picture
84
private static final byte[] PICTURE = "\\pict".getBytes();
86
* Constant for a jpeg image
88
private static final byte[] PICTURE_JPEG = "\\jpegblip".getBytes();
90
* Constant for a png image
92
private static final byte[] PICTURE_PNG = "\\pngblip".getBytes();
94
* Constant for a bmp image
96
private static final byte[] PICTURE_BMP = "\\dibitmap0".getBytes();
98
* Constant for a wmf image
100
private static final byte[] PICTURE_WMF = "\\wmetafile8".getBytes();
102
* Constant for the picture width
104
private static final byte[] PICTURE_WIDTH = "\\picw".getBytes();
106
* Constant for the picture height
108
private static final byte[] PICTURE_HEIGHT = "\\pich".getBytes();
110
* Constant for the picture width scale
112
private static final byte[] PICTURE_SCALED_WIDTH = "\\picwgoal".getBytes();
114
* Constant for the picture height scale
116
private static final byte[] PICTURE_SCALED_HEIGHT = "\\pichgoal".getBytes();
119
* The type of image this is.
121
private int imageType = Image.ORIGINAL_NONE;
123
* The actual image. Already formated for direct inclusion in the rtf document
125
private byte[] image = new byte[0];
127
* The alignment of this picture
129
private int alignment = Element.ALIGN_LEFT;
131
* The width of this picture
133
private float width = 0;
135
* The height of this picutre
137
private float height = 0;
139
* The intended display width of this picture
141
private float plainWidth = 0;
143
* The intended display height of this picture
145
private float plainHeight = 0;
147
* Whether this RtfImage is a top level element and should
148
* be an extra paragraph.
150
private boolean topLevelElement = false;
153
* Constructs a RtfImage for an Image.
155
* @param doc The RtfDocument this RtfImage belongs to
156
* @param image The Image that this RtfImage wraps
157
* @throws DocumentException If an error occured accessing the image content
159
public RtfImage(RtfDocument doc, Image image) throws DocumentException {
161
imageType = image.getOriginalType();
162
if (!(imageType == Image.ORIGINAL_JPEG || imageType == Image.ORIGINAL_BMP
163
|| imageType == Image.ORIGINAL_PNG || imageType == Image.ORIGINAL_WMF)) {
164
throw new DocumentException("Only BMP, PNG, WMF and JPEG images are supported by the RTF Writer");
166
alignment = image.alignment();
167
width = image.width();
168
height = image.height();
169
plainWidth = image.plainWidth();
170
plainHeight = image.plainHeight();
171
this.image = getImage(image);
175
* Extracts the image data from the Image. The data is formated for direct inclusion
178
* @param image The Image for which to extract the content
179
* @return The image data formated for the rtf document
180
* @throws DocumentException If an error occurs accessing the image content
182
private byte[] getImage(Image image) throws DocumentException {
183
ByteArrayOutputStream imageTemp = new ByteArrayOutputStream();
186
if (imageType == Image.ORIGINAL_BMP) {
187
imageIn = new ByteArrayInputStream(MetaDo.wrapBMP(image));
189
if (image.getOriginalData() == null) {
190
imageIn = image.url().openStream();
192
imageIn = new ByteArrayInputStream(image.getOriginalData());
194
if (imageType == Image.ORIGINAL_WMF) { //remove the placeable header
195
long skipLength = 22;
196
while(skipLength > 0) {
197
skipLength = skipLength - imageIn.skip(skipLength);
203
while((buffer = imageIn.read()) != -1) {
204
String helperStr = Integer.toHexString(buffer);
205
if (helperStr.length() < 2) helperStr = "0" + helperStr;
206
imageTemp.write(helperStr.getBytes());
209
imageTemp.write((byte) '\n');
213
} catch(IOException ioe) {
214
throw new DocumentException(ioe.getMessage());
216
return imageTemp.toByteArray();
220
* Writes the RtfImage content
222
* @return the RtfImage content
224
public byte[] write() {
225
ByteArrayOutputStream result = new ByteArrayOutputStream();
227
if(this.topLevelElement) {
228
result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
231
case Element.ALIGN_LEFT:
232
result.write(RtfParagraphStyle.ALIGN_LEFT);
234
case Element.ALIGN_RIGHT:
235
result.write(RtfParagraphStyle.ALIGN_RIGHT);
237
case Element.ALIGN_CENTER:
238
result.write(RtfParagraphStyle.ALIGN_CENTER);
240
case Element.ALIGN_JUSTIFIED:
241
result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
244
result.write(OPEN_GROUP);
245
result.write(PICTURE_GROUP);
246
result.write(OPEN_GROUP);
247
result.write(PICTURE);
249
case Image.ORIGINAL_JPEG:
250
result.write(PICTURE_JPEG);
252
case Image.ORIGINAL_PNG:
253
result.write(PICTURE_PNG);
255
case Image.ORIGINAL_WMF:
256
case Image.ORIGINAL_BMP:
257
result.write(PICTURE_WMF);
260
result.write(PICTURE_WIDTH);
261
result.write(intToByteArray((int) width));
262
result.write(PICTURE_HEIGHT);
263
result.write(intToByteArray((int) height));
264
if(width != plainWidth || this.imageType == Image.ORIGINAL_BMP) {
265
result.write(PICTURE_SCALED_WIDTH);
266
result.write(intToByteArray((int) (plainWidth * RtfElement.TWIPS_FACTOR)));
268
if(height != plainHeight || this.imageType == Image.ORIGINAL_BMP) {
269
result.write(PICTURE_SCALED_HEIGHT);
270
result.write(intToByteArray((int) (plainHeight * RtfElement.TWIPS_FACTOR)));
272
result.write(DELIMITER);
273
result.write((byte) '\n');
275
result.write(CLOSE_GROUP);
276
result.write(CLOSE_GROUP);
277
if(this.topLevelElement) {
278
result.write(RtfParagraph.PARAGRAPH);
279
result.write(RtfParagraph.PARAGRAPH);
281
result.write((byte) '\n');
282
} catch(IOException ioe) {
283
ioe.printStackTrace();
285
return result.toByteArray();
289
* Sets the alignment of this RtfImage. Uses the alignments from com.lowagie.text.Element.
291
* @param alignment The alignment to use.
293
public void setAlignment(int alignment) {
294
this.alignment = alignment;
298
* Set whether this RtfImage should behave like a top level element
299
* and enclose itself in a paragraph.
301
* @param topLevelElement Whether to behave like a top level element.
303
public void setTopLevelElement(boolean topLevelElement) {
304
this.topLevelElement = topLevelElement;