Options for XmpMetaFactory.SerializeToBuffer(IXmpMeta, SerializeOptions). @since 24.01.2006
상속: XmpOptions
예제 #1
0
        /// <summary>
        /// Static method to Serialize the metadata object. For each serialisation, a new XMPSerializer
        /// instance is created, either XMPSerializerRDF or XMPSerializerPlain so thats its possible to 
        /// serialialize the same XMPMeta objects in two threads.
        /// </summary>
        /// <param name="xmp"> a metadata implementation object </param>
        /// <param name="out"> the output stream to Serialize to </param>
        /// <param name="options"> serialization options, can be <code>null</code> for default. </param>
        /// <exception cref="XmpException"> </exception>
        public static void Serialize(XmpMetaImpl xmp, Stream @out, SerializeOptions options) {
            options = options ?? new SerializeOptions();

            // sort the internal data model on demand
            if (options.Sort) {
                xmp.Sort();
            }
            (new XmpSerializerRdf()).Serialize(xmp, @out, options);
        }
예제 #2
0
        /// <summary>
        /// Serializes an <code>XMPMeta</code>-object as RDF into a string.
        /// <em>Note:</em> Encoding is forced to UTF-16 when serializing to a
        /// string to ensure the correctness of &quot;exact packet size&quot;.
        /// </summary>
        /// <param name="xmp"> a metadata implementation object </param>
        /// <param name="options"> Options to control the serialization (see
        ///            <seealso cref="SerializeOptions"/>). </param>
        /// <returns> Returns a string containing the serialized RDF. </returns>
        /// <exception cref="XmpException"> on serializsation errors. </exception>
        public static string SerializeToString(XmpMetaImpl xmp, SerializeOptions options) {
            // forces the encoding to be UTF-16 to get the correct string length
            options = options ?? new SerializeOptions();
            options.EncodeUtf16Be = true;

            MemoryStream @out = new MemoryStream(2048);
            Serialize(xmp, @out, options);

            try {
                return Encoding.GetEncoding(options.Encoding).GetString(@out.GetBuffer());
            }
            catch (Exception) {
                // cannot happen as UTF-8/16LE/BE is required to be implemented in
                // Java
                return GetString(@out.GetBuffer());
            }
        }
예제 #3
0
        virtual internal protected void Close(IDictionary<String, String> moreInfo) {
            if (closed) {
                return;
            }
            if (useVp) {
                SetViewerPreferences();
            }
            if (flat) {
                FlatFields();
            }
            if (flatFreeText) {
                FlatFreeTextFields();
            }
            if (flatannotations) {
                FlattenAnnotations();
            }
            AddFieldResources();
            PdfDictionary catalog = reader.Catalog;
            GetPdfVersion().AddToCatalog(catalog);
            PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.ACROFORM), reader.Catalog);
            if (acroFields != null && acroFields.Xfa.Changed) {
                MarkUsed(acroForm);
                if (!flat) {
                    acroFields.Xfa.SetXfa(this);
                }
            }
            if (sigFlags != 0) {
                if (acroForm != null) {
                    acroForm.Put(PdfName.SIGFLAGS, new PdfNumber(sigFlags));
                    MarkUsed(acroForm);
                    MarkUsed(catalog);
                }
            }
            closed = true;
            AddSharedObjectsToBody();
            SetOutlines();
            SetJavaScript();
            AddFileAttachments();
            // [C11] Output Intents
            if (extraCatalog != null) {
                catalog.MergeDifferent(extraCatalog);
            }
            if (openAction != null) {
                catalog.Put(PdfName.OPENACTION, openAction);
            }
            if (pdf.pageLabels != null) {
                catalog.Put(PdfName.PAGELABELS, pdf.pageLabels.GetDictionary(this));
            }
            // OCG
            if (documentOCG.Count > 0) {
                FillOCProperties(false);
                PdfDictionary ocdict = catalog.GetAsDict(PdfName.OCPROPERTIES);
                if (ocdict == null) {
                    reader.Catalog.Put(PdfName.OCPROPERTIES, OCProperties);
                }
                else {
                    ocdict.Put(PdfName.OCGS, OCProperties.Get(PdfName.OCGS));
                    PdfDictionary ddict = ocdict.GetAsDict(PdfName.D);
                    if (ddict == null) {
                        ddict = new PdfDictionary();
                        ocdict.Put(PdfName.D, ddict);
                    }
                    ddict.Put(PdfName.ORDER, OCProperties.GetAsDict(PdfName.D).Get(PdfName.ORDER));
                    ddict.Put(PdfName.RBGROUPS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.RBGROUPS));
                    ddict.Put(PdfName.OFF, OCProperties.GetAsDict(PdfName.D).Get(PdfName.OFF));
                    ddict.Put(PdfName.AS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.AS));
                }
                PdfWriter.CheckPdfIsoConformance(this, PdfIsoKeys.PDFISOKEY_LAYER, OCProperties);
            }
            // metadata
            int skipInfo = -1;
            PdfIndirectReference iInfo = reader.Trailer.GetAsIndirectObject(PdfName.INFO);
            if (iInfo != null) {
                skipInfo = iInfo.Number;
            }
            PdfDictionary oldInfo = reader.Trailer.GetAsDict(PdfName.INFO);
            String producer = null;
            if (oldInfo != null && oldInfo.Get(PdfName.PRODUCER) != null) {
                producer = oldInfo.GetAsString(PdfName.PRODUCER).ToUnicodeString();
            }
            Version version = Version.GetInstance();
            if (producer == null || version.GetVersion.IndexOf(version.Product) == -1) {
                producer = version.GetVersion;
            } else {
                int idx = producer.IndexOf("; modified using");
                StringBuilder buf;
                if (idx == -1)
                    buf = new StringBuilder(producer);
                else
                    buf = new StringBuilder(producer.Substring(0, idx));
                buf.Append("; modified using ");
                buf.Append(version.GetVersion);
                producer = buf.ToString();
            }
            PdfIndirectReference info = null;
            PdfDictionary newInfo = new PdfDictionary();
            if (oldInfo != null) {
                foreach (PdfName key in oldInfo.Keys) {
                    PdfObject value = PdfReader.GetPdfObject(oldInfo.Get(key));
                    newInfo.Put(key, value);
                }
            }
            if (moreInfo != null) {
                foreach (KeyValuePair<string,string> entry in moreInfo) {
                    PdfName keyName = new PdfName(entry.Key);
                    String value = entry.Value;
                    if (value == null)
                        newInfo.Remove(keyName);
                    else
                        newInfo.Put(keyName, new PdfString(value, PdfObject.TEXT_UNICODE));
                }
            }
            PdfDate date = new PdfDate();
            newInfo.Put(PdfName.MODDATE, date);
            newInfo.Put(PdfName.PRODUCER, new PdfString(producer, PdfObject.TEXT_UNICODE));
            if (append) {
                if (iInfo == null) {
                    info = AddToBody(newInfo, false).IndirectReference;
                } else {
                    info = AddToBody(newInfo, iInfo.Number, false).IndirectReference;
                }
            } else {
                info = AddToBody(newInfo, false).IndirectReference;
            }
            // XMP
            byte[] altMetadata = null;
            PdfObject xmpo = PdfReader.GetPdfObject(catalog.Get(PdfName.METADATA));
            if (xmpo != null && xmpo.IsStream()) {
                altMetadata = PdfReader.GetStreamBytesRaw((PRStream)xmpo);
                PdfReader.KillIndirect(catalog.Get(PdfName.METADATA));
            }
            PdfStream xmp = null;
            if (xmpMetadata != null) {
                altMetadata = xmpMetadata;
            } else if (xmpWriter != null) {
                try {
                    MemoryStream baos = new MemoryStream();
                    PdfProperties.SetProducer(xmpWriter.XmpMeta, producer);
                    XmpBasicProperties.SetModDate(xmpWriter.XmpMeta, date.GetW3CDate());
                    XmpBasicProperties.SetMetaDataDate(xmpWriter.XmpMeta, date.GetW3CDate());
                    xmpWriter.Serialize(baos);
                    xmpWriter.Close();
                    xmp = new PdfStream(baos.ToArray());
                } catch (XmpException) {
                    xmpWriter = null;
                }
            }
            if (xmp == null && altMetadata != null) {
                try {
                    MemoryStream baos = new MemoryStream();
                    if (moreInfo == null || xmpMetadata != null) {
                        IXmpMeta xmpMeta = XmpMetaFactory.ParseFromBuffer(altMetadata);

                        PdfProperties.SetProducer(xmpMeta, producer);
                        XmpBasicProperties.SetModDate(xmpMeta, date.GetW3CDate());
                        XmpBasicProperties.SetMetaDataDate(xmpMeta, date.GetW3CDate());

                        SerializeOptions serializeOptions = new SerializeOptions();
                        serializeOptions.Padding = 2000;
                        XmpMetaFactory.Serialize(xmpMeta, baos, serializeOptions);
                    } else {
                        XmpWriter xmpw = CreateXmpWriter(baos, newInfo);
                        xmpw.Close();
                    }
                    xmp = new PdfStream(baos.ToArray());
                } catch (XmpException) {
                    xmp = new PdfStream(altMetadata);
                } catch (IOException) {
                    xmp = new PdfStream(altMetadata);
                }
            }
            if (xmp != null) {
                xmp.Put(PdfName.TYPE, PdfName.METADATA);
                xmp.Put(PdfName.SUBTYPE, PdfName.XML);
                if (crypto != null && !crypto.IsMetadataEncrypted()) {
                    PdfArray ar = new PdfArray();
                    ar.Add(PdfName.CRYPT);
                    xmp.Put(PdfName.FILTER, ar);
                }
                if (append && xmpo != null) {
                    body.Add(xmp, xmpo.IndRef);
                }
                else {
                    catalog.Put(PdfName.METADATA, body.Add(xmp).IndirectReference);
                    MarkUsed(catalog);
                }
            }
            Close(info, skipInfo);
        }
예제 #4
0
 /// <summary>
 /// Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer.
 /// </summary>
 /// <param name="xmp"> a metadata implementation object </param>
 /// <param name="options"> Options to control the serialization (see <seealso cref="SerializeOptions"/>). </param>
 /// <returns> Returns a byte buffer containing the serialized RDF. </returns>
 /// <exception cref="XmpException"> on serializsation errors. </exception>
 public static byte[] SerializeToBuffer(XmpMetaImpl xmp, SerializeOptions options) {
     MemoryStream @out = new MemoryStream(2048);
     Serialize(xmp, @out, options);
     return @out.GetBuffer();
 }
예제 #5
0
        /**
         * Creates an XmpWriter.
         * @param os
         * @param utfEncoding
         * @param extraSpace
         * @throws IOException
         */

        public XmpWriter(Stream os, String utfEncoding, int extraSpace) {
            outputStream = os;
            serializeOptions = new SerializeOptions();
            if (UTF16BE.Equals(utfEncoding) || UTF16.Equals(utfEncoding))
                serializeOptions.EncodeUtf16Be = true;
            else if (UTF16LE.Equals(utfEncoding))
                serializeOptions.EncodeUtf16Le = true;
            serializeOptions.Padding = extraSpace;
            xmpMeta = XmpMetaFactory.Create();
            xmpMeta.ObjectName = XmpConst.TAG_XMPMETA;
            xmpMeta.ObjectName = "";
            try {
                xmpMeta.SetProperty(XmpConst.NS_DC, DublinCoreProperties.FORMAT, "application/pdf");
                xmpMeta.SetProperty(XmpConst.NS_PDF, PdfProperties.PRODUCER, Version.GetInstance().GetVersion);
            }
            catch (XmpException) {}
        }
예제 #6
0
        /// <summary>
        /// The actual serialization.
        /// </summary>
        /// <param name="xmp"> the metadata object to be serialized </param>
        /// <param name="out"> outputStream the output stream to Serialize to </param>
        /// <param name="options"> the serialization options
        /// </param>
        /// <exception cref="XmpException"> If case of wrong options or any other serialization error. </exception>
        public virtual void Serialize(IXmpMeta xmp, Stream @out, SerializeOptions options) {
            try {
                _outputStream = new CountOutputStream(@out);
                _writer = new StreamWriter(_outputStream, Encoding.GetEncoding(options.Encoding));

                _xmp = (XmpMetaImpl) xmp;
                _options = options;
                _padding = options.Padding;

                _writer = new StreamWriter(_outputStream, Encoding.GetEncoding(options.Encoding));

                CheckOptionsConsistence();

                // serializes the whole packet, but don't write the tail yet 
                // and flush to make sure that the written bytes are calculated correctly
                string tailStr = SerializeAsRdf();
                _writer.Flush();

                // adds padding
                AddPadding(tailStr.Length);

                // writes the tail
                Write(tailStr);
                _writer.Flush();

                _outputStream.Close();
            }
            catch (IOException) {
                throw new XmpException("Error writing to the OutputStream", XmpError.UNKNOWN);
            }
        }
예제 #7
0
 /// 
 /// <returns> Returns clone of this SerializeOptions-object with the same options set. </returns>
 public object Clone() {
     try {
         SerializeOptions clone = new SerializeOptions(Options);
         clone.BaseIndent = _baseIndent;
         clone.Indent = _indent;
         clone.Newline = _newline;
         clone.Padding = _padding;
         return clone;
     }
     catch (XmpException) {
         // This cannot happen, the options are already checked in "this" object.
         return null;
     }
 }
예제 #8
0
 /// <summary>
 /// Serializes an <code>XMPMeta</code>-object as RDF into a string. <em>Note:</em> Encoding
 /// is ignored when serializing to a string.
 /// </summary>
 /// <param name="xmp">     a metadata object </param>
 /// <param name="options"> Options to control the serialization (see <seealso cref="SerializeOptions"/>). </param>
 /// <returns> Returns a string containing the serialized RDF. </returns>
 /// <exception cref="XmpException"> on serializsation errors. </exception>
 public static string SerializeToString(IXmpMeta xmp, SerializeOptions options) {
     AssertImplementation(xmp);
     return XmpSerializerHelper.SerializeToString((XmpMetaImpl) xmp, options);
 }
예제 #9
0
 /// <summary>
 /// Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer.
 /// </summary>
 /// <param name="xmp">     a metadata object </param>
 /// <param name="options"> Options to control the serialization (see <seealso cref="SerializeOptions"/>). </param>
 /// <returns> Returns a byte buffer containing the serialized RDF. </returns>
 /// <exception cref="XmpException"> on serializsation errors. </exception>
 public static byte[] SerializeToBuffer(IXmpMeta xmp, SerializeOptions options) {
     AssertImplementation(xmp);
     return XmpSerializerHelper.SerializeToBuffer((XmpMetaImpl) xmp, options);
 }
예제 #10
0
 /// <summary>
 /// Serializes an <code>XMPMeta</code>-object as RDF into an <code>OutputStream</code>.
 /// </summary>
 /// <param name="xmp">     a metadata object </param>
 /// <param name="options"> Options to control the serialization (see <seealso cref="SerializeOptions"/>). </param>
 /// <param name="out">     an <code>OutputStream</code> to write the serialized RDF to. </param>
 /// <exception cref="XmpException"> on serializsation errors. </exception>
 public static void Serialize(IXmpMeta xmp, Stream @out, SerializeOptions options) {
     AssertImplementation(xmp);
     XmpSerializerHelper.Serialize((XmpMetaImpl) xmp, @out, options);
 }