001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }