]> source.dussan.org Git - poi.git/commitdiff
Fix bug #51188 - Support for getting and setting XPWF zoom settings
authorNick Burch <nick@apache.org>
Fri, 27 May 2011 14:00:44 +0000 (14:00 +0000)
committerNick Burch <nick@apache.org>
Fri, 27 May 2011 14:00:44 +0000 (14:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1128312 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java

index 74812dc6eb94e7f6175fc2549bbda8b54758ffad..39172140414389680b925fa995b7beee6f0ee9a1 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta3" date="2011-??-??">
+           <action dev="poi-developers" type="add">51188 - Support for getting and setting XPWF zoom settings</action>
            <action dev="poi-developers" type="add">51134 - Support for adding Numbering and Styles to a XWPF document that doesn't already have them</action>
            <action dev="poi-developers" type="fix">51273 - Formula Value Cache fix for repeated evaluations</action>
            <action dev="poi-developers" type="add">51171 - Improved performance of SharedValueManager </action>
index f138a6dfc4f212476d6fa1daa7b4f97f8cdcbd56..7fed7b3e9084bcb33b6ced1b57df965abfa8ccf8 100644 (file)
@@ -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
index e34d6c145c590cdc6aa3da4ed616a6d84da511e3..09ec4cc6fb090b448aff492cdebfed293a107eb3 100644 (file)
@@ -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.<br/>
+     * In the zoom tag inside settings.xml file <br/>
+     * it sets the value of zoom
+     * <br/>
+     * sample snippet from settings.xml 
+     * <pre>
+     *    &lt;w:zoom w:percent="50" /&gt; 
+     * <pre>
+     * @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.<br/>
+     * In the zoom tag inside settings.xml file <br/>
+     * it sets the value of zoom
+     * <br/>
+     * sample snippet from settings.xml 
+     * <pre>
+     *    &lt;w:zoom w:percent="50" /&gt; 
+     * <pre>
+     * @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 <br/>
index b3f1ce34ad1eb0505c571ed7646f0bb7a7ed3a98..7b6af4ee941569f6574ddb2edf8d6208e96dbd5a 100644 (file)
@@ -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<PackagePart> gifParts = doc.getPackage().getPartsByContentType(XWPFRelation.IMAGE_GIF.getContentType());
            assertEquals("Expected exactly one GIF part in package.",1,gifParts.size());