From 4fbad5c3f725ced7a1cb7bab7c505c0317653cd6 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 19 Jul 2019 21:05:15 +0000 Subject: [PATCH] [github-150] XWPFPicture: easy access to width and depth. Thanks to Thibaut Cuvelier. This closes #150 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863434 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFPicture.java | 15 ++++++++++++ .../poi/xwpf/usermodel/TestXWPFRun.java | 23 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java index 9f8e3fc6fc..a64eabecba 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java @@ -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; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index 49f03ef79f..27df95095e 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -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); + } + } -- 2.39.5