Class BinaryPartAbstractImage
java.lang.Object
org.docx4j.openpackaging.Base
org.docx4j.openpackaging.parts.Part
org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart
org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage
- Direct Known Subclasses:
ImageBmpPart,ImageBrokenPart,ImageEpsPart,ImageGifPart,ImageJpegPart,ImagePngPart,ImageTiffPart,MetafilePart
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsFields inherited from class org.docx4j.openpackaging.Base
contentType, partName, relationships -
Constructor Summary
ConstructorsConstructorDescriptionBinaryPartAbstractImage(ExternalTarget externalTarget) BinaryPartAbstractImage(PartName partName) -
Method Summary
Modifier and TypeMethodDescriptionstatic voidconvertToPNG(InputStream is, OutputStream os, int density) Convert image formats which are not supported by Word (eg EPS, PDF), into ones which are.static voidcopy2(InputStream is, OutputStream os) createImageInline(String filenameHint, String altText, long id1, int id2) Deprecated.createImageInline(String filenameHint, String altText, long id1, int id2, boolean link) Create a <wp:inline> element suitable for this image, which can be linked or embedded in w:p/w:r/w:drawing.createImageInline(String filenameHint, String altText, long id1, int id2, boolean link, int maxWidth) createImageInline(String filenameHint, String altText, long id1, int id2, long cx) Deprecated.createImageInline(String filenameHint, String altText, long id1, int id2, long cx, boolean link) Create a <wp:inline> element suitable for this image, which can be _embedded_ in w:p/w:r/w:drawing.createImageInline(String filenameHint, String altText, long id1, int id2, long cx, long cy, boolean link) Create a <wp:inline> element suitable for this image, which can be linked or embedded in w:p/w:r/w:drawing, specifying height and width.static StringcreateImageName(Base sourcePart, String proposedRelId, String ext) Deprecated.static StringcreateImageName(OpcPackage opcPackage, Base sourcePart, String proposedRelId, String ext) static BinaryPartAbstractImagecreateImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes) Create an image part from the provided byte array, attach it to the source part (eg the main document part, a header part etc), and return it.static BinaryPartAbstractImagecreateImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes, String mime) Create an image part from the provided byte array, attach it to the source part (eg the main document part, a header part etc), and return it.static BinaryPartAbstractImagecreateImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes, String mime, String ext) Deprecated.static BinaryPartAbstractImagecreateImagePart(OpcPackage opcPackage, Part sourcePart, File imageFile) Create an image part from the provided filePath image, attach it to the source part (eg the main document part, a header part etc), and return it.static BinaryPartAbstractImagecreateImagePart(WordprocessingMLPackage wordMLPackage, byte[] bytes) Create an image part from the provided byte array, attach it to the docx main document part, and return it.static BinaryPartAbstractImagecreateImagePart(WordprocessingMLPackage wordMLPackage, File imageFile) Possibility to put directly an image filePath instead of giving an image byte arraystatic BinaryPartAbstractImagecreateLinkedImagePart(OpcPackage opcPackage, Part sourcePart, URL url) Create a linked image part, and attach it as a rel of the specified source part (eg a header part).static BinaryPartAbstractImagecreateLinkedImagePart(WordprocessingMLPackage wordMLPackage, URL fileurl) Create a linked image part, and attach it as a rel of the docx main document partstatic voiddisplayImageInfo(org.apache.xmlgraphics.image.loader.ImageInfo info) static byte[]getImage(WordprocessingMLPackage wmlPkg, Graphic graphic) Convenience method, given a Graphic in a document, to get the byte[] representing the associated image The method assumes your image is in the main document part (as opposed to a header/footer etc)org.apache.xmlgraphics.image.loader.ImageInfoDeprecated.static org.apache.xmlgraphics.image.loader.ImageInfogetImageInfo(URL url) protected static org.apache.xmlgraphics.image.loader.ImageManagergetRels()static voidstatic voidsetDensity(int density) Set the resolution at which a PDF or EPS is converted to PNG.voidsetImageInfo(org.apache.xmlgraphics.image.loader.ImageInfo imageInfo) Deprecated.Methods inherited from class org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart
getBuffer, getBytes, getExternalTarget, isContentEqual, isLoaded, setBinaryData, setBinaryData, setBinaryData, writeDataToOutputStreamMethods inherited from class org.docx4j.openpackaging.parts.Part
getContentLengthAsLoaded, getOwningRelationshipPart, getPackage, getRelationshipType, getSourceRelationship, getSourceRelationships, getVersion, remove, rename, setContentLengthAsLoaded, setOwningRelationshipPart, setPackage, setPartShortcut, setRelationshipType, setSourceRelationship, setVersionMethods inherited from class org.docx4j.openpackaging.Base
addTargetPart, addTargetPart, addTargetPart, addTargetPart, getContentType, getPartName, getRelationshipsPart, getRelationshipsPart, getUserData, reset, setContentType, setPartName, setRelationships, setUserData
-
Field Details
-
log
protected static org.slf4j.Logger log
-
-
Constructor Details
-
BinaryPartAbstractImage
- Throws:
InvalidFormatException
-
BinaryPartAbstractImage
-
-
Method Details
-
getImageInfo
Deprecated. -
setImageInfo
Deprecated. -
getRels
-
getRelLast
-
setDensity
public static void setDensity(int density) Set the resolution at which a PDF or EPS is converted to PNG. For best quality, you should set this to match the target output device. Higher densities (eg 600) give better quality, at the expense of conversion time (and file size).- Parameters:
density-
-
getImageManager
protected static org.apache.xmlgraphics.image.loader.ImageManager getImageManager() -
createImagePart
public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, byte[] bytes) throws Exception Create an image part from the provided byte array, attach it to the docx main document part, and return it.- Parameters:
wordMLPackage-sourcePart-bytes-- Returns:
- Throws:
Exception
-
createImagePart
public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, File imageFile) throws Exception Possibility to put directly an image filePath instead of giving an image byte array- Parameters:
wordMLPackage-imageFile-- Throws:
Exception
-
createImageName
Deprecated.This method assumes your package is a docx (not a pptx or xlsx).- Parameters:
sourcePart-proposedRelId-ext-- Returns:
-
createImageName
public static String createImageName(OpcPackage opcPackage, Base sourcePart, String proposedRelId, String ext) - Parameters:
opcPackage-sourcePart-proposedRelId-ext- extension eg png- Returns:
-
createImagePart
public static BinaryPartAbstractImage createImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes) throws Exception Create an image part from the provided byte array, attach it to the source part (eg the main document part, a header part etc), and return it. Works for both docx and pptx. Note: this method creates a temp file (and attempts to delete it). That's because it uses org.apache.xmlgraphics- Parameters:
opcPackage-sourcePart-bytes-- Returns:
- Throws:
Exception
-
createImagePart
@Deprecated public static BinaryPartAbstractImage createImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes, String mime, String ext) throws Exception Deprecated.Create an image part from the provided byte array, attach it to the source part (eg the main document part, a header part etc), and return it. Works for both docx and pptx. Knowing the MIME type allows you to avoid ImageInfo, but you'll probably also need to know the image dimensions- Parameters:
opcPackage-sourcePart-bytes-mime- MIME type eg image/pngext- filename extension eg png- Returns:
- Throws:
Exception
-
createImagePart
public static BinaryPartAbstractImage createImagePart(OpcPackage opcPackage, Part sourcePart, byte[] bytes, String mime) throws Exception Create an image part from the provided byte array, attach it to the source part (eg the main document part, a header part etc), and return it. Works for both docx and pptx. Knowing the MIME type allows you to avoid ImageInfo, but you'll probably also need to know the image dimensions- Parameters:
opcPackage-sourcePart-bytes-mime- MIME type eg image/png- Returns:
- Throws:
Exception
-
createImagePart
public static BinaryPartAbstractImage createImagePart(OpcPackage opcPackage, Part sourcePart, File imageFile) throws Exception Create an image part from the provided filePath image, attach it to the source part (eg the main document part, a header part etc), and return it. Works for both docx and pptx.- Parameters:
opcPackage-sourcePart-filePath-- Returns:
- Throws:
Exception
-
createLinkedImagePart
public static BinaryPartAbstractImage createLinkedImagePart(WordprocessingMLPackage wordMLPackage, URL fileurl) throws Exception Create a linked image part, and attach it as a rel of the docx main document part- Parameters:
wordMLPackage-fileurl-- Returns:
- Throws:
Exception
-
createLinkedImagePart
public static BinaryPartAbstractImage createLinkedImagePart(OpcPackage opcPackage, Part sourcePart, URL url) throws Exception Create a linked image part, and attach it as a rel of the specified source part (eg a header part). The current behaviour is that the part is added to the package, but since the target mode of the rel is external, the part is redundant.- Parameters:
wordMLPackage-sourcePart-url-- Returns:
- Throws:
Exception
-
createImageInline
@Deprecated public Inline createImageInline(String filenameHint, String altText, long id1, int id2) throws Exception Deprecated.Create a <wp:inline> element suitable for this image, which can be _embedded_ in w:p/w:r/w:drawing. If the image is wider than the page, it will be scaled automatically. To avoid the deprecated warning, use the same method, but with an additional argument of false appended.- Parameters:
filenameHint- Any text, for example the original filenamealtText- Like HTML's alt textid1- An id unique in the documentid2- Another id unique in the document None of these things seem to be exposed in Word 2007's user interface, but Word won't open the document if any of the attributes these go in (except @ desc) aren't present!- Throws:
Exception
-
createImageInline
public Inline createImageInline(String filenameHint, String altText, long id1, int id2, boolean link) throws Exception Create a <wp:inline> element suitable for this image, which can be linked or embedded in w:p/w:r/w:drawing. If the image is wider than the page, it will be scaled automatically.- Parameters:
filenameHint- Any text, for example the original filenamealtText- Like HTML's alt textid1- An id unique in the documentid2- Another id unique in the documentlink- true if this is to be linked not embedded None of these things seem to be exposed in Word 2007's user interface, but Word won't open the document if any of the attributes these go in (except @ desc) aren't present!- Throws:
Exception
-
createImageInline
public Inline createImageInline(String filenameHint, String altText, long id1, int id2, boolean link, int maxWidth) throws Exception - Parameters:
filenameHint-altText-id1-id2-link-maxWidth-- Returns:
- Throws:
Exception- Since:
- 3.3.0
-
createImageInline
@Deprecated public Inline createImageInline(String filenameHint, String altText, long id1, int id2, long cx) throws Exception Deprecated.Create a <wp:inline> element suitable for this image, which can be _embedded_ in w:p/w:r/w:drawing. To avoid the deprecated warning, use the same method, but with an additional argument of false appended.- Parameters:
filenameHint- Any text, for example the original filenamealtText- Like HTML's alt textid1- An id unique in the documentid2- Another id unique in the documentcx- Image width in twip None of these things seem to be exposed in Word 2007's user interface, but Word won't open the document if any of the attributes these go in (except @ desc) aren't present!- Throws:
Exception
-
createImageInline
public Inline createImageInline(String filenameHint, String altText, long id1, int id2, long cx, boolean link) throws Exception Create a <wp:inline> element suitable for this image, which can be _embedded_ in w:p/w:r/w:drawing.- Parameters:
filenameHint- Any text, for example the original filenamealtText- Like HTML's alt textid1- An id unique in the documentid2- Another id unique in the documentcx- Image width in twiplink- true if this is to be linked not embedded None of these things seem to be exposed in Word 2007's user interface, but Word won't open the document if any of the attributes these go in (except @ desc) aren't present!- Throws:
Exception
-
createImageInline
public Inline createImageInline(String filenameHint, String altText, long id1, int id2, long cx, long cy, boolean link) throws Exception Create a <wp:inline> element suitable for this image, which can be linked or embedded in w:p/w:r/w:drawing, specifying height and width. Note that you'd ordinarily use one of the methods which don't require you to specify height (cy).- Parameters:
filenameHint- Any text, for example the original filenamealtText- Like HTML's alt textid1- An id unique in the documentid2- Another id unique in the document None of these things seem to be exposed in Word 2007's user interface, but Word won't open the document if any of the attributes these go in (except @ desc) aren't present!cx- Image width in EMUcy- Image height in EMUlink- true if this is to be linked not embedded- Throws:
Exception
-
getImageInfo
- Throws:
Exception
-
main
- Throws:
Exception
-
displayImageInfo
public static void displayImageInfo(org.apache.xmlgraphics.image.loader.ImageInfo info) -
getImage
Convenience method, given a Graphic in a document, to get the byte[] representing the associated image The method assumes your image is in the main document part (as opposed to a header/footer etc)- Parameters:
wmlPkg-graphic-- Returns:
-
convertToPNG
public static void convertToPNG(InputStream is, OutputStream os, int density) throws IOException, InterruptedException Convert image formats which are not supported by Word (eg EPS, PDF), into ones which are. This requires ImageMagick to be on your system's path (renamed to imconvert); for EPS and PDF images, Ghostscript is also required.- Parameters:
is-os-density- PixelsPerInch- Throws:
IOExceptionInterruptedException
-
copy2
- Throws:
IOException
-