All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sejda.impl.sambox.SplitByPageNumbersTask Maven / Gradle / Ivy

The newest version!
/* 
 * Copyright 2015 by Andrea Vacondio ([email protected]).
 *
 * This file is part of the Sejda source code
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package org.sejda.impl.sambox;

import org.sejda.impl.sambox.component.DefaultPdfSourceOpener;
import org.sejda.impl.sambox.component.PDDocumentHandler;
import org.sejda.impl.sambox.component.optimization.OptimizationRuler;
import org.sejda.impl.sambox.component.split.PagesPdfSplitter;
import org.sejda.model.exception.TaskException;
import org.sejda.model.input.PdfSource;
import org.sejda.model.input.PdfSourceOpener;
import org.sejda.model.parameter.AbstractSplitByPageParameters;
import org.sejda.model.task.BaseTask;
import org.sejda.model.task.TaskExecutionContext;
import org.sejda.sambox.pdmodel.PDDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.sejda.commons.util.IOUtils.closeQuietly;
import static org.sejda.core.notification.dsl.ApplicationEventsNotifier.notifyEvent;

/**
 * Task splitting an input pdf document on a set of pages defined in the input parameter object.
 * 
 * @author Andrea Vacondio
 * @param 
 *            the type of the parameters.
 */
public class SplitByPageNumbersTask extends BaseTask {

    private static final Logger LOG = LoggerFactory.getLogger(SplitByPageNumbersTask.class);

    private int totalSteps;
    private PDDocument document = null;
    private PdfSourceOpener documentLoader;
    private PagesPdfSplitter splitter;

    @Override
    public void before(T parameters, TaskExecutionContext executionContext) throws TaskException {
        super.before(parameters, executionContext);
        totalSteps = parameters.getSourceList().size();
        documentLoader = new DefaultPdfSourceOpener(executionContext);
    }

    @Override
    public void execute(T parameters) throws TaskException {
        int currentStep = 0;

        for (PdfSource source : parameters.getSourceList()) {
            currentStep++;

            LOG.debug("Opening {}", source);
            executionContext().notifiableTaskMetadata().setCurrentSource(source);
            document = source.open(documentLoader).getUnderlyingPDDocument();

            splitter = new PagesPdfSplitter<>(document, parameters,
                    new OptimizationRuler(parameters.getOptimizationPolicy()).apply(document));

            LOG.debug("Starting split by page numbers for {} ", parameters);
            splitter.split(executionContext(), parameters.getOutputPrefix(), source);

            closeQuietly(document);

            notifyEvent(executionContext().notifiableTaskMetadata()).stepsCompleted(currentStep).outOf(totalSteps);
        }

        executionContext().notifiableTaskMetadata().clearCurrentSource();
        LOG.debug("Input documents split and written to {}", parameters.getOutput());
    }

    @Override
    public void after() {
        closeQuietly(document);
        splitter = null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy