org.sejda.impl.itext.SplitBySizeTask Maven / Gradle / Ivy
/*
* Created on 03/ago/2011
* Copyright 2010 by Andrea Vacondio ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sejda.impl.itext;
import static org.sejda.impl.itext.util.ITextUtils.nullSafeClosePdfReader;
import org.sejda.impl.itext.component.input.PdfSourceOpeners;
import org.sejda.impl.itext.component.split.SizePdfSplitter;
import org.sejda.model.exception.TaskException;
import org.sejda.model.input.PdfSourceOpener;
import org.sejda.model.parameter.SplitBySizeParameters;
import org.sejda.model.task.BaseTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lowagie.text.pdf.PdfReader;
/**
* Task splitting an input pdf document when the generated document reaches a given size.
*
* @author Andrea Vacondio
*
*/
public class SplitBySizeTask extends BaseTask {
private static final Logger LOG = LoggerFactory.getLogger(SplitBySizeTask.class);
private PdfReader reader = null;
private PdfSourceOpener sourceOpener;
private SizePdfSplitter splitter;
public void before(SplitBySizeParameters parameters) {
sourceOpener = PdfSourceOpeners.newPartialReadOpener();
}
public void execute(SplitBySizeParameters parameters) throws TaskException {
LOG.debug("Opening {} ", parameters.getSource());
reader = parameters.getSource().open(sourceOpener);
splitter = new SizePdfSplitter(reader, parameters);
LOG.debug("Starting split by size {} bytes", parameters.getSizeToSplitAt());
splitter.split(getNotifiableTaskMetadata());
LOG.debug("Input documents splitted and written to {}", parameters.getOutput());
}
public void after() {
nullSafeClosePdfReader(reader);
splitter = null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy