Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PDFMetadata.java 10KB

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