To append new page,
"body->SectPr"Tag ( CTBody.getSectPr() ) must be moved to
"body->p->pPr->SectPr". ( CTBody.addNewP().addNewPPr().addNewSectPr() )

I attach sample.(but this sample merge two body to one body)

Yoshitake

(2010/10/28 20:42), gummadi krishna wrote:
As an alternative what i tired is to get the content of the other word document 
as a CTBody object and append it to the new word document using
addNewBody() method of CTdocument class . As a result when i open the word i 
see the new contant appended to the old correctly but is getting appended at 
the end of same page. How can i make sure that new body appended can always be 
seen on a new page when i open a word ? Currently the new body is getting 
apended at athe end of the same page .

cheers

--- On Thu, 28/10/10, Nick Burch<[email protected]>  wrote:

From: Nick Burch<[email protected]>
Subject: Re: Embeed word docs
To: "POI Users List"<[email protected]>
Date: Thursday, 28 October, 2010, 2:39 PM
On Thu, 28 Oct 2010, gummadi krishna
wrote:
Is there a possibility to embeed two word documents in
to a separate word document using poi ?

It might be possible, but it'd be a very large amount of
work...

What wouldn't be too bad is if you created a template with
two simple word documents in. You could then use POIFS to
copy in the details of your real word docs over the top of
the already setup POIFS subdirectories for the embedded
files. You might also need to zap the cached image rendering
of the docs so it gets re-created, deleting the entry from
the pictures table might do that but I've not tried it

Nick

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




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



package epeapteroenta.ooxml.wp.test;


import java.io.File;
import java.io.FileOutputStream;


import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.util.PackageHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;

/**
 * @author Yoshitake Oka
 */
public class BodyTest {

    public static final String EXPRESSION_SERCH_SECTPR =
        "declare namespace 
w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'$this/w:sectPr";

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {

                String orgMainFilePath = 
"C:\\develop\\POI\\data\\test1030_0001\\aaa.docx";
                String orgAddFilePath = 
"C:\\develop\\POI\\data\\test1030_0001\\bbb.docx";
                String mergeFilePath = 
"C:\\develop\\POI\\data\\test1030_0001\\merge.docx";
                String outFilePath = 
"C:\\develop\\POI\\data\\test1030_0001\\out.docx";

                File f = new File(mergeFilePath);
                f.delete();


        OPCPackage mainPkg = OPCPackage.open( orgMainFilePath, 
PackageAccess.READ);
        OPCPackage addPkg = OPCPackage.open( orgAddFilePath, 
PackageAccess.READ);


        File mergeFile = new File(mergeFilePath);
        OPCPackage mergePkg = PackageHelper.clone(mainPkg,mergeFile);


        XWPFDocument mergeDoc = new XWPFDocument(mergePkg);
        mergeDoc.getDocument().unsetBody();
        CTBody mergeBody = mergeDoc.getDocument().addNewBody();


        XWPFDocument mainDoc = new XWPFDocument(mainPkg);
        CTBody mainBody = mainDoc.getDocument().getBody();

        // Change "body>sectPr" To "body>p>pPr>sectPr"
        String strMainSectPr = pickupSectPr(mainBody);
        CTSectPr mainSectPr = CTSectPr.Factory.parse(strMainSectPr);
        mergeBody.set(mainBody);
        mergeBody.addNewP().addNewPPr().addNewSectPr().set(mainSectPr);


        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();


        XWPFDocument addDoc = new XWPFDocument(addPkg);
        CTBody addBody = addDoc.getDocument().getBody();
        String strAddBody = addBody.xmlText(optionsOuter);

        addNewBodyAsBody(mergeBody, strAddBody);

        savePackage(outFilePath,mergeDoc);
        }


    private static String pickupSectPr(CTBody body) throws Exception{
        XmlCursor cursor =body.newCursor();

        CTSectPr sectPr = null;
        cursor.selectPath( EXPRESSION_SERCH_SECTPR );
        cursor.toNextSelection();
        Object o = cursor.getObject();
        if( o instanceof CTSectPr){
            sectPr = (CTSectPr)o;
        } else {
            throw new Exception("doc format already broken!!");
        }

        String strSectPr = sectPr.xmlText();
        cursor.removeXml();

        return strSectPr;
    }

    private static void addNewBodyAsBody(CTBody mainBody, String strAddBody) 
throws Exception {

        String strMainBody = mainBody.xmlText();

        String prefix = strMainBody.substring(0,strMainBody.indexOf(">")+1);
        String mainPart = 
strMainBody.substring(strMainBody.indexOf(">")+1,strMainBody.lastIndexOf("<"));
        String sufix = strMainBody.substring( strMainBody.lastIndexOf("<") );
        String addPart = 
strAddBody.substring(strAddBody.indexOf(">")+1,strAddBody.lastIndexOf("<"));

        CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);

        mainBody.set(makeBody);

    }

    public static void savePackage( String path , XWPFDocument doc) throws 
Exception{

        FileOutputStream out  = new FileOutputStream( path );
        doc.write(out);
    }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to