1 | package pro.verron.officestamper.preset; | |
2 | ||
3 | import org.apache.commons.io.IOUtils; | |
4 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
5 | import org.docx4j.openpackaging.parts.Part; | |
6 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; | |
7 | import org.docx4j.wml.R; | |
8 | import pro.verron.officestamper.api.DocxPart; | |
9 | import pro.verron.officestamper.api.OfficeStamperException; | |
10 | import pro.verron.officestamper.utils.WmlFactory; | |
11 | ||
12 | import java.io.ByteArrayOutputStream; | |
13 | import java.io.IOException; | |
14 | import java.io.InputStream; | |
15 | ||
16 | /** | |
17 | * This class describes an image which will be inserted into a document. | |
18 | * | |
19 | * @author Joseph Verron | |
20 | * @author Romster | |
21 | * @version ${version} | |
22 | * @since 1.0.0 | |
23 | */ | |
24 | public final class Image { | |
25 | ||
26 | private final byte[] imageBytes; | |
27 | private Integer maxWidth; | |
28 | ||
29 | /** | |
30 | * <p>Constructor for Image.</p> | |
31 | * | |
32 | * @param in - content of the image as InputStream | |
33 | * | |
34 | * @throws IOException if any. | |
35 | */ | |
36 | public Image(InputStream in) | |
37 | throws IOException { | |
38 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
39 | IOUtils.copy(in, out); | |
40 | this.imageBytes = out.toByteArray(); | |
41 | } | |
42 | ||
43 | /** | |
44 | * <p>Constructor for Image.</p> | |
45 | * | |
46 | * @param in - content of the image as InputStream | |
47 | * @param maxWidth - max width of the image in twip | |
48 | * | |
49 | * @throws IOException if any. | |
50 | */ | |
51 | public Image(InputStream in, Integer maxWidth) | |
52 | throws IOException { | |
53 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
54 | IOUtils.copy(in, out); | |
55 | this.imageBytes = out.toByteArray(); | |
56 | this.maxWidth = maxWidth; | |
57 | } | |
58 | ||
59 | /** | |
60 | * <p>Constructor for Image.</p> | |
61 | * | |
62 | * @param imageBytes - content of the image as an array of the bytes | |
63 | */ | |
64 | public Image(byte[] imageBytes) { | |
65 | this.imageBytes = imageBytes; | |
66 | } | |
67 | ||
68 | /** | |
69 | * <p>Constructor for Image.</p> | |
70 | * | |
71 | * @param imageBytes - content of the image as an array of the bytes | |
72 | * @param maxWidth - max width of the image in twip | |
73 | */ | |
74 | public Image(byte[] imageBytes, Integer maxWidth) { | |
75 | this.imageBytes = imageBytes; | |
76 | this.maxWidth = maxWidth; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Creates a new run with the provided image and associated metadata. | |
81 | * <p> | |
82 | * TODO: adding the same image twice will put the image twice into the docx-zip file. | |
83 | * We should make the second addition of the same image a reference instead. | |
84 | * | |
85 | * @param document The document part where the image will be inserted. | |
86 | * @param filenameHint A hint for the filename to be used. | |
87 | * @param altText Alternative text for the image. | |
88 | * | |
89 | * @return The created run containing the image. | |
90 | * | |
91 | * @throws OfficeStamperException If there is an error creating the image part | |
92 | */ | |
93 | public R newRun(DocxPart document, String filenameHint, String altText) { | |
94 | WordprocessingMLPackage wordprocessingMLPackage = document.document(); | |
95 | Part part = document.part(); | |
96 | try { | |
97 | var image = BinaryPartAbstractImage.createImagePart(wordprocessingMLPackage, part, imageBytes); | |
98 |
1
1. newRun : replaced return value with null for pro/verron/officestamper/preset/Image::newRun → KILLED |
return WmlFactory.newRun(maxWidth, image, filenameHint, altText); |
99 | } catch (Exception e) { | |
100 | throw new OfficeStamperException("Failed to create an ImagePart", e); | |
101 | } | |
102 | } | |
103 | ||
104 | /** | |
105 | * <p>Getter for the field <code>maxWidth</code>.</p> | |
106 | * | |
107 | * @return a {@link Integer} object | |
108 | * | |
109 | * @deprecated use the {@link #newRun(DocxPart, String, String)} method directly to generate a Run with Inline | |
110 | * Drawing | |
111 | */ | |
112 | @Deprecated(since = "2.6", forRemoval = true) public Integer getMaxWidth() { | |
113 |
1
1. getMaxWidth : replaced Integer return value with 0 for pro/verron/officestamper/preset/Image::getMaxWidth → NO_COVERAGE |
return maxWidth; |
114 | } | |
115 | ||
116 | /** | |
117 | * <p>Getter for the field <code>imageBytes</code>.</p> | |
118 | * | |
119 | * @return an array of {@link byte} objects | |
120 | * | |
121 | * @deprecated use the {@link #newRun(DocxPart, String, String)} method directly to generate a Run with Inline | |
122 | * Drawing | |
123 | */ | |
124 | @Deprecated(since = "2.6", forRemoval = true) public byte[] getImageBytes() { | |
125 |
1
1. getImageBytes : replaced return value with null for pro/verron/officestamper/preset/Image::getImageBytes → NO_COVERAGE |
return imageBytes; |
126 | } | |
127 | } | |
Mutations | ||
98 |
1.1 |
|
113 |
1.1 |
|
125 |
1.1 |