PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fstmerge/examples/iText/rev2818-3392/right-branch-3392/core/com/lowagie/text/pdf/PdfStamper.java

https://github.com/RoDaniel/featurehouse
Java | 325 lines | 228 code | 97 blank | 0 comment | 15 complexity | 5463202bcfde654037645bca9e84dd7d MD5 | raw file
  1. package com.lowagie.text.pdf;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.security.SignatureException;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import com.lowagie.text.DocWriter;
  11. import com.lowagie.text.DocumentException;
  12. import com.lowagie.text.ExceptionConverter;
  13. import com.lowagie.text.Image;
  14. import com.lowagie.text.Rectangle;
  15. import com.lowagie.text.pdf.collection.PdfCollection;
  16. import com.lowagie.text.pdf.interfaces.PdfEncryptionSettings;
  17. import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
  18. import java.security.cert.Certificate;
  19. public class PdfStamper
  20. implements PdfViewerPreferences, PdfEncryptionSettings {
  21. protected PdfStamperImp stamper;
  22. private HashMap<String, String> moreInfo;
  23. private boolean hasSignature;
  24. private PdfSignatureAppearance sigApp;
  25. public PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException {
  26. stamper = new PdfStamperImp(reader, os, '\0', false);
  27. }
  28. public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException {
  29. stamper = new PdfStamperImp(reader, os, pdfVersion, false);
  30. }
  31. public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append) throws DocumentException, IOException {
  32. stamper = new PdfStamperImp(reader, os, pdfVersion, append);
  33. }
  34. public HashMap<String, String> getMoreInfo() {
  35. return this.moreInfo;
  36. }
  37. public void setMoreInfo(HashMap<String, String> moreInfo) {
  38. this.moreInfo = moreInfo;
  39. }
  40. public void replacePage(PdfReader r, int pageImported, int pageReplaced) {
  41. stamper.replacePage(r, pageImported, pageReplaced);
  42. }
  43. public void insertPage(int pageNumber, Rectangle mediabox) {
  44. stamper.insertPage(pageNumber, mediabox);
  45. }
  46. public PdfSignatureAppearance getSignatureAppearance() {
  47. return sigApp;
  48. }
  49. public void close() throws DocumentException, IOException {
  50. if (!hasSignature) {
  51. stamper.close(moreInfo);
  52. return;
  53. }
  54. sigApp.preClose();
  55. PdfSigGenericPKCS sig = sigApp.getSigStandard();
  56. PdfLiteral lit = (PdfLiteral)sig.get(PdfName.CONTENTS);
  57. int totalBuf = (lit.getPosLength() - 2) / 2;
  58. byte buf[] = new byte[8192];
  59. int n;
  60. InputStream inp = sigApp.getRangeStream();
  61. try {
  62. while ((n = inp.read(buf)) > 0) {
  63. sig.getSigner().update(buf, 0, n);
  64. }
  65. }
  66. catch (SignatureException se) {
  67. throw new ExceptionConverter(se);
  68. }
  69. buf = new byte[totalBuf];
  70. byte[] bsig = sig.getSignerContents();
  71. System.arraycopy(bsig, 0, buf, 0, bsig.length);
  72. PdfString str = new PdfString(buf);
  73. str.setHexWriting(true);
  74. PdfDictionary dic = new PdfDictionary();
  75. dic.put(PdfName.CONTENTS, str);
  76. sigApp.close(dic);
  77. stamper.reader.close();
  78. }
  79. public PdfContentByte getUnderContent(int pageNum) {
  80. return stamper.getUnderContent(pageNum);
  81. }
  82. public PdfContentByte getOverContent(int pageNum) {
  83. return stamper.getOverContent(pageNum);
  84. }
  85. public boolean isRotateContents() {
  86. return stamper.isRotateContents();
  87. }
  88. public void setRotateContents(boolean rotateContents) {
  89. stamper.setRotateContents(rotateContents);
  90. }
  91. public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
  92. if (stamper.isAppend())
  93. throw new DocumentException("Append mode does not support changing the encryption status.");
  94. if (stamper.isContentWritten())
  95. throw new DocumentException("Content was already written to the output.");
  96. stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40);
  97. }
  98. public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, int encryptionType) throws DocumentException {
  99. if (stamper.isAppend())
  100. throw new DocumentException("Append mode does not support changing the encryption status.");
  101. if (stamper.isContentWritten())
  102. throw new DocumentException("Content was already written to the output.");
  103. stamper.setEncryption(userPassword, ownerPassword, permissions, encryptionType);
  104. }
  105. public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException {
  106. setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength);
  107. }
  108. public void setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions) throws DocumentException {
  109. setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, encryptionType);
  110. }
  111. public void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException {
  112. if (stamper.isAppend())
  113. throw new DocumentException("Append mode does not support changing the encryption status.");
  114. if (stamper.isContentWritten())
  115. throw new DocumentException("Content was already written to the output.");
  116. stamper.setEncryption(certs, permissions, encryptionType);
  117. }
  118. public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber) {
  119. return stamper.getImportedPage(reader, pageNumber);
  120. }
  121. public PdfWriter getWriter() {
  122. return stamper;
  123. }
  124. public PdfReader getReader() {
  125. return stamper.reader;
  126. }
  127. public AcroFields getAcroFields() {
  128. return stamper.getAcroFields();
  129. }
  130. public void setFormFlattening(boolean flat) {
  131. stamper.setFormFlattening(flat);
  132. }
  133. public void setFreeTextFlattening(boolean flat) {
  134. stamper.setFreeTextFlattening(flat);
  135. }
  136. public void addAnnotation(PdfAnnotation annot, int page) {
  137. stamper.addAnnotation(annot, page);
  138. }
  139. public void addComments(FdfReader fdf) throws IOException {
  140. stamper.addComments(fdf);
  141. }
  142. public void setOutlines(List<HashMap<String, Object>> outlines) {
  143. stamper.setOutlines(outlines);
  144. }
  145. public void setThumbnail(Image image, int page) throws PdfException, DocumentException {
  146. stamper.setThumbnail(image, page);
  147. }
  148. public boolean partialFormFlattening(String name) {
  149. return stamper.partialFormFlattening(name);
  150. }
  151. public void addJavaScript(String js) {
  152. stamper.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
  153. }
  154. public void addFileAttachment(String description, byte fileStore[], String file, String fileDisplay) throws IOException {
  155. addFileAttachment(description, PdfFileSpecification.fileEmbedded(stamper, file, fileDisplay, fileStore));
  156. }
  157. public void addFileAttachment(String description, PdfFileSpecification fs) throws IOException {
  158. stamper.addFileAttachment(description, fs);
  159. }
  160. public void makePackage( PdfName initialView ) {
  161. PdfCollection collection = new PdfCollection(0);
  162. collection.put(PdfName.VIEW, initialView);
  163. stamper.makePackage( collection );
  164. }
  165. public void makePackage(PdfCollection collection) {
  166. stamper.makePackage(collection);
  167. }
  168. public void setViewerPreferences(int preferences) {
  169. stamper.setViewerPreferences(preferences);
  170. }
  171. public void addViewerPreference(PdfName key, PdfObject value) {
  172. stamper.addViewerPreference(key, value);
  173. }
  174. public void setXmpMetadata(byte[] xmp) {
  175. stamper.setXmpMetadata(xmp);
  176. }
  177. public boolean isFullCompression() {
  178. return stamper.isFullCompression();
  179. }
  180. public void setFullCompression() {
  181. if (stamper.isAppend())
  182. return;
  183. stamper.setFullCompression();
  184. }
  185. public void setPageAction(PdfName actionType, PdfAction action, int page) throws PdfException {
  186. stamper.setPageAction(actionType, action, page);
  187. }
  188. public void setDuration(int seconds, int page) {
  189. stamper.setDuration(seconds, page);
  190. }
  191. public void setTransition(PdfTransition transition, int page) {
  192. stamper.setTransition(transition, page);
  193. }
  194. public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile, boolean append) throws DocumentException, IOException {
  195. PdfStamper stp;
  196. if (tempFile == null) {
  197. ByteBuffer bout = new ByteBuffer();
  198. stp = new PdfStamper(reader, bout, pdfVersion, append);
  199. stp.sigApp = new PdfSignatureAppearance(stp.stamper);
  200. stp.sigApp.setSigout(bout);
  201. }
  202. else {
  203. if (tempFile.isDirectory())
  204. tempFile = File.createTempFile("pdf", null, tempFile);
  205. FileOutputStream fout = new FileOutputStream(tempFile);
  206. stp = new PdfStamper(reader, fout, pdfVersion, append);
  207. stp.sigApp = new PdfSignatureAppearance(stp.stamper);
  208. stp.sigApp.setTempFile(tempFile);
  209. }
  210. stp.sigApp.setOriginalout(os);
  211. stp.sigApp.setStamper(stp);
  212. stp.hasSignature = true;
  213. PdfDictionary catalog = reader.getCatalog();
  214. PdfDictionary acroForm = (PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.ACROFORM), catalog);
  215. if (acroForm != null) {
  216. acroForm.remove(PdfName.NEEDAPPEARANCES);
  217. stp.stamper.markUsed(acroForm);
  218. }
  219. return stp;
  220. }
  221. public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException {
  222. return createSignature(reader, os, pdfVersion, null, false);
  223. }
  224. public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile) throws DocumentException, IOException
  225. {
  226. return createSignature(reader, os, pdfVersion, tempFile, false);
  227. }
  228. }