Example usage for org.apache.poi.hwpf HWPFDocument getDocProperties

List of usage examples for org.apache.poi.hwpf HWPFDocument getDocProperties

Introduction

In this page you can find the example usage for org.apache.poi.hwpf HWPFDocument getDocProperties.

Prototype

public DocumentProperties getDocProperties() 

Source Link

Usage

From source file:org.docx4j.convert.in.Doc.java

License:Apache License

/**
 * This method is private, since the fact that conversion is (currently)
 * performed using POI's HWPF should be encapsulated.
 * /*from  ww w.  j  av a 2s.  c  om*/
 * @param doc
 * @param wordMLPackage
 * @return success or failure
 */
private static void convert(HWPFDocument doc, WordprocessingMLPackage wordMLPackage) throws Exception {

    // Convert styles
    org.apache.poi.hwpf.model.StyleSheet stylesheet = doc.getStyleSheet();
    // TODO - higher priority
    // At present, a default set of styles are defined in the output
    // document.

    // Convert lists
    org.apache.poi.hwpf.model.ListTables listTables = doc.getListTables();
    // TODO

    // Convert document properties
    org.apache.poi.hwpf.model.DocumentProperties docProps = doc.getDocProperties();
    // TODO

    // Convert main document part

    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();

    Range r = doc.getRange();

    for (int x = 0; x < r.numSections(); x++) {
        Section s = r.getSection(x);

        // TODO - convert section

        for (int y = 0; y < s.numParagraphs(); y++) {
            Paragraph p = s.getParagraph(y);

            if (p.isInTable()) {
                Table t = s.getTable(p);
                int cl = numCol(t);

                log.info("Found " + t.numRows() + "x" + cl + " table - TODO - convert");

                handleTable(wordMLPackage, doc, t, stylesheet, documentPart, factory);

                // addTODO(factory, wmlP, "[TABLE " + + t.numRows() + "x" +
                // cl
                // + " - can't convert tables yet]");

                y += t.numParagraphs() - 1;
            }

            else {
                org.docx4j.wml.P paraToAdd = handleP(wordMLPackage, doc, p, stylesheet, documentPart, factory);

                documentPart.addObject(paraToAdd);
            }

        }
    }

}

From source file:rocky.sizecounter.SizeCounterUtil.java

License:Apache License

/**
 * Count Word's number of page from input directory.
 * /*w  w  w .  j  av  a2s.c  o  m*/
 * @param filePath .
 * @return Number of A4 pages
 */
public static int countWordFile(String filePath) {
    FileInputStream fis = null;
    int page = 0;
    try {
        fis = new FileInputStream(filePath);

        if (CommonUtil.getExtension(filePath).equals("doc")) { // When file is .DOC
            HWPFDocument doc = new HWPFDocument(fis);
            page = doc.getDocProperties().getCPg();
        } else if (CommonUtil.getExtension(filePath).equals("docx")) { // When file is .DOCX
            XWPFDocument doc = new XWPFDocument(fis);
            XWPFWordExtractor ex = new XWPFWordExtractor(doc);
            page = ex.getExtendedProperties().getUnderlyingProperties().getPages();
        }
    } catch (FileNotFoundException ex) {
        LOG.warn("File " + filePath + " not found", ex);
    } catch (IOException ex) {
        LOG.warn("Invalid when reading file.", ex);
    } catch (Exception ex) {
        LOG.warn("Can not count file " + filePath, ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                LOG.warn("Close the file input stream", ex);
            }
        }
    }
    return page;
}