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 package org.apache.pdfbox.pdmodel.common; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 import javax.xml.transform.TransformerException; 23 24 import org.apache.jempbox.xmp.XMPMetadata; 25 import org.apache.pdfbox.cos.COSStream; 26 27 import org.apache.pdfbox.pdmodel.PDDocument; 28 29 /** 30 * This class represents metadata for various objects in a PDF document. 31 * 32 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 33 * @version $Revision: 1.3 $ 34 */ 35 public class PDMetadata extends PDStream 36 { 37 38 /** 39 * This will create a new PDMetadata object. 40 * 41 * @param document The document that the stream will be part of. 42 */ 43 public PDMetadata( PDDocument document ) 44 { 45 super( document ); 46 getStream().setName( "Type", "Metadata" ); 47 getStream().setName( "Subtype", "XML" ); 48 } 49 50 /** 51 * Constructor. Reads all data from the input stream and embeds it into the 52 * document, this will close the InputStream. 53 * 54 * @param doc The document that will hold the stream. 55 * @param str The stream parameter. 56 * @param filtered True if the stream already has a filter applied. 57 * @throws IOException If there is an error creating the stream in the document. 58 */ 59 public PDMetadata( PDDocument doc, InputStream str, boolean filtered ) throws IOException 60 { 61 super( doc, str, filtered ); 62 getStream().setName( "Type", "Metadata" ); 63 getStream().setName( "Subtype", "XML" ); 64 } 65 66 /** 67 * Constructor. 68 * 69 * @param str The stream parameter. 70 */ 71 public PDMetadata( COSStream str ) 72 { 73 super( str ); 74 } 75 76 /** 77 * Extract the XMP metadata and create and build an in memory object. 78 * To persist changes back to the PDF you must call importXMPMetadata. 79 * 80 * @return A parsed XMP object. 81 * 82 * @throws IOException If there is an error parsing the XMP data. 83 */ 84 public XMPMetadata exportXMPMetadata() throws IOException 85 { 86 return XMPMetadata.load( createInputStream() ); 87 } 88 89 /** 90 * Import an XMP stream into the PDF document. 91 * 92 * @param xmp The XMP data. 93 * 94 * @throws IOException If there is an error generating the XML document. 95 * @throws TransformerException If there is an error generating the XML document. 96 */ 97 public void importXMPMetadata( XMPMetadata xmp ) 98 throws IOException, TransformerException 99 { 100 xmp.save( createOutputStream() ); 101 } 102 }