Java Code Examples for org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart#getContents()

The following examples show how to use org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart#getContents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WordprocessingMLPackageExtractor.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void extract(WordprocessingMLPackage wmlPackage,Writer out) throws Exception {
	MainDocumentPart documentPart = wmlPackage.getMainDocumentPart();		
	org.docx4j.wml.Document wmlDocumentEl = documentPart.getContents();
	TextUtils.extractText(wmlDocumentEl, out);
	//out.flush();
	//out.close();
}
 
Example 2
Source File: Docx4j_删除所有批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void removeAllComment(String filePath, String savePath)  
        throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage  
            .load(new java.io.File(filePath));  
    //清空comments.xml内容   
    Parts parts = wordMLPackage.getParts();  
    HashMap<PartName, Part> partMap = parts.getParts();  
    CommentsPart commentPart = (CommentsPart) partMap.get(new PartName(  
            "/word/comments.xml"));  
    Comments comments = commentPart.getContents();  
    List<Comment> commentList = comments.getComment();  
    for (int i = 0, len = commentList.size(); i < len; i++) {  
        commentList.remove(0);  
    }  
      
    //清空document.xml文件中批注  
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart  
            .getContents();  
    Body body = wmlDocumentEl.getBody();  
    CommentFinder cf = new CommentFinder();  
    new TraversalUtil(body, cf);  
    for (Child commentElement : cf.commentElements) {  
        System.out.println(commentElement.getClass().getName());  
        Object parent = commentElement.getParent();  
        List<Object> theList = ((ContentAccessor) parent).getContent();  
        boolean removeResult = remove(theList, commentElement);  
        System.out.println(removeResult);  
    }  
    wordMLPackage.save(new FileOutputStream(savePath));  
}
 
Example 3
Source File: DocxProducer.java    From OfficeProducer with Apache License 2.0 4 votes vote down vote up
/**
 * 替换书签为图片
 *
 * @param wordMLPackage
 * @param documentPart
 * @param imageParameters
 * @throws Exception
 */
private static void replaceBookMarkWithImage(WordprocessingMLPackage wordMLPackage,
                                             MainDocumentPart documentPart,
                                             Map<String, String> imageParameters)
        throws Exception {
    Document wmlDoc = documentPart.getContents();
    Body body = wmlDoc.getBody();
    // 提取正文中所有段落
    List<Object> paragraphs = body.getContent();
    // 提取书签并创建书签的游标
    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(paragraphs, rt);

    // 遍历书签
    for (CTBookmark bm : rt.getStarts()) {
        String bookmarkName = bm.getName();
        String imagePath = imageParameters.get(bookmarkName);
        if (imagePath != null) {
            File imageFile = new File(imagePath);
            InputStream imageStream = new FileInputStream(imageFile);
            // 读入图片并转化为字节数组,因为docx4j只能字节数组的方式插入图片
            byte[] bytes = IOUtils.toByteArray(imageStream);
            // 创建一个行内图片
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
            // createImageInline函数的前四个参数我都没有找到具体啥意思
            // 最后一个是限制图片的宽度,缩放的依据
            Inline inline = imagePart.createImageInline(null, null, 0, 1, false, 800);
            // 获取该书签的父级段落
            P p = (P) (bm.getParent());
            ObjectFactory factory = new ObjectFactory();
            // 新建一个Run
            R run = factory.createR();
            // drawing 画布
            Drawing drawing = factory.createDrawing();
            drawing.getAnchorOrInline()
                    .add(inline);
            run.getContent()
                    .add(drawing);
            p.getContent()
                    .add(run);
        }
    }
}