Beispiel #1
0
        private static DateTime?ToDate(PdfString pdfstr)
        {
            string strRet = null;

            if (pdfstr != null)
            {
                strRet = pdfstr.ToUnicodeString();

                return(PdfDate.Decode(strRet));
            }
            return(null);
        }
Beispiel #2
0
        ///// <summary>
        ///// Fonts in file
        ///// </summary>
        //internal List<Font> Fonts { get; set; }

        public override string ToString()
        => new StringBuilder($"PDF description:")
        .Append($"\r\n    - Title: {Title}")
        .Append($"\r\n    - Author: {Author}")
        .Append($"\r\n    - Subject: {Subject}")
        .Append($"\r\n    - Keywords: {Keywords}")
        .Append($"\r\n    - Creator: {Creator}")
        .Append($"\r\n    - Producer: {Producer}")
        .Append($"\r\n    - Version: {Version}")
        .Append($"\r\n    - Creatioan date: " +
                $"{(string.IsNullOrEmpty(CreationDate) ? string.Empty : PdfDate.Decode(CreationDate).ToString())}")
        .Append($"\r\n    - Modification date: " +
                $"{(string.IsNullOrEmpty(ModificationDate) ? string.Empty : PdfDate.Decode(ModificationDate).ToString())}")
        .ToString();
Beispiel #3
0
        /// <summary>
        /// Prepares an
        /// <see cref="PdfPKCS7"/>
        /// instance for the given signature.
        /// This method handles signature parsing and might throw an exception if
        /// signature is malformed.
        /// <p>
        /// The returned
        /// <see cref="PdfPKCS7"/>
        /// can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </p>
        /// Prepared
        /// <see cref="PdfPKCS7"/>
        /// instance calculates digest based on signature's /ByteRange entry.
        /// In order to check that /ByteRange is properly defined and given signature indeed covers the current PDF document
        /// revision please use
        /// <see cref="SignatureCoversWholeDocument(System.String)"/>
        /// method.
        /// </summary>
        /// <param name="signatureFieldName">the signature field name</param>
        /// <param name="securityProvider">the security provider or null for the default provider</param>
        /// <returns>
        /// a
        /// <see cref="PdfPKCS7"/>
        /// instance which can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </returns>
        public virtual PdfPKCS7 ReadSignatureData(String signatureFieldName)
        {
            PdfSignature signature = GetSignature(signatureFieldName);

            if (signature == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = signature.GetSubFilter();
                PdfString contents = signature.GetContents();
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = signature.GetPdfObject().GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = signature.GetPdfObject().GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, signature);
                PdfString date = signature.GetDate();
                if (date != null)
                {
                    pk.SetSignDate(PdfDate.Decode(date.ToString()));
                }
                String signName = signature.GetName();
                pk.SetSignName(signName);
                String reason = signature.GetReason();
                if (reason != null)
                {
                    pk.SetReason(reason);
                }
                String location = signature.GetLocation();
                if (location != null)
                {
                    pk.SetLocation(location);
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Returns a PdfObject that can be used as the value of a Collection Item.
        /// </summary>
        public PdfObject GetValue(string v)
        {
            switch (FieldType)
            {
            case TEXT:
                return(new PdfString(v, TEXT_UNICODE));

            case DATE:
                return(new PdfDate(PdfDate.Decode(v)));

            case NUMBER:
                return(new PdfNumber(v));
            }
            throw new InvalidOperationException($"{v} is not an acceptable value for the field {Get(PdfName.N)}");
        }
        /**
         * Returns a PdfObject that can be used as the value of a Collection Item.
         * @param String    value   the value that has to be changed into a PdfObject (PdfString, PdfDate or PdfNumber)
         */
        virtual public PdfObject GetValue(String v)
        {
            switch (fieldType)
            {
            case TEXT:
                return(new PdfString(v, PdfObject.TEXT_UNICODE));

            case DATE:
                return(new PdfDate(PdfDate.Decode(v)));

            case NUMBER:
                return(new PdfNumber(v));
            }
            throw new InvalidOperationException(MessageLocalization.GetComposedMessage("1.is.not.an.acceptable.value.for.the.field.2", v, Get(PdfName.N).ToString()));
        }
        public virtual void DontAddDateItemToAnotherSubTypeFieldTest()
        {
            String        fieldName         = "fieldName";
            String        timeValueAsString = "D:19860426012347+04'00'";
            PdfDictionary pdfObject         = new PdfDictionary();

            pdfObject.Put(PdfName.Subtype, PdfName.F);
            PdfCollectionField  field  = new PdfCollectionField(pdfObject);
            PdfCollectionSchema schema = new PdfCollectionSchema();

            schema.AddField(fieldName, field);
            PdfCollectionItem item = new PdfCollectionItem(schema);

            item.AddItem(fieldName, new PdfDate(PdfDate.Decode(timeValueAsString)));
            NUnit.Framework.Assert.IsNull(item.GetPdfObject().GetAsString(new PdfName(fieldName)));
        }
        public virtual void AddDateItemTest()
        {
            String        fieldName         = "fieldName";
            String        timeValueAsString = "D:19860426012347+04'00'";
            PdfDictionary pdfObject         = new PdfDictionary();

            pdfObject.Put(PdfName.Subtype, PdfName.D);
            PdfCollectionField  field  = new PdfCollectionField(pdfObject);
            PdfCollectionSchema schema = new PdfCollectionSchema();

            schema.AddField(fieldName, field);
            PdfCollectionItem item = new PdfCollectionItem(schema);

            item.AddItem(fieldName, new PdfDate(PdfDate.Decode(timeValueAsString)));
            NUnit.Framework.Assert.IsTrue(((PdfString)field.GetValue(timeValueAsString)).GetValue().StartsWith("D:1986"
                                                                                                               ));
        }
Beispiel #8
0
 private string GetModifyDatePdf(string fullPath)
 {
     try
     {
         using (PdfReader reader = new PdfReader(fullPath))
         {
             if (reader.Info.ContainsKey("ModDate"))
             {
                 return(PdfDate.Decode(reader.Info["ModDate"]).ToString());
             }
             return(blank);
         }
     }
     catch
     {
         return(cannotOpen);
     }
 }
Beispiel #9
0
        public virtual PdfObject GetValue(String value)
        {
            switch (subType)
            {
            case TEXT: {
                return(new PdfString(value));
            }

            case DATE: {
                return(new PdfDate(PdfDate.Decode(value)).GetPdfObject());
            }

            case NUMBER: {
                return(new PdfNumber(System.Double.Parse(value.Trim(), System.Globalization.CultureInfo.InvariantCulture)));
            }
            }
            throw new PdfException(PdfException._1IsNotAnAcceptableValueForTheField2).SetMessageParams(value, GetPdfObject
                                                                                                           ().GetAsName(PdfName.N).GetValue());
        }
Beispiel #10
0
        public string Pdf_get_metadata(string path)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(path));
            Document    doc    = new Document(pdfDoc);
            PdfFont     pdfFont;

            pdfFont = null;

            PdfDictionary infoDictionary = pdfDoc.GetTrailer().GetAsDictionary(PdfName.Info);
            string        metadatastring = $"Metadata: {Environment.NewLine}";

            foreach (PdfName key in infoDictionary.KeySet())
            {
                string keytext = infoDictionary.GetAsString(key).ToUnicodeString();
                if (key == PdfName.CreationDate | key == PdfName.ModDate)
                {
                    DateTime date = PdfDate.Decode(keytext);
                    keytext = date.ToString("dd/MM/yyyy HH:mm:ss");
                }

                string cleankey = ($"{ key }").Replace("/", "");

                metadatastring += $"{cleankey}: {keytext}{Environment.NewLine}{Environment.NewLine}";
            }
            metadatastring += $"-----------------{Environment.NewLine}";

            HashSet <string> fontinfo = Find_fonts(path);

            if (fontinfo != null && fontinfo.Count > 0)
            {
                metadatastring += $"OCR present because pdf has {fontinfo.Count} fonts.{Environment.NewLine}-----------------{Environment.NewLine}";
            }
            else
            {
                metadatastring += $"OCR NOT present because pdf has no fonts.{Environment.NewLine}-----------------{Environment.NewLine}";
            }


            metadatastring += $"Pages: {pdfDoc.GetNumberOfPages().ToString()}" + Environment.NewLine;
            return(metadatastring);
        }
        public static PDFFileInfo GetPDFMetaData(Stream pdfStream, string filePath, bool deepinfo)
        {
            var fileInfo = new PDFFileInfo();

            fileInfo.FilePath = filePath;
            fileInfo.FileName = System.IO.Path.GetFileName(filePath);
            if (pdfStream.Length == 0)
            {
                fileInfo.ErrorMessages.Add("Zero length file");
                return(fileInfo);
            }

            try
            {
                pdfStream.Position = 0; // Ensure that we are at the start
            }
            catch (NotSupportedException) { }

            try
            {
                // Note: PdfReader Dispose closes the stream...
                using (PdfReader reader = new PdfReader(pdfStream))
                {
                    fileInfo.PDFVersion = reader.PdfVersion;
                    fileInfo.PageCount  = reader.NumberOfPages;
                    fileInfo.FileSize   = reader.FileLength;
                    if (reader.Info != null)
                    {
                        try
                        {
                            if (reader.Info.ContainsKey("CreationDate"))
                            {
                                fileInfo.CreationDate = PdfDate.Decode(reader.Info["CreationDate"]);
                            }
                            if (reader.Info.ContainsKey("ModDate"))
                            {
                                fileInfo.ModDate = PdfDate.Decode(reader.Info["ModDate"]);
                            }
                        }
                        catch (Exception ex)
                        {
                            fileInfo.ErrorMessages.Add($"PdfDate Decode {ex.Message.Replace(',',' ')}");
                        }
                        if (reader.Info.ContainsKey("Creator"))
                        {
                            fileInfo.Creator = reader.Info["Creator"];
                        }
                        if (reader.Info.ContainsKey("Producer"))
                        {
                            fileInfo.Producer = reader.Info["Producer"];
                        }
                    }

                    if (deepinfo)
                    {
                        var parser = new PdfReaderContentParser(reader);
                        ImageRenderListener listener = null;

                        for (var i = 1; i <= reader.NumberOfPages; i++)
                        {
                            var page = new PDFPageInfo();
                            try
                            {
                                parser.ProcessContent(i, (listener = new ImageRenderListener(fileInfo.ErrorMessages)));
                            }
                            catch (Exception ex)
                            {
                                fileInfo.ErrorMessages.Add($"Page {i} Image Processing Exception: {ex.Message.Replace(',', ' ')}");
                            }

                            try
                            {
                                var pageText = PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy());
                                page.TextCharacters += pageText.Length;
                            }
                            catch (System.ArgumentException ex)
                            {
                                fileInfo.ErrorMessages.Add($"Page {i} Text Extraction Exception {ex.Message.Replace(',', ' ')}");
                            }

                            page.PageNum    = i;
                            page.ImageCount = listener.Images.Count;
                            page.Images     = listener.Images;
                            for (int j = 0; j < page.ImageCount; j++)
                            {
                                page.Images[j].ImageNum = j + 1;
                                page.ImageBytes        += page.Images[j].ImageBytes;
                            }

                            fileInfo.Pages.Add(page);
                            fileInfo.ImageCount     += page.ImageCount;
                            fileInfo.ImageBytes     += page.ImageBytes;
                            fileInfo.TextCharacters += page.TextCharacters;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                fileInfo.ErrorMessages.Add(ex.Message);
            }

            return(fileInfo);
        }
Beispiel #12
0
        /// <summary>Verifies a signature.</summary>
        /// <remarks>
        /// Verifies a signature. Further verification can be done on the returned
        /// <see cref="PdfPKCS7"/>
        /// object.
        /// </remarks>
        /// <param name="name">the signature field name</param>
        /// <param name="provider">the provider or null for the default provider</param>
        /// <returns>PdfPKCS7 object to continue the verification</returns>
        public virtual PdfPKCS7 VerifySignature(String name)
        {
            PdfDictionary v = GetSignatureDictionary(name);

            if (v == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = v.GetAsName(PdfName.SubFilter);
                PdfString contents = v.GetAsString(PdfName.Contents);
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = v.GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = v.GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, v);
                PdfString str = v.GetAsString(PdfName.M);
                if (str != null)
                {
                    pk.SetSignDate(PdfDate.Decode(str.ToString()));
                }
                PdfObject obj = v.Get(PdfName.Name);
                if (obj != null)
                {
                    if (obj.IsString())
                    {
                        pk.SetSignName(((PdfString)obj).ToUnicodeString());
                    }
                    else
                    {
                        if (obj.IsName())
                        {
                            pk.SetSignName(((PdfName)obj).GetValue());
                        }
                    }
                }
                str = v.GetAsString(PdfName.Reason);
                if (str != null)
                {
                    pk.SetReason(str.ToUnicodeString());
                }
                str = v.GetAsString(PdfName.Location);
                if (str != null)
                {
                    pk.SetLocation(str.ToUnicodeString());
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }
Beispiel #13
0
        public static void CheckForXMPMetadataUpdateStrings(string file, Metadata metadata, OutputFormat format, bool encryption, string ownerPassword)
        {
            if (encryption)
            {
                file = GetDecryptedFile(file, ownerPassword);
            }

            var content = File.ReadAllText(file);
            var regex   = new Regex(@"(<.xpacket begin=[\s\S]*?<.xpacket end=.+>)");
            var matches = regex.Matches(content);

            string xmlString;

            Assert.DoesNotThrow(() => { xmlString = matches[matches.Count - 1].ToString(); },
                                "Missing XMP Metadata starting and ending  with \'<?xpacket\'.");
            xmlString = matches[matches.Count - 1].ToString();

            //Check if xmp structure is valid
            var doc = new XmlDocument();

            doc.LoadXml(xmlString); //Throws XmlException
            doc.Schemas = new XmlSchemaSet();

            var reader = new PdfReader(file);

            var nsmgr = new XmlNamespaceManager(doc.NameTable);

            nsmgr.AddNamespace("pdfaid", "http://www.aiim.org/pdfa/ns/id/");
            nsmgr.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
            nsmgr.AddNamespace("xmp", "http://ns.adobe.com/xap/1.0/");
            nsmgr.AddNamespace("xmpMM", "http://ns.adobe.com/xap/1.0/mm/");
            nsmgr.AddNamespace("pdf", "http://ns.adobe.com/pdf/1.3/");

            var node = doc.SelectSingleNode("//pdfaid:part", nsmgr);

            Assert.NotNull(node, "PDF/A id part is not set.");
            var part = format == OutputFormat.PdfA1B ? "1" : "2";

            Assert.AreEqual(part, node.InnerText.Trim(), "PDF/A id part is not set properly.");

            node = doc.SelectSingleNode("//pdfaid:conformance", nsmgr);
            Assert.NotNull(node, "PDF/A id conformance is not set.");
            Assert.AreEqual("B", node.InnerText.Trim(), "PDF/A id conformance is not set properly.");

            node = doc.SelectSingleNode("//xmp:CreateDate", nsmgr);
            Assert.NotNull(node, "Creation date is not set.");
            var expectedDate = PdfDate.Decode(reader.Info["CreationDate"]);
            var creationDate = DateTime.Parse(node.InnerText.Trim());

            Assert.AreEqual(expectedDate, creationDate, "Creation date is wrong.");

            node = doc.SelectSingleNode("//xmp:ModifyDate", nsmgr);
            Assert.NotNull(node, "Modify date is not set.");
            Assert.IsTrue(Regex.IsMatch(node.InnerText.Trim(), @"[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]([\+\-Z][0-2][0-9]:[0-5][0-9]){0,1}"), "Modify date in wrong format.");
            //same as creation date???

            node = doc.SelectSingleNode("//dc:title/rdf:Alt/rdf:li", nsmgr);
            Assert.NotNull(node, "Title is not set.");
            Assert.AreEqual(metadata.Title, node.InnerText.Trim(), "Title is not set properly.");

            node = doc.SelectSingleNode("//dc:description/rdf:Alt/rdf:li", nsmgr);
            Assert.NotNull(node, "Description is not set.");
            Assert.AreEqual(metadata.Subject, node.InnerText.Trim(), "Description (Subject) is not set properly.");

            node = doc.SelectSingleNode("//dc:creator/rdf:Seq/rdf:li", nsmgr);
            Assert.NotNull(node, "Creator is not set.");
            Assert.AreEqual(metadata.Author, node.InnerText.Trim(), "Creator (Author) is not set properly.");

            node = doc.SelectSingleNode("//pdf:Producer", nsmgr);
            Assert.NotNull(node, "PDF producer is not set.");
            Assert.IsTrue(Regex.IsMatch(node.InnerText.Trim(), "PDFCreator [0-9].[0-9].[0-9].[0-9].*"), "PDF producer is not PDFCreator X.X.X.X.");

            node = doc.SelectSingleNode("//pdf:Keywords", nsmgr);
            Assert.NotNull(node, "PDF Keywords are not set.");
            Assert.AreEqual(metadata.Keywords, node.InnerText.Trim(), "Keywords are not set properly.");

            /*
             * node = doc.SelectSingleNode("//xmpMM:DocumentID", nsmgr);
             * Assert.NotNull(node, "Document ID is not set.");
             * Assert.IsTrue(Regex.IsMatch(node.InnerText.Trim(), @"uuid:[0-9A-Za-z\-]{32,}"), "Document ID in wrong format.");
             * //*/
        }
 public static DateTime ToDateTime(this string str)
 {
     return(PdfDate.Decode(str));
 }