PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/itext-5.0.3/core/com/itextpdf/text/pdf/PdfCopyFields.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 237 lines | 68 code | 22 blank | 147 comment | 0 complexity | c147956d74cabb49db62900db46d08b9 MD5 | raw file
  1. /*
  2. * $Id: PdfCopyFields.java 4242 2010-01-02 23:22:20Z xlv $
  3. *
  4. * This file is part of the iText project.
  5. * Copyright (c) 1998-2009 1T3XT BVBA
  6. * Authors: Bruno Lowagie, Paulo Soares, et al.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3
  10. * as published by the Free Software Foundation with the addition of the
  11. * following permission added to Section 15 as permitted in Section 7(a):
  12. * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
  13. * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU Affero General Public License for more details.
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program; if not, see http://www.gnu.org/licenses or write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  23. * http://itextpdf.com/terms-of-use/
  24. *
  25. * The interactive user interfaces in modified source and object code versions
  26. * of this program must display Appropriate Legal Notices, as required under
  27. * Section 5 of the GNU Affero General Public License.
  28. *
  29. * In accordance with Section 7(b) of the GNU Affero General Public License,
  30. * you must retain the producer line in every PDF that is created or manipulated
  31. * using iText.
  32. *
  33. * You can be released from the requirements of the license by purchasing
  34. * a commercial license. Buying such a license is mandatory as soon as you
  35. * develop commercial activities involving the iText software without
  36. * disclosing the source code of your own applications.
  37. * These activities include: offering paid services to customers as an ASP,
  38. * serving PDFs on the fly in a web application, shipping iText with a closed
  39. * source product.
  40. *
  41. * For more information, please contact iText Software Corp. at this
  42. * address: sales@itextpdf.com
  43. */
  44. package com.itextpdf.text.pdf;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. import java.security.cert.Certificate;
  48. import java.util.HashMap;
  49. import java.util.List;
  50. import com.itextpdf.text.DocWriter;
  51. import com.itextpdf.text.DocumentException;
  52. import com.itextpdf.text.pdf.interfaces.PdfEncryptionSettings;
  53. import com.itextpdf.text.pdf.interfaces.PdfViewerPreferences;
  54. /**
  55. * Concatenates PDF documents including form fields. The rules for the form field
  56. * concatenation are the same as in Acrobat. All the documents are kept in memory unlike
  57. * PdfCopy.
  58. * @author Paulo Soares
  59. */
  60. public class PdfCopyFields
  61. implements PdfViewerPreferences, PdfEncryptionSettings {
  62. private PdfCopyFieldsImp fc;
  63. /**
  64. * Creates a new instance.
  65. * @param os the output stream
  66. * @throws DocumentException on error
  67. */
  68. public PdfCopyFields(OutputStream os) throws DocumentException {
  69. fc = new PdfCopyFieldsImp(os);
  70. }
  71. /**
  72. * Creates a new instance.
  73. * @param os the output stream
  74. * @param pdfVersion the pdf version the output will have
  75. * @throws DocumentException on error
  76. */
  77. public PdfCopyFields(OutputStream os, char pdfVersion) throws DocumentException {
  78. fc = new PdfCopyFieldsImp(os, pdfVersion);
  79. }
  80. /**
  81. * Concatenates a PDF document.
  82. * @param reader the PDF document
  83. * @throws DocumentException on error
  84. */
  85. public void addDocument(PdfReader reader) throws DocumentException, IOException {
  86. fc.addDocument(reader);
  87. }
  88. /**
  89. * Concatenates a PDF document selecting the pages to keep. The pages are described as a
  90. * <CODE>List</CODE> of <CODE>Integer</CODE>. The page ordering can be changed but
  91. * no page repetitions are allowed.
  92. * @param reader the PDF document
  93. * @param pagesToKeep the pages to keep
  94. * @throws DocumentException on error
  95. */
  96. public void addDocument(PdfReader reader, List<Integer> pagesToKeep) throws DocumentException, IOException {
  97. fc.addDocument(reader, pagesToKeep);
  98. }
  99. /**
  100. * Concatenates a PDF document selecting the pages to keep. The pages are described as
  101. * ranges. The page ordering can be changed but
  102. * no page repetitions are allowed.
  103. * @param reader the PDF document
  104. * @param ranges the comma separated ranges as described in {@link SequenceList}
  105. * @throws DocumentException on error
  106. */
  107. public void addDocument(PdfReader reader, String ranges) throws DocumentException, IOException {
  108. fc.addDocument(reader, SequenceList.expand(ranges, reader.getNumberOfPages()));
  109. }
  110. /** Sets the encryption options for this document. The userPassword and the
  111. * ownerPassword can be null or have zero length. In this case the ownerPassword
  112. * is replaced by a random string. The open permissions for the document can be
  113. * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
  114. * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
  115. * The permissions can be combined by ORing them.
  116. * @param userPassword the user password. Can be null or empty
  117. * @param ownerPassword the owner password. Can be null or empty
  118. * @param permissions the user permissions
  119. * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
  120. * @throws DocumentException if the document is already open
  121. */
  122. public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
  123. fc.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40);
  124. }
  125. /**
  126. * Sets the encryption options for this document. The userPassword and the
  127. * ownerPassword can be null or have zero length. In this case the ownerPassword
  128. * is replaced by a random string. The open permissions for the document can be
  129. * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
  130. * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
  131. * The permissions can be combined by ORing them.
  132. * @param strength true for 128 bit key length. false for 40 bit key length
  133. * @param userPassword the user password. Can be null or empty
  134. * @param ownerPassword the owner password. Can be null or empty
  135. * @param permissions the user permissions
  136. * @throws DocumentException if the document is already open
  137. */
  138. public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException {
  139. setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength);
  140. }
  141. /**
  142. * Closes the output document.
  143. */
  144. public void close() {
  145. fc.close();
  146. }
  147. /**
  148. * Opens the document. This is usually not needed as addDocument() will do it
  149. * automatically.
  150. */
  151. public void open() {
  152. fc.openDoc();
  153. }
  154. /**
  155. * Adds JavaScript to the global document
  156. * @param js the JavaScript
  157. */
  158. public void addJavaScript(String js) {
  159. fc.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
  160. }
  161. /**
  162. * Sets the bookmarks. The list structure is defined in
  163. * <CODE>SimpleBookmark#</CODE>.
  164. * @param outlines the bookmarks or <CODE>null</CODE> to remove any
  165. */
  166. public void setOutlines(List<HashMap<String, Object>> outlines) {
  167. fc.setOutlines(outlines);
  168. }
  169. /** Gets the underlying PdfWriter.
  170. * @return the underlying PdfWriter
  171. */
  172. public PdfWriter getWriter() {
  173. return fc;
  174. }
  175. /**
  176. * Gets the 1.5 compression status.
  177. * @return <code>true</code> if the 1.5 compression is on
  178. */
  179. public boolean isFullCompression() {
  180. return fc.isFullCompression();
  181. }
  182. /**
  183. * Sets the document's compression to the new 1.5 mode with object streams and xref
  184. * streams. It can be set at any time but once set it can't be unset.
  185. * <p>
  186. * If set before opening the document it will also set the pdf version to 1.5.
  187. */
  188. public void setFullCompression() {
  189. fc.setFullCompression();
  190. }
  191. /**
  192. * @see com.itextpdf.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(byte[], byte[], int, int)
  193. */
  194. public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) throws DocumentException {
  195. fc.setEncryption(userPassword, ownerPassword, permissions, encryptionType);
  196. }
  197. /**
  198. * @see com.itextpdf.text.pdf.interfaces.PdfViewerPreferences#addViewerPreference(com.itextpdf.text.pdf.PdfName, com.itextpdf.text.pdf.PdfObject)
  199. */
  200. public void addViewerPreference(PdfName key, PdfObject value) {
  201. fc.addViewerPreference(key, value);
  202. }
  203. /**
  204. * @see com.itextpdf.text.pdf.interfaces.PdfViewerPreferences#setViewerPreferences(int)
  205. */
  206. public void setViewerPreferences(int preferences) {
  207. fc.setViewerPreferences(preferences);
  208. }
  209. /**
  210. * @see com.itextpdf.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(java.security.cert.Certificate[], int[], int)
  211. */
  212. public void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException {
  213. fc.setEncryption(certs, permissions, encryptionType);
  214. }
  215. }