Java tutorial
/* * PDFGalWeb * Copyright (c) 2014, Alejandro Pernas Pan, All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. */ package org.pdfgal.pdfgalweb.services.impl; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.exceptions.COSVisitorException; import org.pdfgal.pdfgal.model.enumerated.WatermarkPosition; import org.pdfgal.pdfgal.pdfgal.PDFGal; import org.pdfgal.pdfgalweb.forms.DownloadForm; import org.pdfgal.pdfgalweb.model.enumerated.CustomColor; import org.pdfgal.pdfgalweb.services.WatermarkService; import org.pdfgal.pdfgalweb.utils.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @Service public class WatermarkServiceImpl implements WatermarkService { @Autowired private FileUtils fileUtils; @Autowired private PDFGal pdfGal; @Override public DownloadForm putWatermark(final MultipartFile file, final String text, final CustomColor customColor, final Float alpha, final WatermarkPosition watermarkPosition, final String pages, final HttpServletResponse response) throws Exception { DownloadForm result = new DownloadForm(); if (!file.isEmpty() && StringUtils.isNotBlank(text) && customColor != null && alpha != null && watermarkPosition != null && response != null) { final String originalName = file.getOriginalFilename(); final String inputUri = this.fileUtils.saveFile(file); final String outputUri = this.fileUtils.getAutogeneratedName(originalName); // File is watermarked try { final List<Integer> pagesList = this.getPages(pages); this.pdfGal.putWatermark(inputUri, outputUri, text, customColor.getColor(), alpha, watermarkPosition, pagesList); } catch (COSVisitorException | IOException | IllegalArgumentException e) { // Temporal files are deleted from system this.fileUtils.delete(inputUri); this.fileUtils.delete(outputUri); throw e; } // Temporal files are deleted from system this.fileUtils.delete(inputUri); result = new DownloadForm(outputUri, originalName); } return result; } /** * Gets the pages from a string with tokens. * * @param pages * @return */ private List<Integer> getPages(final String pages) { final List<Integer> result = new ArrayList<Integer>(); final StringTokenizer st = new StringTokenizer(pages, ","); while (st.hasMoreElements()) { final String token = (String) st.nextElement(); try { final Integer page = Integer.parseInt(token); result.add(page); } catch (final Exception e) { final StringTokenizer secondSt = new StringTokenizer(token, "-"); if (secondSt.countTokens() != 2) { throw new IllegalArgumentException(); } final String secondTokenFirst = (String) secondSt.nextElement(); final String secondTokenLast = (String) secondSt.nextElement(); try { final Integer firstPage = Integer.parseInt(secondTokenFirst); final Integer lastPage = Integer.parseInt(secondTokenLast); for (int i = firstPage; i <= lastPage; i++) { result.add(i); } } catch (final Exception e2) { throw new IllegalArgumentException(); } } } return result; } }