1. I have tried in the below way for getting the content. I need to know how
to replace the text in the content.

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.usermodel.ISDTContent;
import org.apache.poi.xwpf.usermodel.XWPFDefaultParagraphStyle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class XWPFSDTEg {

        static String SOURCE_FILE = "Test.docx";
        static String OUTPUT_FILE = "TestOP.docx";

        public static void main(String[] args) throws Exception {

                Resource resource = new ClassPathResource(SOURCE_FILE);
                XWPFDocument document = new 
XWPFDocument(resource.getInputStream());

                
                List<XWPFSDT> xwpfsdts = extractSDTsFromBody(document);
                for (XWPFSDT xwpfsdt : xwpfsdts) {
                        ISDTContent isdtContent = xwpfsdt.getContent();
                        System.out.println("aa"+isdtContent.getText());

                        System.out.println(xwpfsdt.getTitle().toString());
                        
                        String text = isdtContent.getText();
                        text = text.replaceAll("Contents", "Contents-Contents");

                        XWPFDocument opdoc = new XWPFDocument();
                        FileOutputStream out = new FileOutputStream(new 
File(OUTPUT_FILE));
                        XWPFParagraph paragraph = opdoc.createParagraph();
                        XWPFRun run = paragraph.createRun();
                        run.setText(text);
                        
                        opdoc.write(out);
                        out.close();
                        System.out.println("Completed");
                }

        }

        private static List<XWPFSDT> extractSDTsFromBody(XWPFDocument document)
throws XmlException {

                XWPFSDT sdt;
                XmlCursor xmlcursor = 
document.getDocument().getBody().newCursor();
                List<XWPFSDT> allsdts = new ArrayList<XWPFSDT>();
                while (xmlcursor.hasNextToken()) {
                        XmlCursor.TokenType tokentype = xmlcursor.toNextToken();
                        if (tokentype.isStart()) {
                                if (xmlcursor.getObject() instanceof CTSdtRun) {
                                        sdt = new XWPFSDT((CTSdtRun) 
xmlcursor.getObject(), document);
                                        allsdts.add(sdt);
                                } else if (xmlcursor.getObject() instanceof 
CTSdtBlock) {
                                        sdt = new XWPFSDT((CTSdtBlock) 
xmlcursor.getObject(), document);
                                        allsdts.add(sdt);
                                }
                        }
                }
                return allsdts;
        }

}

2. How to get the title in the document and how to replace it?



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

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

Reply via email to