Search in sources :

Example 1 with Part

use of org.docx4j.openpackaging.parts.Part in project Java-Tutorial by gpcodervn.

the class ReportFromTemplate method main.

public static void main(String[] args) {
    final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
    String fileName = "";
    try {
        // Populate the Strings that will replace the template text
        Map<String, String> map = new HashMap<String, String>();
        map.put("Project", "BP Mount");
        map.put("Date", "21-Mar-2011");
        // C:\\test\\template1.docx is the template file
        WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("resources/Template1.docx"));
        Parts parts = template.getParts();
        HashMap<PartName, Part> partsMap = parts.getParts();
        PartName partName = null;
        Part part = null;
        Set<PartName> set = partsMap.keySet();
        for (Iterator<PartName> iterator = set.iterator(); iterator.hasNext(); ) {
            PartName name = (PartName) iterator.next();
            if (name.getName().equalsIgnoreCase("/word/media/image1.png")) {
                part = partsMap.get(name);
                partName = name;
            }
        }
        if (part != null && partName != null) {
            part = partsMap.get(partName);
            BinaryPart binaryPart = (BinaryPart) part;
            binaryPart.setBinaryData(fileToBytes(fileToReplace));
        }
        List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
        for (Object obj : texts) {
            Text text = (Text) ((JAXBElement) obj).getValue();
            String textValue = text.getValue();
            for (Object key : map.keySet()) {
                // textValue = textValue.replaceAll("\\$\\{" + key + "\\}", (String) map.get(key));
                textValue = textValue.trim().replace("${" + key + "}", (String) map.get(key));
            }
            text.setValue(textValue);
        }
        /*
			 * Add the other contents here
			 */
        template.save(new File("output/ReportFromTemplate.docx"));
        System.out.println("Done");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Errors");
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) HashMap(java.util.HashMap) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) Part(org.docx4j.openpackaging.parts.Part) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) File(java.io.File)

Example 2 with Part

use of org.docx4j.openpackaging.parts.Part in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method getComments.

public int getComments(WordprocessingMLPackage wordMLPackage) throws Exception {
    Parts parts = wordMLPackage.getParts();
    HashMap<PartName, Part> partMap = parts.getParts();
    CommentsPart commentPart = (CommentsPart) partMap.get(new CommentsPart().getPartName());
    Comments comments = commentPart.getContents();
    List<Comment> commentList = comments.getComment();
    for (Comment comment : commentList) {
        StringBuffer sb = new StringBuffer();
        sb.append(" ID: ").append(comment.getId());
        sb.append(" 作者:").append(comment.getAuthor());
        sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime());
        sb.append(" 内容:").append(comment.getContent());
        // sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString()));
        System.out.println(sb.toString());
    }
    return 0;
}
Also used : Comment(org.docx4j.wml.Comments.Comment) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Part(org.docx4j.openpackaging.parts.Part) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) Comments(org.docx4j.wml.Comments) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)

Example 3 with Part

use of org.docx4j.openpackaging.parts.Part in project com.revolsys.open by revolsys.

the class XlsxRecordReader method initDo.

@Override
protected void initDo() {
    super.initDo();
    try (InputStream in = this.resource.newBufferedInputStream()) {
        final SpreadsheetMLPackage spreadsheetPackage = (SpreadsheetMLPackage) OpcPackage.load(in);
        final DocPropsCustomPart customProperties = spreadsheetPackage.getDocPropsCustomPart();
        if (customProperties != null) {
            int srid = 0;
            try {
                srid = Integer.parseInt(customProperties.getProperty("srid").getLpwstr());
            } catch (final Throwable e) {
            }
            int axisCount = 2;
            try {
                axisCount = Integer.parseInt(customProperties.getProperty("axisCount").getLpwstr());
                if (axisCount > 4) {
                    axisCount = 2;
                }
            } catch (final Throwable e) {
            }
            double scaleXy = 0;
            try {
                scaleXy = Double.parseDouble(customProperties.getProperty("scaleXy").getLpwstr());
            } catch (final Throwable e) {
            }
            double scaleZ = 0;
            try {
                scaleZ = Double.parseDouble(customProperties.getProperty("scaleZ").getLpwstr());
            } catch (final Throwable e) {
            }
            final GeometryFactory geometryFactory = GeometryFactory.fixed(srid, axisCount, scaleXy, scaleXy, scaleZ);
            setGeometryFactory(geometryFactory);
        }
        WorksheetPart worksheetPart = null;
        for (final Part part : spreadsheetPackage.getParts().getParts().values()) {
            if (part instanceof WorksheetPart) {
                if (worksheetPart == null) {
                    worksheetPart = (WorksheetPart) part;
                }
            } else if (part instanceof SharedStrings) {
                final SharedStrings sharedStrings = (SharedStrings) part;
                final CTSst contents = sharedStrings.getContents();
                this.sharedStringList = contents.getSi();
            }
        }
        if (worksheetPart != null) {
            final Worksheet worksheet = worksheetPart.getContents();
            final SheetData sheetData = worksheet.getSheetData();
            this.rows = sheetData.getRow();
            final List<String> line = readNextRow();
            final String baseName = this.resource.getBaseName();
            newRecordDefinition(baseName, line);
        }
    } catch (final IOException | Docx4JException e) {
        Logs.error(this, "Unable to open " + this.resource, e);
    } catch (final NoSuchElementException e) {
    }
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Worksheet(org.xlsx4j.sml.Worksheet) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) CTSst(org.xlsx4j.sml.CTSst) SheetData(org.xlsx4j.sml.SheetData) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) Part(org.docx4j.openpackaging.parts.Part) SharedStrings(org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with Part

use of org.docx4j.openpackaging.parts.Part in project docx4j-template by vindell.

the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.

/**
 * @Description: 提取word图片
 */
public void saveDocxImg(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
        if (entry.getValue() instanceof BinaryPartAbstractImage) {
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
            // 图片minetype
            String imgContentType = binImg.getContentType();
            PartName pt = binImg.getPartName();
            String fileName = null;
            if (pt.getName().indexOf("word/media/") != -1) {
                fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
            }
            System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
            FileOutputStream fos = new FileOutputStream(savePath + fileName);
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
            fos.close();
        }
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) PartName(org.docx4j.openpackaging.parts.PartName) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) Part(org.docx4j.openpackaging.parts.Part) FileOutputStream(java.io.FileOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File)

Example 5 with Part

use of org.docx4j.openpackaging.parts.Part in project docx4j-template by vindell.

the class Docx4j_删除所有批注_S3_Test method removeAllComment.

// 这里2个路径可以一致
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));
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) ContentAccessor(org.docx4j.wml.ContentAccessor) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) Comment(org.docx4j.wml.Comments.Comment) Comments(org.docx4j.wml.Comments) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) TraversalUtil(org.docx4j.TraversalUtil) Part(org.docx4j.openpackaging.parts.Part) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) FileOutputStream(java.io.FileOutputStream)

Aggregations

Part (org.docx4j.openpackaging.parts.Part)6 PartName (org.docx4j.openpackaging.parts.PartName)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 Parts (org.docx4j.openpackaging.parts.Parts)3 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 BinaryPart (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart)2 CommentsPart (org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)2 Comments (org.docx4j.wml.Comments)2 Comment (org.docx4j.wml.Comments.Comment)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 TraversalUtil (org.docx4j.TraversalUtil)1 InvalidFormatException (org.docx4j.openpackaging.exceptions.InvalidFormatException)1