| 1 | package pro.verron.officestamper.preset; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; | |
| 4 | import org.docx4j.wml.R; | |
| 5 | import org.jspecify.annotations.Nullable; | |
| 6 | import pro.verron.officestamper.api.DocxPart; | |
| 7 | import pro.verron.officestamper.api.OfficeStamperException; | |
| 8 | import pro.verron.officestamper.utils.wml.WmlFactory; | |
| 9 | ||
| 10 | import java.io.IOException; | |
| 11 | import java.io.InputStream; | |
| 12 | ||
| 13 | /// This class describes an image, which will be inserted into a document. | |
| 14 | /// | |
| 15 | /// @author Joseph Verron | |
| 16 | /// @author Romster | |
| 17 | /// @version ${version} | |
| 18 | /// @since 1.0.0 | |
| 19 | public final class Image { | |
| 20 | ||
| 21 | private final byte[] imageBytes; | |
| 22 | private final @Nullable Integer maxWidth; | |
| 23 | private final String filenameHint; | |
| 24 | private final String altText; | |
| 25 | ||
| 26 | /// Constructor for Image. | |
| 27 | /// | |
| 28 | /// @param in - content of the image as InputStream | |
| 29 | /// | |
| 30 | /// @throws IOException if any. | |
| 31 | public Image(InputStream in) | |
| 32 | throws IOException { | |
| 33 | this(in.readAllBytes()); | |
| 34 | } | |
| 35 | ||
| 36 | /// Constructor for Image. | |
| 37 | /// | |
| 38 | /// @param imageBytes - content of the image as an array of the bytes | |
| 39 | public Image(byte[] imageBytes) { | |
| 40 | this(imageBytes, null); | |
| 41 | } | |
| 42 | ||
| 43 | /// Constructor for Image. | |
| 44 | /// | |
| 45 | /// @param imageBytes - content of the image as an array of the bytes | |
| 46 | /// @param maxWidth - max width of the image in twip | |
| 47 | public Image(byte[] imageBytes, @Nullable Integer maxWidth) { | |
| 48 | this(imageBytes, maxWidth, "dummyFileName", "dummyAltText"); | |
| 49 | } | |
| 50 | ||
| 51 | /// Constructor for Image. | |
| 52 | /// | |
| 53 | /// @param imageBytes content of the image as an array of the bytes | |
| 54 | /// @param maxWidth max width of the image in twip | |
| 55 | /// @param filenameHint filename hint for the image. | |
| 56 | /// @param altText alternative text for the image. | |
| 57 | public Image(byte[] imageBytes, @Nullable Integer maxWidth, String filenameHint, String altText) { | |
| 58 | this.imageBytes = imageBytes; | |
| 59 | this.maxWidth = maxWidth; | |
| 60 | this.filenameHint = filenameHint; | |
| 61 | this.altText = altText; | |
| 62 | } | |
| 63 | ||
| 64 | /// Constructor for Image. | |
| 65 | /// | |
| 66 | /// @param in - content of the image as InputStream | |
| 67 | /// @param maxWidth - max width of the image in twip | |
| 68 | /// | |
| 69 | /// @throws IOException if any. | |
| 70 | public Image(InputStream in, @Nullable Integer maxWidth) | |
| 71 | throws IOException { | |
| 72 | this(in.readAllBytes(), maxWidth); | |
| 73 | } | |
| 74 | ||
| 75 | /// Creates a new run with the provided image and associated metadata. | |
| 76 | /// | |
| 77 | /// TODO: adding the same image twice will put the image twice into the docx-zip file. | |
| 78 | /// We should make the second addition of the same image a reference instead. | |
| 79 | /// | |
| 80 | /// @param part The document part where the image will be inserted. | |
| 81 | /// | |
| 82 | /// @return The created run containing the image. | |
| 83 | /// | |
| 84 | /// @throws OfficeStamperException If there is an error creating the image part | |
| 85 | public R newRun(DocxPart part) { | |
| 86 | var mlPackage = part.document(); | |
| 87 | try { | |
| 88 | var image = BinaryPartAbstractImage.createImagePart(mlPackage, part.part(), imageBytes); | |
| 89 |
1
1. newRun : replaced return value with null for pro/verron/officestamper/preset/Image::newRun → TIMED_OUT |
return WmlFactory.newRun(maxWidth, image, filenameHint, altText); |
| 90 | } catch (Exception e) { | |
| 91 | throw new OfficeStamperException("Failed to create an ImagePart", e); | |
| 92 | } | |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 89 |
1.1 |