diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 74812dc6eb..3917214041 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51188 - Support for getting and setting XPWF zoom settings 51134 - Support for adding Numbering and Styles to a XWPF document that doesn't already have them 51273 - Formula Value Cache fix for repeated evaluations 51171 - Improved performance of SharedValueManager diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index f138a6dfc4..7fed7b3e90 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -960,6 +960,20 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { public void removeProtectionEnforcement() { settings.removeEnforcement(); } + + /** + * Return the zoom level, as a percentage + */ + public long getZoomPercent() { + return settings.getZoomPercent(); + } + + /** + * Sets the zoom level, as a percentage + */ + public void setZoomPercent(long zoomPercent) { + settings.setZoomPercent(zoomPercent); + } /** * inserts an existing XWPFTable to the arrays bodyElements and tables diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java index e34d6c145c..09ec4cc6fb 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java @@ -19,6 +19,7 @@ package org.apache.poi.xwpf.usermodel; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.math.BigInteger; import java.util.HashMap; import java.util.Map; @@ -30,6 +31,7 @@ import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocProtect; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTZoom; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument; @@ -48,6 +50,46 @@ public class XWPFSettings extends POIXMLDocumentPart { ctSettings = CTSettings.Factory.newInstance(); } + /** + * Set zoom.
+ * In the zoom tag inside settings.xml file
+ * it sets the value of zoom + *
+ * sample snippet from settings.xml + *
+     *    <w:zoom w:percent="50" /> 
+     * 
+     * @return percentage as an integer of zoom level
+     */
+    public long getZoomPercent() {
+       CTZoom zoom; 
+       if (!ctSettings.isSetZoom()) {
+          zoom = ctSettings.addNewZoom();
+       } else {
+          zoom = ctSettings.getZoom();
+       }
+
+       return zoom.getPercent().longValue();
+    }
+
+    /**
+     * Set zoom.
+ * In the zoom tag inside settings.xml file
+ * it sets the value of zoom + *
+ * sample snippet from settings.xml + *
+     *    <w:zoom w:percent="50" /> 
+     * 
+     * @return percentage as an integer of zoom level
+     */
+    public void setZoomPercent(long zoomPercent) {
+       if (! ctSettings.isSetZoom()) {
+          ctSettings.addNewZoom();
+       }
+       CTZoom zoom = ctSettings.getZoom();
+       zoom.setPercent(BigInteger.valueOf(zoomPercent));
+    }
 
     /**
      * Verifies the documentProtection tag inside settings.xml file 
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java index b3f1ce34ad..7b6af4ee94 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java @@ -200,8 +200,30 @@ public final class TestXWPFDocument extends TestCase { assertEquals(p3, doc.getParagraphs().get(0)); } - public void testGIFSupport() throws Exception - { + public void testSettings() throws Exception { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx"); + assertEquals(120, doc.getZoomPercent()); + assertEquals(false, doc.isEnforcedCommentsProtection()); + assertEquals(false, doc.isEnforcedFillingFormsProtection()); + assertEquals(false, doc.isEnforcedReadonlyProtection()); + assertEquals(false, doc.isEnforcedTrackedChangesProtection()); + + doc.setZoomPercent(124); + + // Only one enforcement allowed, last one wins! + doc.enforceFillingFormsProtection(); + doc.enforceReadonlyProtection(); + + doc = XWPFTestDataSamples.writeOutAndReadBack(doc); + + assertEquals(124, doc.getZoomPercent()); + assertEquals(false, doc.isEnforcedCommentsProtection()); + assertEquals(false, doc.isEnforcedFillingFormsProtection()); + assertEquals(true, doc.isEnforcedReadonlyProtection()); + assertEquals(false, doc.isEnforcedTrackedChangesProtection()); + } + + public void testGIFSupport() throws Exception { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx"); ArrayList gifParts = doc.getPackage().getPartsByContentType(XWPFRelation.IMAGE_GIF.getContentType()); assertEquals("Expected exactly one GIF part in package.",1,gifParts.size());