AbstractPDFCore.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.pdf;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.InputStream;

  24. import org.apache.pdfbox.pdmodel.PDDocument;
  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.resources.FileSystemUtilities;

  28. /**
  29. * AbstractPDFCore
  30. *
  31. * @author Andrea Poli (apoli@link.it)
  32. * @author $Author$
  33. * @version $Rev$, $Date$
  34. */
  35. public class AbstractPDFCore {

  36.     protected PDDocument document;
  37.     protected byte[] rawDocument;
  38.    
  39.     public AbstractPDFCore(PDDocument doc) throws UtilsException {
  40.         this.document = doc;
  41.         if(this.document==null) {
  42.             throw new UtilsException("Document undefined");
  43.         }
  44.     }
  45.     public AbstractPDFCore(byte [] content, boolean saveRawDocument) throws UtilsException {
  46.         try {
  47.             if(saveRawDocument) {
  48.                 this.rawDocument = content;
  49.             }
  50.             this.document = PDDocument.load(content);
  51.         }catch(Exception e) {
  52.             throw new UtilsException(e.getMessage(),e);
  53.         }
  54.     }
  55.     public AbstractPDFCore(InputStream is, boolean saveRawDocument) throws UtilsException {
  56.         try {
  57.             if(saveRawDocument) {
  58.                 this.rawDocument = Utilities.getAsByteArray(is);
  59.                 try(ByteArrayInputStream bin = new ByteArrayInputStream(this.rawDocument)){
  60.                     this.document = PDDocument.load(bin);
  61.                 }
  62.             }
  63.             else {
  64.                 this.document = PDDocument.load(is);
  65.             }
  66.         }catch(Exception e) {
  67.             throw new UtilsException(e.getMessage(),e);
  68.         }
  69.     }
  70.     public AbstractPDFCore(File doc, boolean saveRawDocument) throws UtilsException {
  71.         try {
  72.             if(saveRawDocument) {
  73.                 this.rawDocument = FileSystemUtilities.readBytesFromFile(doc);
  74.             }
  75.             this.document = PDDocument.load(doc);
  76.         }catch(Exception e) {
  77.             throw new UtilsException(e.getMessage(),e);
  78.         }
  79.     }
  80.    
  81.     public PDDocument getDocument() {
  82.         return this.document;
  83.     }
  84.    
  85.     protected void checkDocumentCatalog() throws UtilsException {
  86.         if(this.document.getDocumentCatalog()==null) {
  87.             throw new UtilsException("DocumentCatalog undefined");
  88.         }
  89.     }
  90.    
  91. }