Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFDocument#getFooterList()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFDocument#getFooterList() . 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: POIServices.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Marks the {@link CTSimpleField} of the given {@link IBody} as
 * {@link CTSimpleField#setDirty(org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff.Enum) dirty}.
 * 
 * @param body
 *            the {@link IBody}
 */
public void markFieldsAsDirty(IBody body) {
    if (body instanceof XWPFDocument) {
        final XWPFDocument document = (XWPFDocument) body;
        for (XWPFHeader header : document.getHeaderList()) {
            markFieldsAsDirty(header);
        }
        for (XWPFFooter footer : document.getFooterList()) {
            markFieldsAsDirty(footer);
        }
    }
    for (IBodyElement element : body.getBodyElements()) {
        markFieldsAsDirty(element);
    }
}
 
Example 2
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parses a document for {@link UserContent} and returns the {@link DocumentTemplate} resulting from
 * this parsing.
 * 
 * @param uriConverter
 *            the {@link URIConverter uri converter} to use.
 * @param documentURI
 *            URI for the document
 * @param queryEnvironment
 *            the {@link IQueryEnvironment}
 * @return the {@link DocumentTemplate} resulting from parsing the specified
 *         document
 * @throws DocumentParserException
 *             if a problem occurs while parsing the document.
 */
@SuppressWarnings("resource")
public static DocumentTemplate parseUserContent(URIConverter uriConverter, URI documentURI,
        IQueryEnvironment queryEnvironment) throws DocumentParserException {
    final DocumentTemplate result = (DocumentTemplate) EcoreUtil.create(TemplatePackage.Literals.DOCUMENT_TEMPLATE);
    final ResourceImpl r = new ResourceImpl(documentURI);

    try {
        // resources are closed in DocumentTemplate.close()
        final InputStream is = uriConverter.createInputStream(documentURI);
        final OPCPackage oPackage = OPCPackage.open(is);
        final XWPFDocument document = new XWPFDocument(oPackage);
        r.getContents().add(result);
        final BodyGeneratedParser parser = new BodyGeneratedParser(document, queryEnvironment);
        result.setBody(parser.parseBlock(null));
        result.setInputStream(is);
        result.setOpcPackage(oPackage);
        result.setDocument(document);
        for (XWPFFooter footer : document.getFooterList()) {
            final BodyGeneratedParser footerParser = new BodyGeneratedParser(footer, queryEnvironment);
            result.getFooters().add(footerParser.parseBlock(null));
        }
        for (XWPFHeader header : document.getHeaderList()) {
            final BodyGeneratedParser headerParser = new BodyGeneratedParser(header, queryEnvironment);
            result.getHeaders().add(headerParser.parseBlock(null));
        }

    } catch (IOException e) {
        throw new DocumentParserException("Unable to open " + documentURI, e);
    } catch (InvalidFormatException e1) {
        throw new DocumentParserException("Invalid .docx format " + documentURI, e1);
    }

    return result;
}
 
Example 3
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Parses a template document and returns the {@link DocumentTemplate} resulting from
 * this parsing.
 * 
 * @param uriConverter
 *            the {@link URIConverter uri converter} to use.
 * @param templateURI
 *            URI for the template, used when external links (images, includes) have to be resolved
 * @param queryEnvironment
 *            the {@link IQueryEnvironment}
 * @param classProvider
 *            the {@link IClassProvider} to use for service Loading
 * @param monitor
 *            used to track the progress will generating
 * @return the {@link DocumentTemplate} resulting from parsing the specified
 *         document
 * @throws DocumentParserException
 *             if a problem occurs while parsing the document.
 */
@SuppressWarnings("resource")
public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI, IQueryEnvironment queryEnvironment,
        IClassProvider classProvider, Monitor monitor) throws DocumentParserException {
    final DocumentTemplate result = (DocumentTemplate) EcoreUtil.create(TemplatePackage.Literals.DOCUMENT_TEMPLATE);
    final ResourceImpl r = new ResourceImpl(templateURI);

    try {
        monitor.beginTask("Parsing " + templateURI, TOTAL_PARSE_MONITOR_WORK);
        monitor.subTask("Loading template");
        // resources are closed in DocumentTemplate.close()
        final InputStream is = uriConverter.createInputStream(templateURI);
        final OPCPackage oPackage = OPCPackage.open(is);
        final XWPFDocument document = new XWPFDocument(oPackage);

        nextSubTask(monitor, LOAD_TEMPLATE_MONITOR_WORK, "Parsing template custom properties");

        final List<TemplateValidationMessage> messages = parseTemplateCustomProperties(queryEnvironment,
                classProvider, document);
        r.getContents().add(result);

        nextSubTask(monitor, PARSE_TEMPLATE_CUSTOM_PROPERTIES_MONITOR_WORK, "Parsing template body");
        final int unitOfWork = PARSE_TEMPLATE_MONITOR_WORK
            / (1 + document.getFooterList().size() + document.getHeaderList().size());

        final M2DocParser parser = new M2DocParser(document, queryEnvironment);
        final Block documentBody = parser.parseBlock(result.getTemplates(), TokenType.EOF);
        for (TemplateValidationMessage validationMessage : messages) {
            documentBody.getValidationMessages().add(validationMessage);
        }
        result.setBody(documentBody);
        result.setInputStream(is);
        result.setOpcPackage(oPackage);
        result.setDocument(document);

        nextSubTask(monitor, unitOfWork, "Parsing template footers");

        for (XWPFFooter footer : document.getFooterList()) {
            final M2DocParser footerParser = new M2DocParser(footer, queryEnvironment);
            result.getFooters().add(footerParser.parseBlock(null, TokenType.EOF));

            monitor.worked(unitOfWork);
        }

        nextSubTask(monitor, 0, "Parsing template headers");

        for (XWPFHeader header : document.getHeaderList()) {
            final M2DocParser headerParser = new M2DocParser(header, queryEnvironment);
            result.getHeaders().add(headerParser.parseBlock(null, TokenType.EOF));

            monitor.worked(unitOfWork);
        }

    } catch (IOException e) {
        throw new DocumentParserException("Unable to open " + templateURI, e);
    } catch (InvalidFormatException e1) {
        throw new DocumentParserException("Invalid .docx format " + templateURI, e1);
    } finally {
        monitor.done();
    }

    return result;
}