From: Nick Burch Date: Tue, 1 May 2012 09:46:15 +0000 (+0000) Subject: Patch from Josh Holthaus from bug #53165 - HWPF support for fetching the description... X-Git-Tag: 3.10-beta1~195 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a7796d06d848a3b1aaac941adc7baf2f1b6c9ff0;p=poi.git Patch from Josh Holthaus from bug #53165 - HWPF support for fetching the description (alt text) of a picture git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1332594 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 17c2490dfe..7b085461f6 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 53165 - HWPF support for fetching the description (alt text) of a picture 48528 - support negative arguments to the DATE() function 53092 - allow specifying of a TimeZone to DateUtil.getJavaDate(), for when it is known that a file comes from a different (known) timezone to the current machine 53043 - don't duplicate hyperlink relationships when saving XSSF file diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java index 58ad7aefa1..ff7af7cf21 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java @@ -27,11 +27,16 @@ import java.util.zip.InflaterInputStream; import org.apache.poi.ddf.EscherBSERecord; import org.apache.poi.ddf.EscherBlipRecord; +import org.apache.poi.ddf.EscherComplexProperty; +import org.apache.poi.ddf.EscherOptRecord; +import org.apache.poi.ddf.EscherProperties; +import org.apache.poi.ddf.EscherProperty; import org.apache.poi.ddf.EscherRecord; import org.apache.poi.hwpf.model.PICF; import org.apache.poi.hwpf.model.PICFAndOfficeArtData; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.util.StringUtil; /** * Represents embedded picture extracted from Word Document @@ -488,6 +493,28 @@ public final class Picture } return width; } + + /** + * returns the description stored in the alternative text + * + * @return pictue description + */ + public String getDescription() + { + for(EscherRecord escherRecord : _picfAndOfficeArtData.getShape().getChildRecords()){ + if(escherRecord instanceof EscherOptRecord){ + EscherOptRecord escherOptRecord = (EscherOptRecord) escherRecord; + for(EscherProperty property : escherOptRecord.getEscherProperties()){ + if(EscherProperties.GROUPSHAPE__DESCRIPTION == property.getPropertyNumber()){ + byte[] complexData = ((EscherComplexProperty)property).getComplexData(); + return StringUtil.getFromUnicodeLE(complexData,0,complexData.length/2-1); + } + } + } + } + + return null; + } /** * tries to suggest extension for picture's file by matching signatures of diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java index c2d4093fdf..cddaee7e43 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java @@ -333,4 +333,12 @@ public final class TestPictures extends TestCase { assertEquals(PictureType.PNG, p.suggestPictureType()); assertEquals("png", p.suggestFileExtension()); } + + public void testPictureWithAlternativeText() throws Exception { + HWPFDocument document = HWPFTestDataSamples.openSampleFile("Picture_Alternative_Text.doc"); + PicturesTable pictureTable = document.getPicturesTable(); + Picture picture = pictureTable.getAllPictures().get(0); + + assertEquals("This is the alternative text for the picture.", picture.getDescription()); + } } diff --git a/test-data/document/Picture_Alternative_Text.doc b/test-data/document/Picture_Alternative_Text.doc new file mode 100644 index 0000000000..55bf875d0d Binary files /dev/null and b/test-data/document/Picture_Alternative_Text.doc differ