poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Beardsley <markbrd...@tiscali.co.uk>
Subject Re: Zoom settings for XWPF?
Date Wed, 26 Aug 2015 14:39:38 GMT
Late I know - and sorry for that - but I wondered if you found out how to
change the zoom percentage of a document of not. This code will work even
though it is very far from perfect as I need to find a way to locate the
relationship ID from the name of the relationship.

Look at the bottom of the listing for the main() method which shows how to
run the code. Hope it helps, and it not too late.

import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFSettings;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.POIXMLDocumentPart;

/******************************************************************
  * An instance of this class can be used to open a Word OpenXML
  * document and change the zoom percentage. To obtain a reference
  * to the XWPFSettings object encapsulated within the XWPFDocument
  * when it was opened, the code simply iterates through the various
  * POIXMLDocumentParts that comprise the document 'looking' for
  * an instance of the XWPFSettings class. When it finds this
  * object, the code simply casts from POIXMLDocumentPart to
  * XWPFSettings and then makes the change to the zoom perecntage.
  * 
  * There is likely a better way to get at this particular object
  * and I will continue to look for it. My hope is that it should be
  * possible to locate the relationship ID from the name of the 
  * relationship but as yet I have not found it.
  *****************************************************************
  */
public class SetZoomPercentage {
  
  private XWPFDocument doc = null;
  
  /****************************************************************
    * Create an instance of the SetZoomPercentage class. In reality,
    * there is no need to create an explicit constructor for this 
    * class however, the message may be usefull for confirming 
    * object creation during testing.
    ***************************************************************
    */
  public SetZoomPercentage() {
    
    System.out.println("Create an instance of the SetZoomPercentage
class.");
    
  }
  
  /****************************************************************
    * Open the named document.
    * 
    * @param docName An instance of the String class that encapsulates
    * the name of and path to a valid WordML document.
    * 
    * @throws IOException if any problems are encountered opening the
    * document. For example, the file may simply not be found.
    ****************************************************************
    */
  public void openDocument(String docName) throws IOException {
    
    File inputFile = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    
    try {
      
      inputFile = new File(docName);
      fis = new FileInputStream(inputFile);
      bis = new BufferedInputStream(fis);
      this.doc = new XWPFDocument(bis);
      // Message just to confirm the document has been opened.
      System.out.println("Document " + docName + " opened.");
      
    }
    catch(IOException ioEx) {
      
      SetZoomPercentage.printException(ioEx);
      throw(ioEx);
      
    }
    finally {
      
      if(bis != null) {
        
        try {
          
          bis.close();
          
        }
        catch(IOException ioEx) {
          
          SetZoomPercentage.printException(ioEx);
          
        }
      }
    }
  }

  /***************************************************************
    * Save the currently open document using the name and location
    * specified.
    * 
    * @param docName An instance of the String class that
    * encapsulates the name of and path to the document file.
    * 
    * @throws IOException if any problems are encountered trying
    * to save the document file.
    **************************************************************
    */
  public void saveDocument(String docName) throws IOException {
    
    File outputFile = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    
    try {
      
      outputFile = new File(docName);
      fos = new FileOutputStream(outputFile);
      bos = new BufferedOutputStream(fos);
      this.doc.write(bos);
      System.out.println("Document " + docName + " saved.");
      
    }
    catch(IOException ioEx) {
      
      SetZoomPercentage.printException(ioEx);
      throw(ioEx);
      
    }
    finally {
      
      if(bos != null) {
        
        try {
          
          bos.flush();
          bos.close();
          
        }
        catch(IOException ioEx) {
          
          SetZoomPercentage.printException(ioEx);
          
        }
      }
    }
  }

  /******************************************************************
    * Called to set the zoom percentage for the document.
    * 
    * @param zoomPerecent A primitive long parameter whose value will
    * be used to set the zoom percentage of the document.
    *****************************************************************
    */
  public void setZoomPercentage(long zoomPercent) {
    
    POIXMLDocumentPart corePart = null;
    POIXMLDocumentPart docPart = null;
    XWPFSettings settings = null;
    
    List<POIXMLDocumentPart> partsList = null;
    Iterator<POIXMLDocumentPart> partsListIter = null;
    
    // Get at the POIXMLDocumentPart that refers to the currently
    // open Word document. From this, get a List of the documents
    // relations and an Iterator to step through those.
    corePart = this.doc.getPart();
    partsList = corePart.getRelations();
    partsListIter = partsList.iterator();
    
    // Step through the documents relations looking for the one
    // that references its settings. This loop is not ideal as
    // it will examine all of the relations even if the settings
    // are the first. It can be simply improved but will be left
    // for testing as it makes it easy to examine all of the
    // relations.
    while(partsListIter.hasNext()) {
      
      docPart = partsListIter.next();
      
      // Test the class of the document part and, if it references
      // the documents settings, cast it to the XWPFSettings type.
     
if(docPart.getClass().getName().equals("org.apache.poi.xwpf.usermodel.XWPFSettings"))
{
        
        settings = (XWPFSettings)docPart;
        
      }
    }

    // With the settings object in hand, display the existing zoom
percentage
    // and then change it.
    System.out.println("Zoom percentage is currently " + 
                       settings.getZoomPercent() + 
                       "%. Setting it to " + 
                       zoomPercent + 
                       "%.");
    settings.setZoomPercent(zoomPercent);
  }
  
  
  public static final void printException(Throwable thr) {
    System.out.println("Caught a " + thr.getClass().getName());
    System.out.println("Message " + thr.getMessage());
    System.out.println("Stacktrace.....");
    thr.printStackTrace();
  }
  
  public static void main(String[] args) {
    try {
      SetZoomPercentage szp = new SetZoomPercentage();
      szp.openDocument("/home/markb/Public/java/testdocs/zoomed.docx");
      szp.setZoomPercentage(200);
      szp.saveDocument("/home/markb/Public/java/testdocs/new zoomed.docx");
    }
    catch(IOException ioEx) {
      System.out.println("Caught an " + ioEx.getClass().getName());
    }
  }
}



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Zoom-settings-for-XWPF-tp5719896p5719940.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message