]> source.dussan.org Git - poi.git/commitdiff
[github-150] XWPFPicture: easy access to width and depth. Thanks to Thibaut Cuvelier...
authorPJ Fanning <fanningpj@apache.org>
Fri, 19 Jul 2019 21:05:15 +0000 (21:05 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 19 Jul 2019 21:05:15 +0000 (21:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863434 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java

index 9f8e3fc6fc6a1a99679cfc81d9ca9b252af5a293..a64eabecba81cb73199ecd2ca0848bd118f40dc0 100644 (file)
@@ -18,6 +18,7 @@ package org.apache.poi.xwpf.usermodel;
 
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.util.Units;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
 import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
 
@@ -77,6 +78,20 @@ public class XWPFPicture {
         }
         return null;
     }
+   
+    /**
+     * Returns the width of the picture (in points). 
+     */
+    public double getWidth() {
+        return Units.toPoints(ctPic.getSpPr().getXfrm().getExt().getCx());
+    }
+   
+    /**
+     * Returns the depth of the picture (in points). 
+     */
+    public double getDepth() {
+        return Units.toPoints(ctPic.getSpPr().getXfrm().getExt().getCy());
+    }
 
     public String getDescription() {
         return description;
index 49f03ef79fd1677b49fe4b817aa9165a7ccbefaf..27df95095e0c1f35ada7334455a4acb8c41d3e05 100644 (file)
@@ -520,7 +520,7 @@ public class TestXWPFRun {
         XWPFPicture pic = r.getEmbeddedPictures().get(0);
         CTPicture ctPic = pic.getCTPicture();
         CTBlipFillProperties ctBlipFill = ctPic.getBlipFill();
-        
+
         assertNotNull(ctBlipFill);
         
         CTBlip ctBlip = ctBlipFill.getBlip();
@@ -788,4 +788,25 @@ public class TestXWPFRun {
         document.close();
     }
 
+    @Test
+    public void testGetDepthWidth() throws IOException, InvalidFormatException {
+        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
+        XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
+        XWPFParagraph p = hdr.createParagraph();
+        XWPFRun r = p.createRun();
+
+        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);
+
+        assertEquals(1, hdr.getAllPictures().size());
+        assertEquals(1, r.getEmbeddedPictures().size());
+
+        XWPFPicture pic = r.getEmbeddedPictures().get(0);
+
+        assertEquals(pic.getWidth(), Units.toPoints(21), 0.0);
+        assertEquals(pic.getDepth(), Units.toPoints(32), 0.0);
+    }
+
 }