OK. I can adjust the margins size using this code;

import java.io.*;
import java.util.List;
import java.util.Iterator;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.SectionProperties;
import org.apache.poi.hwpf.model.SectionTable;
import org.apache.poi.hwpf.model.SEPX;

/**
 *
 * @author Mark Beardsley
 */
public class DOCTest {
    
    public DOCTest(String filename, String resultFilename) throws
IOException {
        File file = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        HWPFDocument document = null;
        SectionTable sectTable = null;
        SectionProperties sectProps = null;
        Section section = null;
        List<SEPX> sectList = null;
        Iterator<SEPX> sectIter = null;
        try {
            
            file = new File(filename);
            fis = new FileInputStream(file);
            document = new HWPFDocument(fis);
            fis.close();
            fis = null;
            
            sectTable = document.getSectionTable();
            sectList = sectTable.getSections();
            sectIter = sectList.iterator();
            while(sectIter.hasNext()) {
                SEPX sectSEPX = sectIter.next();
                sectProps = sectSEPX.getSectionProperties();
                sectProps.setDxaLeft(25);
            }
            
            file = new File(resultFilename);
            fos = new FileOutputStream(file);
            document.write(fos);
        }
        finally {
            if(fis != null) {
                try {
                    fis.close();
                    fis = null;
                }
                catch(IOException ioEx) {
                    
                }
            }
            if(fos != null) {
                try {
                    fos.close();
                    fos = null;
                }
                catch(IOException ioEx) {
                    
                }
            }
        }
        
    }
    
    public static void main(String[] args) {
        try {
            new DOCTest("C:/temp/Test.doc", "C:/temp/Updated Test.doc");
        }
        catch(Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
}

but it still leaves lots of question unanswered.

One of these, I will leave for you to answer - which methods do you need to
call on the SectionDescriptior to set the other (right, top, bottom and
gutter) margins. Another, you can simply expriment with to see if you can
find a standard - what values should you pass to the setXXXX methods to get
the desired margin widths. It might be worthwhile looking at the source of
the SectionPropeties class to find out how it converts the values it reads
into those returned when you ask the section for the sizes of the margins.
The question that most concerns me is how do you know, in a multi section
document, which section to address when you are changing the margins? Of
course, if your document contains a single section then this question is
moot.

Yours

Mark B

PS. All I did to test the code was use Word to create a test document, in
binary (.doc) format and then used that as the input to the test.

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/How-to-change-margins-of-a-word-document-tp5710067p5710109.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to