You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFInfo.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.util.Date;
  22. import java.util.TimeZone;
  23. import org.apache.xmlgraphics.util.DateFormatUtil;
  24. /**
  25. * class representing an /Info object
  26. */
  27. public class PDFInfo extends PDFObject {
  28. /**
  29. * the application producing the PDF
  30. */
  31. private String producer;
  32. private String title = null;
  33. private String author = null;
  34. private String subject = null;
  35. private String keywords = null;
  36. private Date creationDate = null;
  37. private Date modDate = null;
  38. /**
  39. * the name of the application that created the
  40. * original document before converting to PDF
  41. */
  42. private String creator;
  43. /** @return the producer of the document or null if not set */
  44. public String getProducer() {
  45. return this.producer;
  46. }
  47. /**
  48. * set the producer string
  49. *
  50. * @param producer the producer string
  51. */
  52. public void setProducer(String producer) {
  53. this.producer = producer;
  54. }
  55. /** @return the creator of the document or null if not set */
  56. public String getCreator() {
  57. return this.creator;
  58. }
  59. /**
  60. * set the creator string
  61. *
  62. * @param creator the document creator
  63. */
  64. public void setCreator(String creator) {
  65. this.creator = creator;
  66. }
  67. /** @return the title string */
  68. public String getTitle() {
  69. return this.title;
  70. }
  71. /**
  72. * set the title string
  73. *
  74. * @param t the document title
  75. */
  76. public void setTitle(String t) {
  77. this.title = t;
  78. }
  79. /** @return the author of the document or null if not set */
  80. public String getAuthor() {
  81. return this.author;
  82. }
  83. /**
  84. * set the author string
  85. *
  86. * @param a the document author
  87. */
  88. public void setAuthor(String a) {
  89. this.author = a;
  90. }
  91. /** @return the subject of the document or null if not set */
  92. public String getSubject() {
  93. return this.subject;
  94. }
  95. /**
  96. * set the subject string
  97. *
  98. * @param s the document subject
  99. */
  100. public void setSubject(String s) {
  101. this.subject = s;
  102. }
  103. /** @return the keywords for the document or null if not set */
  104. public String getKeywords() {
  105. return this.keywords;
  106. }
  107. /**
  108. * set the keywords string
  109. *
  110. * @param k the keywords for this document
  111. */
  112. public void setKeywords(String k) {
  113. this.keywords = k;
  114. }
  115. /**
  116. * @return last set creation date
  117. */
  118. public Date getCreationDate() {
  119. return creationDate;
  120. }
  121. /**
  122. * @param date Date to store in the PDF as creation date. Use null to force current system date.
  123. */
  124. public void setCreationDate(Date date) {
  125. creationDate = date;
  126. }
  127. /** @return last modification date
  128. */
  129. public Date getModDate() {
  130. return this.modDate;
  131. }
  132. /**
  133. * Sets the date of the last modification.
  134. * @param date the last modification date or null if there are no modifications
  135. */
  136. public void setModDate(Date date) {
  137. this.modDate = date;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public byte[] toPDF() {
  143. PDFProfile profile = getDocumentSafely().getProfile();
  144. ByteArrayOutputStream bout = new ByteArrayOutputStream(128);
  145. try {
  146. bout.write(encode("<<\n"));
  147. if (title != null && title.length() > 0) {
  148. bout.write(encode("/Title "));
  149. bout.write(encodeText(this.title));
  150. bout.write(encode("\n"));
  151. } else {
  152. profile.verifyTitleAbsent();
  153. }
  154. if (author != null) {
  155. bout.write(encode("/Author "));
  156. bout.write(encodeText(this.author));
  157. bout.write(encode("\n"));
  158. }
  159. if (subject != null) {
  160. bout.write(encode("/Subject "));
  161. bout.write(encodeText(this.subject));
  162. bout.write(encode("\n"));
  163. }
  164. if (keywords != null) {
  165. bout.write(encode("/Keywords "));
  166. bout.write(encodeText(this.keywords));
  167. bout.write(encode("\n"));
  168. }
  169. if (creator != null) {
  170. bout.write(encode("/Creator "));
  171. bout.write(encodeText(this.creator));
  172. bout.write(encode("\n"));
  173. }
  174. bout.write(encode("/Producer "));
  175. bout.write(encodeText(this.producer));
  176. bout.write(encode("\n"));
  177. // creation date in form (D:YYYYMMDDHHmmSSOHH'mm')
  178. if (creationDate == null) {
  179. creationDate = new Date();
  180. }
  181. bout.write(encode("/CreationDate "));
  182. bout.write(encodeString(formatDateTime(creationDate)));
  183. bout.write(encode("\n"));
  184. if (profile.isModDateRequired() && this.modDate == null) {
  185. this.modDate = this.creationDate;
  186. }
  187. if (this.modDate != null) {
  188. bout.write(encode("/ModDate "));
  189. bout.write(encodeString(formatDateTime(modDate)));
  190. bout.write(encode("\n"));
  191. }
  192. if (profile.isPDFXActive()) {
  193. bout.write(encode("/GTS_PDFXVersion "));
  194. bout.write(encodeString(profile.getPDFXMode().getName()));
  195. bout.write(encode("\n"));
  196. }
  197. if (profile.isTrappedEntryRequired()) {
  198. bout.write(encode("/Trapped /False\n"));
  199. }
  200. bout.write(encode(">>"));
  201. } catch (IOException ioe) {
  202. log.error("Ignored I/O exception", ioe);
  203. }
  204. return bout.toByteArray();
  205. }
  206. /**
  207. * Formats a date/time according to the PDF specification (D:YYYYMMDDHHmmSSOHH'mm').
  208. * @param time date/time value to format
  209. * @param tz the time zone
  210. * @return the requested String representation
  211. */
  212. protected static String formatDateTime(final Date time, TimeZone tz) {
  213. return DateFormatUtil.formatPDFDate(time, tz);
  214. }
  215. /**
  216. * Formats a date/time according to the PDF specification. (D:YYYYMMDDHHmmSSOHH'mm').
  217. * @param time date/time value to format
  218. * @return the requested String representation
  219. */
  220. protected static String formatDateTime(final Date time) {
  221. return formatDateTime(time, TimeZone.getDefault());
  222. }
  223. }