Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Date;
  22. import javax.xml.transform.TransformerConfigurationException;
  23. import org.xml.sax.SAXException;
  24. import org.apache.xmlgraphics.xmp.Metadata;
  25. import org.apache.xmlgraphics.xmp.XMPSerializer;
  26. import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
  27. import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
  28. import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
  29. import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
  30. import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFAdapter;
  31. import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFSchema;
  32. import org.apache.xmlgraphics.xmp.schemas.pdf.PDFAAdapter;
  33. import org.apache.xmlgraphics.xmp.schemas.pdf.PDFAXMPSchema;
  34. /**
  35. * Special PDFStream for Metadata.
  36. * @since PDF 1.4
  37. */
  38. public class PDFMetadata extends PDFStream {
  39. private Metadata xmpMetadata;
  40. private boolean readOnly = true;
  41. /**
  42. * @param xmp xmp metadata
  43. * @param readOnly true if read only
  44. * @see org.apache.fop.pdf.PDFObject#PDFObject()
  45. */
  46. public PDFMetadata(Metadata xmp, boolean readOnly) {
  47. super();
  48. if (xmp == null) {
  49. throw new NullPointerException(
  50. "The parameter for the XMP Document must not be null");
  51. }
  52. this.xmpMetadata = xmp;
  53. this.readOnly = readOnly;
  54. }
  55. /** {@inheritDoc} */
  56. protected String getDefaultFilterName() {
  57. return PDFFilterList.METADATA_FILTER;
  58. }
  59. /**
  60. * @return the XMP metadata
  61. */
  62. public Metadata getMetadata() {
  63. return this.xmpMetadata;
  64. }
  65. /**
  66. * overload the base object method so we don't have to copy
  67. * byte arrays around so much
  68. * {@inheritDoc}
  69. */
  70. public int output(java.io.OutputStream stream)
  71. throws java.io.IOException {
  72. int length = super.output(stream);
  73. this.xmpMetadata = null; //Release DOM when it's not used anymore
  74. return length;
  75. }
  76. /** {@inheritDoc} */
  77. protected void outputRawStreamData(OutputStream out) throws IOException {
  78. try {
  79. XMPSerializer.writeXMPPacket(xmpMetadata, out, this.readOnly);
  80. } catch (TransformerConfigurationException tce) {
  81. throw new IOException("Error setting up Transformer for XMP stream serialization: "
  82. + tce.getMessage());
  83. } catch (SAXException saxe) {
  84. throw new IOException("Error while serializing XMP stream: "
  85. + saxe.getMessage());
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. protected void populateStreamDict(Object lengthEntry) {
  90. final String filterEntry = getFilterList().buildFilterDictEntries();
  91. if (getDocumentSafely().getProfile().getPDFAMode().isPDFA1LevelB()
  92. && filterEntry != null && filterEntry.length() > 0) {
  93. throw new PDFConformanceException(
  94. "The Filter key is prohibited when PDF/A-1 is active");
  95. }
  96. put("Type", new PDFName("Metadata"));
  97. put("Subtype", new PDFName("XML"));
  98. super.populateStreamDict(lengthEntry);
  99. }
  100. /**
  101. * Creates an XMP document based on the settings on the PDF Document.
  102. * @param pdfDoc the PDF Document
  103. * @return the requested XMP metadata
  104. */
  105. public static Metadata createXMPFromPDFDocument(PDFDocument pdfDoc) {
  106. Metadata meta = new Metadata();
  107. PDFInfo info = pdfDoc.getInfo();
  108. PDFRoot root = pdfDoc.getRoot();
  109. //Set creation date if not available, yet
  110. if (info.getCreationDate() == null) {
  111. Date d = new Date();
  112. info.setCreationDate(d);
  113. }
  114. //Important: Acrobat 7's preflight check for PDF/A-1b wants the creation date in the Info
  115. //object and in the XMP metadata to have the same timezone or else it shows a validation
  116. //error even if the times are essentially equal.
  117. //Dublin Core
  118. DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
  119. if (info.getAuthor() != null) {
  120. dc.addCreator(info.getAuthor());
  121. }
  122. if (info.getTitle() != null) {
  123. dc.setTitle(info.getTitle());
  124. }
  125. if (info.getSubject() != null) {
  126. //Subject maps to dc:description["x-default"] as per ISO-19005-1:2005/Cor.1:2007
  127. dc.setDescription(null, info.getSubject());
  128. }
  129. if (root.getLanguage() != null) {
  130. //Note: No check is performed to make sure the value is valid RFC 3066!
  131. dc.addLanguage(root.getLanguage());
  132. }
  133. dc.addDate(info.getCreationDate());
  134. //Somewhat redundant but some PDF/A checkers issue a warning without this.
  135. dc.setFormat("application/pdf");
  136. //PDF/A identification
  137. PDFAMode pdfaMode = pdfDoc.getProfile().getPDFAMode();
  138. if (pdfaMode.isPDFA1LevelB()) {
  139. PDFAAdapter pdfa = PDFAXMPSchema.getAdapter(meta);
  140. pdfa.setPart(1);
  141. if (pdfaMode == PDFAMode.PDFA_1A) {
  142. pdfa.setConformance("A"); //PDF/A-1a
  143. } else {
  144. pdfa.setConformance("B"); //PDF/A-1b
  145. }
  146. }
  147. //XMP Basic Schema
  148. XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(meta);
  149. xmpBasic.setCreateDate(info.getCreationDate());
  150. PDFProfile profile = pdfDoc.getProfile();
  151. if (info.getModDate() != null) {
  152. xmpBasic.setModifyDate(info.getModDate());
  153. } else if (profile.isModDateRequired()) {
  154. //if modify date is needed but none is in the Info object, use creation date
  155. xmpBasic.setModifyDate(info.getCreationDate());
  156. }
  157. if (info.getCreator() != null) {
  158. xmpBasic.setCreatorTool(info.getCreator());
  159. }
  160. AdobePDFAdapter adobePDF = AdobePDFSchema.getAdapter(meta);
  161. if (info.getKeywords() != null) {
  162. adobePDF.setKeywords(info.getKeywords());
  163. }
  164. if (info.getProducer() != null) {
  165. adobePDF.setProducer(info.getProducer());
  166. }
  167. adobePDF.setPDFVersion(pdfDoc.getPDFVersionString());
  168. return meta;
  169. }
  170. /**
  171. * Updates the values in the Info object from the XMP metadata according to the rules defined
  172. * in PDF/A-1 (ISO 19005-1:2005)
  173. * @param meta the metadata
  174. * @param info the Info object
  175. */
  176. public static void updateInfoFromMetadata(Metadata meta, PDFInfo info) {
  177. DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
  178. info.setTitle(dc.getTitle());
  179. String[] creators = dc.getCreators();
  180. if (creators != null && creators.length > 0) {
  181. info.setAuthor(creators[0]);
  182. } else {
  183. info.setAuthor(null);
  184. }
  185. //dc:description["x-default"] maps to Subject as per ISO-19005-1:2005/Cor.1:2007
  186. info.setSubject(dc.getDescription());
  187. AdobePDFAdapter pdf = AdobePDFSchema.getAdapter(meta);
  188. info.setKeywords(pdf.getKeywords());
  189. info.setProducer(pdf.getProducer());
  190. XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(meta);
  191. info.setCreator(xmpBasic.getCreatorTool());
  192. Date d;
  193. d = xmpBasic.getCreateDate();
  194. xmpBasic.setCreateDate(d); //To make Adobe Acrobat happy (bug filed with Adobe)
  195. //Adobe Acrobat doesn't like it when the xmp:CreateDate has a different timezone
  196. //than Info/CreationDate
  197. info.setCreationDate(d);
  198. d = xmpBasic.getModifyDate();
  199. if (d != null) { //ModifyDate is only required for PDF/X
  200. xmpBasic.setModifyDate(d);
  201. info.setModDate(d);
  202. }
  203. }
  204. }