Java tutorial
/* * Created on 06/jun/2010 * * Copyright 2010 by Andrea Vacondio (andrea.vacondio@gmail.com). * * 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.component; import static org.sejda.model.rotation.Rotation.getRotation; import java.util.Set; import org.sejda.model.rotation.Rotation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfName; import com.lowagie.text.pdf.PdfNumber; import com.lowagie.text.pdf.PdfReader; /** * Handles rotations on a given PdfReader * * @author Andrea Vacondio * */ public final class PdfRotator implements OngoingRotation { private static final Logger LOG = LoggerFactory.getLogger(PdfRotator.class); private PdfReader reader; private Rotation rotation; private Set<Integer> pages; private PdfRotator(Rotation rotation, Set<Integer> pages) { this.rotation = rotation; this.pages = pages; } /** * DSL entry point to apply a rotation to a set of pages * <p> * <code>applyRotation(rotation, pages).to(document);</code> * </p> * * @param rotation * @param pages * @return the ongoing apply rotation exposing methods to set the reader you want to apply the rotation to. */ public static OngoingRotation applyRotation(Rotation rotation, Set<Integer> pages) { return new PdfRotator(rotation, pages); } public void to(PdfReader reader) { this.reader = reader; executeRotation(); } /** * Apply the rotation to the dictionary of the given {@link PdfReader} */ private void executeRotation() { LOG.debug("Applying rotation of {} degrees to {} pages", rotation.getDegrees(), pages.size()); for (int p : pages) { apply(p); } } /** * apply the rotation to the given page if necessary * * @param pageNmber */ private void apply(int pageNmber) { if (pages.contains(pageNmber)) { PdfDictionary dictionary = reader.getPageN(pageNmber); dictionary.put(PdfName.ROTATE, new PdfNumber( rotation.addRotation(getRotation(reader.getPageRotation(pageNmber))).getDegrees())); } } }