Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
if(document.getBodyElements().get(0) instanceof XWPFSDT) {
document.removeBodyElement(0);
When debugging
document
the
XWPFSDT
element is correctly removed but on output cover page is still here.
Is there some way to update/refresh document xml, even if changes are happens from low level, how can we refresh docs to keep it uptodate
Until
apache poi
version
3.17
, the
XWPFDocument.removeBodyElement
removes only
BodyElementType.TABLE
or
BodyElementType.PARAGRAPH
properly. It lacks
CTBody.removeSdt
.
So we must do the low level stuff our self:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordRemoveCoverPage {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("WordDocumentWithCoverPage.docx"));
if(document.getBodyElements().get(0) instanceof XWPFSDT) {
System.out.println(document.removeBodyElement(0)); // true == success, but low level <w:sdt> is not removed from the XML
document.getDocument().getBody().removeSdt(0);
document.write(new FileOutputStream("WordDocumentWithoutCoverPage.docx"));
document.close();
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.