コード例 #1
0
 internal static void AppendMetadataToInfo(byte[] xmpMetadata, PdfDocumentInfo info)
 {
     if (xmpMetadata != null)
     {
         try {
             XMPMeta     meta  = XMPMetaFactory.ParseFromBuffer(xmpMetadata);
             XMPProperty title = meta.GetLocalizedText(XMPConst.NS_DC, PdfConst.Title, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT
                                                       );
             if (title != null)
             {
                 info.SetTitle(title.GetValue());
             }
             String author = FetchArrayIntoString(meta, XMPConst.NS_DC, PdfConst.Creator);
             if (author != null)
             {
                 info.SetAuthor(author);
             }
             // We assume that pdf:keywords has precedence over dc:subject
             XMPProperty keywords = meta.GetProperty(XMPConst.NS_PDF, PdfConst.Keywords);
             if (keywords != null)
             {
                 info.SetKeywords(keywords.GetValue());
             }
             else
             {
                 String keywordsStr = FetchArrayIntoString(meta, XMPConst.NS_DC, PdfConst.Subject);
                 if (keywordsStr != null)
                 {
                     info.SetKeywords(keywordsStr);
                 }
             }
             XMPProperty subject = meta.GetLocalizedText(XMPConst.NS_DC, PdfConst.Description, XMPConst.X_DEFAULT, XMPConst
                                                         .X_DEFAULT);
             if (subject != null)
             {
                 info.SetSubject(subject.GetValue());
             }
             XMPProperty creator = meta.GetProperty(XMPConst.NS_XMP, PdfConst.CreatorTool);
             if (creator != null)
             {
                 info.SetCreator(creator.GetValue());
             }
             XMPProperty producer = meta.GetProperty(XMPConst.NS_PDF, PdfConst.Producer);
             if (producer != null)
             {
                 info.Put(PdfName.Producer, new PdfString(producer.GetValue(), PdfEncodings.UNICODE_BIG));
             }
             XMPProperty trapped = meta.GetProperty(XMPConst.NS_PDF, PdfConst.Trapped);
             if (trapped != null)
             {
                 info.SetTrapped(new PdfName(trapped.GetValue()));
             }
         }
         catch (XMPException) {
         }
     }
 }
コード例 #2
0
        public virtual void NullProducerTest()
        {
            String inputFile = SOURCE_FOLDER + "nullProducer.pdf";

            using (PdfDocument document = new PdfDocument(new PdfReader(inputFile))) {
                PdfDocumentInfo documentInfo = document.GetDocumentInfo();
                NUnit.Framework.Assert.AreEqual(PdfNull.PDF_NULL, documentInfo.GetPdfObject().Get(PdfName.Producer));
                NUnit.Framework.Assert.IsNull(documentInfo.GetProducer());
            }
            using (PdfDocument document_1 = new PdfDocument(new PdfReader(inputFile), new PdfWriter(new MemoryStream()
                                                                                                    ))) {
                PdfDocumentInfo documentInfo = document_1.GetDocumentInfo();
                NUnit.Framework.Assert.IsNotNull(documentInfo.GetPdfObject().Get(PdfName.Producer));
                NUnit.Framework.Assert.IsNotNull(document_1.GetDocumentInfo().GetProducer());
            }
        }
コード例 #3
0
        /// <exception cref="iText.Kernel.XMP.XMPException"/>
        internal static void AppendDocumentInfoToMetadata(PdfDocumentInfo info, XMPMeta xmpMeta)
        {
            PdfDictionary docInfo = info.GetPdfObject();

            if (docInfo != null)
            {
                PdfName   key;
                PdfObject obj;
                String    value;
                foreach (PdfName pdfName in docInfo.KeySet())
                {
                    key = pdfName;
                    obj = docInfo.Get(key);
                    if (obj == null)
                    {
                        continue;
                    }
                    if (obj.IsString())
                    {
                        value = ((PdfString)obj).ToUnicodeString();
                    }
                    else
                    {
                        if (obj.IsName())
                        {
                            value = ((PdfName)obj).GetValue();
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (PdfName.Title.Equals(key))
                    {
                        xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Title, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value);
                    }
                    else
                    {
                        if (PdfName.Author.Equals(key))
                        {
                            foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;"))
                            {
                                if (v.Trim().Length > 0)
                                {
                                    AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Creator, v.Trim(), PropertyOptions.ARRAY_ORDERED
                                                                  );
                                }
                            }
                        }
                        else
                        {
                            if (PdfName.Subject.Equals(key))
                            {
                                xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Description, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value
                                                         );
                            }
                            else
                            {
                                if (PdfName.Keywords.Equals(key))
                                {
                                    foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;"))
                                    {
                                        if (v.Trim().Length > 0)
                                        {
                                            AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Subject, v.Trim(), PropertyOptions.ARRAY);
                                        }
                                    }
                                    xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Keywords, value);
                                }
                                else
                                {
                                    if (PdfName.Creator.Equals(key))
                                    {
                                        xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreatorTool, value);
                                    }
                                    else
                                    {
                                        if (PdfName.Producer.Equals(key))
                                        {
                                            xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Producer, value);
                                        }
                                        else
                                        {
                                            if (PdfName.CreationDate.Equals(key))
                                            {
                                                xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreateDate, PdfDate.GetW3CDate(value));
                                            }
                                            else
                                            {
                                                if (PdfName.ModDate.Equals(key))
                                                {
                                                    xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.ModifyDate, PdfDate.GetW3CDate(value));
                                                }
                                                else
                                                {
                                                    if (PdfName.Trapped.Equals(key))
                                                    {
                                                        xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Trapped, value);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }