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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import java.util.Date;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.text.SimpleDateFormat;
  22. /**
  23. * class representing an /Info object
  24. */
  25. public class PDFInfo extends PDFObject {
  26. /**
  27. * the application producing the PDF
  28. */
  29. private String producer;
  30. private String title = null;
  31. private String author = null;
  32. private String subject = null;
  33. private String keywords = null;
  34. private Date creationDate = null;
  35. /**
  36. * the name of the application that created the
  37. * original document before converting to PDF
  38. */
  39. private String creator;
  40. /**
  41. * set the producer string
  42. *
  43. * @param producer the producer string
  44. */
  45. public void setProducer(String producer) {
  46. this.producer = producer;
  47. }
  48. /**
  49. * set the creator string
  50. *
  51. * @param creator the document creator
  52. */
  53. public void setCreator(String creator) {
  54. this.creator = creator;
  55. }
  56. /**
  57. * set the title string
  58. *
  59. * @param t the document title
  60. */
  61. public void setTitle(String t) {
  62. this.title = t;
  63. }
  64. /**
  65. * set the author string
  66. *
  67. * @param a the document author
  68. */
  69. public void setAuthor(String a) {
  70. this.author = a;
  71. }
  72. /**
  73. * set the subject string
  74. *
  75. * @param s the document subject
  76. */
  77. public void setSubject(String s) {
  78. this.subject = s;
  79. }
  80. /**
  81. * set the keywords string
  82. *
  83. * @param k the keywords for this document
  84. */
  85. public void setKeywords(String k) {
  86. this.keywords = k;
  87. }
  88. /**
  89. * @return last set creation date
  90. */
  91. public Date getCreationDate() {
  92. return creationDate;
  93. }
  94. /**
  95. * @param date Date to store in the PDF as creation date. Use null to force current system date.
  96. */
  97. public void setCreationDate(Date date) {
  98. creationDate = date;
  99. }
  100. /**
  101. * @see org.apache.fop.pdf.PDFObject#toPDF()
  102. */
  103. public byte[] toPDF() {
  104. ByteArrayOutputStream bout = new ByteArrayOutputStream(128);
  105. try {
  106. bout.write(encode(getObjectID()));
  107. bout.write(encode("<< /Type /Info\n"));
  108. if (title != null) {
  109. bout.write(encode("/Title "));
  110. bout.write(encodeText(this.title));
  111. bout.write(encode("\n"));
  112. }
  113. if (author != null) {
  114. bout.write(encode("/Author "));
  115. bout.write(encodeText(this.author));
  116. bout.write(encode("\n"));
  117. }
  118. if (subject != null) {
  119. bout.write(encode("/Subject "));
  120. bout.write(encodeText(this.subject));
  121. bout.write(encode("\n"));
  122. }
  123. if (keywords != null) {
  124. bout.write(encode("/Keywords "));
  125. bout.write(encodeText(this.keywords));
  126. bout.write(encode("\n"));
  127. }
  128. if (creator != null) {
  129. bout.write(encode("/Creator "));
  130. bout.write(encodeText(this.creator));
  131. bout.write(encode("\n"));
  132. }
  133. bout.write(encode("/Producer "));
  134. bout.write(encodeText(this.producer));
  135. bout.write(encode("\n"));
  136. // creation date in form (D:YYYYMMDDHHmmSSOHH'mm')
  137. if(creationDate==null) {
  138. creationDate = new Date();
  139. }
  140. final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  141. final String str = sdf.format(creationDate) + "+00'00'";
  142. bout.write(encode("/CreationDate "));
  143. bout.write(encodeString("D:" + str));
  144. bout.write(encode("\n>>\nendobj\n"));
  145. } catch (IOException ioe) {
  146. getDocumentSafely().getLogger().error("Ignored I/O exception", ioe);
  147. }
  148. return bout.toByteArray();
  149. }
  150. }