Пример #1
0
        /* (non-Javadoc)
         * @see com.itextpdf.pdfa.checker.PdfA1Checker#checkMetaData(com.itextpdf.kernel.pdf.PdfDictionary)
         */
        protected override void CheckMetaData(PdfDictionary catalog)
        {
            base.CheckMetaData(catalog);
            XMPMeta xmpMeta;

            try {
                xmpMeta = XMPMetaFactory.ParseFromBuffer(catalog.GetAsStream(PdfName.Metadata).GetBytes());
                String docFileName = xmpMeta.GetPropertyString(ZugferdXMPUtil.ZUGFERD_SCHEMA_NS, ZugferdXMPUtil.ZUGFERD_DOCUMENT_FILE_NAME
                                                               );
                foreach (PdfDictionary attachment in attachments)
                {
                    if ((attachment.GetAsString(PdfName.UF) != null && docFileName.Equals(attachment.GetAsString(PdfName.UF).ToString
                                                                                              ())) || (attachment.GetAsString(PdfName.F) != null && docFileName.Equals(attachment.GetAsString(PdfName
                                                                                                                                                                                              .F).ToString())))
                    {
                        PdfName relationship = attachment.GetAsName(PdfName.AFRelationship);
                        if (!PdfName.Alternative.Equals(relationship))
                        {
                            throw new ZugferdConformanceException(ZugferdConformanceException.AFRelationshipValueShallbeAlternative);
                        }
                        break;
                    }
                }
            }
            catch (XMPException e) {
                throw new PdfException(e);
            }
        }
Пример #2
0
        /// <summary>Open a PDF/A document in stamping mode.</summary>
        /// <param name="reader">PDF reader.</param>
        /// <param name="writer">PDF writer.</param>
        /// <param name="properties">properties of the stamping process</param>
        public PdfADocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
            : base(reader, writer, properties)
        {
            byte[] existingXmpMetadata = GetXmpMetadata();
            if (existingXmpMetadata == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.DOCUMENT_TO_READ_FROM_SHALL_BE_A_PDFA_CONFORMANT_FILE_WITH_VALID_XMP_METADATA
                                                   );
            }
            XMPMeta meta;

            try {
                meta = XMPMetaFactory.ParseFromBuffer(existingXmpMetadata);
            }
            catch (XMPException) {
                throw new PdfAConformanceException(PdfAConformanceException.DOCUMENT_TO_READ_FROM_SHALL_BE_A_PDFA_CONFORMANT_FILE_WITH_VALID_XMP_METADATA
                                                   );
            }
            PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.GetConformanceLevel(meta);

            if (conformanceLevel == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.DOCUMENT_TO_READ_FROM_SHALL_BE_A_PDFA_CONFORMANT_FILE_WITH_VALID_XMP_METADATA
                                                   );
            }
            SetChecker(conformanceLevel);
        }
Пример #3
0
        public PdfADocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
            : base(reader, writer, properties)
        {
            byte[] existingXmpMetadata = GetXmpMetadata();
            if (existingXmpMetadata == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.DocumentToReadFromShallBeAPdfAConformantFileWithValidXmpMetadata
                                                   );
            }
            XMPMeta meta;

            try {
                meta = XMPMetaFactory.ParseFromBuffer(existingXmpMetadata);
            }
            catch (XMPException) {
                throw new PdfAConformanceException(PdfAConformanceException.DocumentToReadFromShallBeAPdfAConformantFileWithValidXmpMetadata
                                                   );
            }
            PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.GetConformanceLevel(meta);

            if (conformanceLevel == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.DocumentToReadFromShallBeAPdfAConformantFileWithValidXmpMetadata
                                                   );
            }
            SetChecker(conformanceLevel);
        }
Пример #4
0
        private void ConvertToPdfa(Job job)
        {
            if (job.Profile.OutputFormat.IsPdfA())
            {
                var sourcePdf = job.IntermediatePdfFile;

                if (string.IsNullOrEmpty(sourcePdf))
                {
                    return;
                }

                var targetPdf = AddTailToFile(sourcePdf, "_pdfa");

                var version          = DeterminePdfVersionAsEnum(job.Profile);
                var writerProperties = new WriterProperties();
                writerProperties.SetPdfVersion(version);

                byte[] resource;
                //Set ICC Profile according to the color model
                switch (job.Profile.PdfSettings.ColorModel)
                {
                case ColorModel.Cmyk:
                    resource = GhostscriptResources.WebCoatedFOGRA28;
                    break;

                case ColorModel.Gray:
                    resource = GhostscriptResources.ISOcoated_v2_grey1c_bas;
                    break;

                default:
                    resource = GhostscriptResources.eciRGB_v2;
                    break;
                }

                try
                {
                    using (var icc = new MemoryStream(resource))
                    {
                        var document = new PdfADocument(new PdfWriter(targetPdf, writerProperties), GetConformLevel(job.Profile.OutputFormat), new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc));

                        document.SetTagged();
                        var toCopyDoc = new PdfDocument(new PdfReader(sourcePdf));
                        toCopyDoc.CopyPagesTo(1, toCopyDoc.GetNumberOfPages(), document);
                        var metaData = XMPMetaFactory.ParseFromBuffer(toCopyDoc.GetXmpMetadata());
                        document.SetXmpMetadata(metaData);
                        toCopyDoc.Close();
                        document.Close();

                        File.Delete(sourcePdf);
                        job.IntermediatePdfFile = targetPdf;
                    }
                }
                catch (PdfAConformanceException ex)
                {
                    throw new ProcessingException("One of the used pdfs does not conform to the PDF-A specification", ErrorCode.Processing_ConformanceMismatch, ex);
                }
            }
        }
Пример #5
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) {
         }
     }
 }
Пример #6
0
        private byte[] RemoveAlwaysDifferentEntries(byte[] cmpBytes)
        {
            XMPMeta xmpMeta = XMPMetaFactory.ParseFromBuffer(cmpBytes);

            XMPUtils.RemoveProperties(xmpMeta, XMPConst.NS_XMP, PdfConst.CreateDate, true, true);
            XMPUtils.RemoveProperties(xmpMeta, XMPConst.NS_XMP, PdfConst.ModifyDate, true, true);
            XMPUtils.RemoveProperties(xmpMeta, XMPConst.NS_XMP, PdfConst.MetadataDate, true, true);
            XMPUtils.RemoveProperties(xmpMeta, XMPConst.NS_PDF, PdfConst.Producer, true, true);
            cmpBytes = XMPMetaFactory.SerializeToBuffer(xmpMeta, new SerializeOptions(SerializeOptions.SORT));
            return(cmpBytes);
        }
Пример #7
0
 public virtual void MetadataReadingInEncryptedDoc()
 {
     PdfReader reader = new PdfReader(sourceFolder + "encryptedWithPlainMetadata.pdf", new ReaderProperties().SetPassword
         (OWNER));
     PdfDocument doc = new PdfDocument(reader);
     XMPMeta xmpMeta = XMPMetaFactory.ParseFromBuffer(doc.GetXmpMetadata());
     XMPProperty creatorToolXmp = xmpMeta.GetProperty(XMPConst.NS_XMP, "CreatorTool");
     doc.Close();
     NUnit.Framework.Assert.IsNotNull(creatorToolXmp);
     NUnit.Framework.Assert.AreEqual("iText 7", creatorToolXmp.GetValue());
 }
Пример #8
0
        /// <summary>
        /// Performs the XMP data extraction, adding found values to the specified instance of
        /// <see cref="Com.Drew.Metadata.Metadata"/>
        /// .
        /// <p/>
        /// The extraction is done with Adobe's XMPCore library.
        /// </summary>
        public virtual void Extract(sbyte[] xmpBytes, Com.Drew.Metadata.Metadata metadata)
        {
            XmpDirectory directory = metadata.GetOrCreateDirectory <XmpDirectory>();

            try
            {
                XMPMeta xmpMeta = XMPMetaFactory.ParseFromBuffer(xmpBytes);
                ProcessXmpTags(directory, xmpMeta);
            }
            catch (XMPException e)
            {
                directory.AddError("Error processing XMP data: " + e.Message);
            }
        }
Пример #9
0
        /// <summary>
        /// Performs the XMP data extraction, adding found values to the specified instance of
        /// <see cref="Com.Drew.Metadata.Metadata"/>
        /// .
        /// <p>
        /// The extraction is done with Adobe's XMPCore library.
        /// </summary>
        public virtual void Extract([NotNull] sbyte[] xmpBytes, [NotNull] Com.Drew.Metadata.Metadata metadata)
        {
            XmpDirectory directory = new XmpDirectory();

            try
            {
                XMPMeta xmpMeta = XMPMetaFactory.ParseFromBuffer(xmpBytes);
                ProcessXmpTags(directory, xmpMeta);
            }
            catch (XMPException e)
            {
                directory.AddError("Error processing XMP data: " + e.Message);
            }
            if (!directory.IsEmpty())
            {
                metadata.AddDirectory(directory);
            }
        }
Пример #10
0
        public async Task ShouldConvertToPdfA2B(string level, string part, string conformance)
        {
            var path    = $"/pdfa";
            var fixture = $"{Program.TestProjectPath}/Fixtures/dummy.pdf";
            var pdfFile = await File.ReadAllBytesAsync(fixture);

            var formData = new PdfFormData().WithConformanceLevel(level).Upload(pdfFile);
            var response = await _client.PostAsync(path, formData);

            var stream = await response.Content.ReadAsStreamAsync();

            var pdfa             = new PdfADocument(new PdfReader(stream), new PdfWriter(new MemoryStream()));
            var xmpMetadataBytes = pdfa.GetXmpMetadata();
            var xmpMetadata      = XMPMetaFactory.ParseFromBuffer(xmpMetadataBytes);
            var conformanceLevel = PdfAConformanceLevel.GetConformanceLevel(xmpMetadata);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(part, conformanceLevel.GetPart());
            Assert.Equal(conformance, conformanceLevel.GetConformance());
        }