001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.image.ImageToolImpl;
018    import com.liferay.portal.kernel.image.ImageTool;
019    
020    import java.awt.image.BufferedImage;
021    import java.awt.image.RenderedImage;
022    
023    import java.io.File;
024    
025    import java.util.List;
026    
027    import javax.imageio.ImageIO;
028    
029    import org.apache.pdfbox.pdmodel.PDDocument;
030    import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
031    import org.apache.pdfbox.pdmodel.PDPage;
032    
033    /**
034     * @author Juan Gonzalez
035     */
036    public class LiferayPDFBoxConverter {
037    
038            public LiferayPDFBoxConverter(
039                    File inputFile, File thumbnailFile, File[] previewFiles,
040                    String extension, String thumbnailExtension, int dpi, int height,
041                    int width, boolean generatePreview, boolean generateThumbnail) {
042    
043                    _inputFile = inputFile;
044                    _thumbnailFile = thumbnailFile;
045                    _previewFiles = previewFiles;
046                    _extension = extension;
047                    _thumbnailExtension = thumbnailExtension;
048                    _dpi = dpi;
049                    _height = height;
050                    _width = width;
051                    _generatePreview = generatePreview;
052                    _generateThumbnail = generateThumbnail;
053            }
054    
055            public void generateImagesPB() throws Exception {
056                    PDDocument pdDocument = null;
057    
058                    try {
059                            pdDocument = PDDocument.load(_inputFile);
060    
061                            PDDocumentCatalog pdDocumentCatalog =
062                                    pdDocument.getDocumentCatalog();
063    
064                            List<PDPage> pdPages = pdDocumentCatalog.getAllPages();
065    
066                            for (int i = 0; i < pdPages.size(); i++) {
067                                    PDPage pdPage = pdPages.get(i);
068    
069                                    if (_generateThumbnail && (i == 0)) {
070                                            _generateImagesPB(
071                                                    pdPage, i, _thumbnailFile, _thumbnailExtension);
072                                    }
073    
074                                    if (!_generatePreview) {
075                                            break;
076                                    }
077    
078                                    _generateImagesPB(pdPage, i + 1, _previewFiles[i], _extension);
079                            }
080                    }
081                    finally {
082                            if (pdDocument != null) {
083                                    pdDocument.close();
084                            }
085                    }
086            }
087    
088            private void _generateImagesPB(
089                            PDPage pdPage, int index, File outputFile, String extension)
090                    throws Exception {
091    
092                    RenderedImage renderedImage = pdPage.convertToImage(
093                            BufferedImage.TYPE_INT_RGB, _dpi);
094    
095                    ImageTool imageTool = ImageToolImpl.getInstance();
096    
097                    if (_height != 0) {
098                            renderedImage = imageTool.scale(renderedImage, _width, _height);
099                    }
100                    else {
101                            renderedImage = imageTool.scale(renderedImage, _width);
102                    }
103    
104                    outputFile.createNewFile();
105    
106                    ImageIO.write(renderedImage, extension, outputFile);
107            }
108    
109            private int _dpi;
110            private String _extension;
111            private boolean _generatePreview;
112            private boolean _generateThumbnail;
113            private int _height;
114            private File _inputFile;
115            private File[] _previewFiles;
116            private String _thumbnailExtension;
117            private File _thumbnailFile;
118            private int _width;
119    
120    }