deprecate old picture type method (#865)

* deprecate old picture type method

* more changes
This commit is contained in:
PJ Fanning 2025-07-25 15:28:51 +01:00 committed by GitHub
parent 659dcb5dea
commit f73913cd84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 39 additions and 21 deletions

View File

@ -22,10 +22,10 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.common.usermodel.PictureType;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
@ -45,30 +45,30 @@ public final class SimpleImages {
XWPFRun r = p.createRun();
for (String imgFile : args) {
int format;
PictureType format;
if (imgFile.endsWith(".emf")) {
format = Document.PICTURE_TYPE_EMF;
format = PictureType.EMF;
} else if (imgFile.endsWith(".wmf")) {
format = Document.PICTURE_TYPE_WMF;
format = PictureType.WMF;
} else if (imgFile.endsWith(".pict")) {
format = Document.PICTURE_TYPE_PICT;
format = PictureType.PICT;
} else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
format = Document.PICTURE_TYPE_JPEG;
format = PictureType.JPEG;
} else if (imgFile.endsWith(".png")) {
format = Document.PICTURE_TYPE_PNG;
format = PictureType.PNG;
} else if (imgFile.endsWith(".dib")) {
format = Document.PICTURE_TYPE_DIB;
format = PictureType.DIB;
} else if (imgFile.endsWith(".gif")) {
format = Document.PICTURE_TYPE_GIF;
format = PictureType.GIF;
} else if (imgFile.endsWith(".tiff")) {
format = Document.PICTURE_TYPE_TIFF;
format = PictureType.TIFF;
} else if (imgFile.endsWith(".eps")) {
format = Document.PICTURE_TYPE_EPS;
format = PictureType.EPS;
} else if (imgFile.endsWith(".bmp")) {
format = Document.PICTURE_TYPE_BMP;
format = PictureType.BMP;
} else if (imgFile.endsWith(".wpg")) {
format = Document.PICTURE_TYPE_WPG;
format = PictureType.WPG;
} else {
System.err.println("Unsupported picture: " + imgFile +
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");

View File

@ -25,6 +25,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
@ -141,7 +142,10 @@ public class XWPFComments extends POIXMLDocumentPart {
* @return the index to this picture (0 based), the added picture can be
* obtained from {@link #getAllPictures()} .
* @throws InvalidFormatException If the format of the picture is not known.
* @deprecated use {@link #addPictureData(byte[], PictureType)} instead.
*/
@Deprecated
@Removal(version = "7.0.0")
public String addPictureData(byte[] pictureData, int format) throws InvalidFormatException {
return addPictureData(pictureData, PictureType.findByOoxmlId(format));
}

View File

@ -60,6 +60,7 @@ import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xddf.usermodel.chart.XDDFChart;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
@ -1724,7 +1725,10 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
* @return the next free ImageNumber
* @throws InvalidFormatException If the format of the picture is not known.
* @see #getNextPicNameNumber(PictureType)
* @deprecated use {@link #getNextPicNameNumber(PictureType)} instead.
*/
@Deprecated
@Removal(version = "7.0.0")
public int getNextPicNameNumber(int format) throws InvalidFormatException {
return getNextPicNameNumber(PictureType.findByOoxmlId(format));
}

View File

@ -31,6 +31,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
@ -235,7 +236,10 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
* @throws InvalidFormatException If the format of the picture is not known.
* @see #addPictureData(byte[], PictureType)
* @deprecated Use {@link #addPictureData(byte[], PictureType)} instead.
*/
@Deprecated
@Removal(version = "7.0.0")
public String addPictureData(byte[] pictureData, int format) throws InvalidFormatException {
return addPictureData(pictureData, PictureType.findByOoxmlId(format));
}

View File

@ -28,6 +28,7 @@ import org.apache.poi.ooxml.POIXMLRelation;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Removal;
/**
* Raw picture data, normally attached to a WordprocessingML Drawing.
@ -152,8 +153,10 @@ public class XWPFPictureData extends POIXMLDocumentPart {
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_GIF
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_SVG
* @see #getPictureTypeEnum()
* @deprecated use #getPictureTypeEnum()
*/
@Deprecated
@Removal(version = "7.0.0") // repurpose to return PictureType
public int getPictureType() {
String contentType = getPackagePart().getContentType();
for (int i = 0; i < RELATIONS.length; i++) {

View File

@ -1238,8 +1238,10 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_GIF
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_SVG
* @see #addPicture(InputStream, PictureType, String, int, int)
* @deprecated use #addPicture(InputStream, PictureType, String, int, int)
*/
@Deprecated
@Removal(version = "7.0.0")
public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
throws InvalidFormatException, IOException {
return addPicture(pictureData, PictureType.findByOoxmlId(pictureType), filename, width, height);

View File

@ -16,6 +16,7 @@
==================================================================== */
package org.apache.poi.xwpf.usermodel;
import org.apache.poi.common.usermodel.PictureType;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xwpf.XWPFTestDataSamples;
@ -100,7 +101,7 @@ class TestXWPFComment {
XWPFParagraph paragraph = comment.createParagraph();
XWPFRun r = paragraph.createRun();
r.addPicture(new ByteArrayInputStream(new byte[0]),
Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
PictureType.JPEG, "test.jpg", 21, 32);
assertEquals(1, comments.getAllPictures().size());
assertEquals(1, doc.getAllPackagePictures().size());

View File

@ -117,7 +117,7 @@ class TestXWPFPictureData {
// Add a picture to the first paragraph
header.getParagraphs().get(0).getRuns().get(0).addPicture(
new ByteArrayInputStream(new byte[]{1, 2, 3, 4}),
Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
PictureType.JPEG, "test.jpg", 2, 2);
// Check
verifyOneHeaderPicture(doc);

View File

@ -553,7 +553,7 @@ class TestXWPFRun {
assertEquals(0, doc.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
r.addPicture(new ByteArrayInputStream(new byte[0]), PictureType.JPEG, "test.jpg", 21, 32);
assertEquals(1, doc.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
@ -581,7 +581,7 @@ class TestXWPFRun {
assertEquals(0, hdr.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
r.addPicture(new ByteArrayInputStream(new byte[0]), PictureType.JPEG, "test.jpg", 21, 32);
assertEquals(1, hdr.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
@ -628,7 +628,7 @@ class TestXWPFRun {
try (XWPFDocument document = new XWPFDocument()) {
document.createParagraph().createRun().addPicture(
new ByteArrayInputStream(image), Document.PICTURE_TYPE_JPEG, "test.jpg", Units.toEMU(300), Units.toEMU(100));
new ByteArrayInputStream(image), PictureType.JPEG, "test.jpg", Units.toEMU(300), Units.toEMU(100));
try (XWPFDocument docBack = writeOutAndReadBack(document)) {
List<XWPFPicture> pictures = docBack.getParagraphArray(0).getRuns().get(0).getEmbeddedPictures();
@ -859,7 +859,7 @@ class TestXWPFRun {
assertEquals(0, hdr.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
r.addPicture(new ByteArrayInputStream(new byte[0]), PictureType.JPEG, "test.jpg", 21, 32);
assertEquals(1, hdr.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());