Пример #1
0
        protected internal override void CheckPdfStream(PdfStream stream)
        {
            if (stream.ContainsKey(PdfName.F) || stream.ContainsKey(PdfName.FFilter) || stream.ContainsKey(PdfName.FDecodeParams
                                                                                                           ))
            {
                throw new PdfAConformanceException(PdfAConformanceException.StreamObjDictShallNotContainForFFilterOrFDecodeParams
                                                   );
            }
            PdfObject filter = stream.Get(PdfName.Filter);

            if (filter is PdfName)
            {
                if (filter.Equals(PdfName.LZWDecode))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted);
                }
            }
            else
            {
                if (filter is PdfArray)
                {
                    foreach (PdfObject f in ((PdfArray)filter))
                    {
                        if (f.Equals(PdfName.LZWDecode))
                        {
                            throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted);
                        }
                    }
                }
            }
        }
Пример #2
0
        protected internal override void CheckPdfStream(PdfStream stream)
        {
            CheckPdfDictionary(stream);
            if (stream.ContainsKey(PdfName.F) || stream.ContainsKey(PdfName.FFilter) || stream.ContainsKey(PdfName.FDecodeParams
                                                                                                           ))
            {
                throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS
                                                   );
            }
            PdfObject filter = stream.Get(PdfName.Filter);

            if (filter is PdfName)
            {
                if (filter.Equals(PdfName.LZWDecode))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED);
                }
            }
            else
            {
                if (filter is PdfArray)
                {
                    foreach (PdfObject f in ((PdfArray)filter))
                    {
                        if (f.Equals(PdfName.LZWDecode))
                        {
                            throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED);
                        }
                    }
                }
            }
        }
Пример #3
0
 protected internal override void CheckFormXObject(PdfStream form)
 {
     if (IsAlreadyChecked(form))
     {
         return;
     }
     if (form.ContainsKey(PdfName.OPI))
     {
         throw new PdfAConformanceException(PdfAConformanceException.AFormXobjectDictionaryShallNotContainOpiKey);
     }
     if (form.ContainsKey(PdfName.PS))
     {
         throw new PdfAConformanceException(PdfAConformanceException.AFormXobjectDictionaryShallNotContainPSKey);
     }
     if (PdfName.PS.Equals(form.GetAsName(PdfName.Subtype2)))
     {
         throw new PdfAConformanceException(PdfAConformanceException.AFormXobjectDictionaryShallNotContainSubtype2KeyWithAValueOfPS
                                            );
     }
     if (form.ContainsKey(PdfName.SMask))
     {
         throw new PdfAConformanceException(PdfAConformanceException.TheSmaskKeyIsNotAllowedInXobjects);
     }
     if (form.ContainsKey(PdfName.Group) && PdfName.Transparency.Equals(form.GetAsDictionary(PdfName.Group).GetAsName
                                                                            (PdfName.S)))
     {
         throw new PdfAConformanceException(PdfAConformanceException.AGroupObjectWithAnSKeyWithAValueOfTransparencyShallNotBeIncludedInAFormXobject
                                            );
     }
 }
Пример #4
0
 protected internal override void CheckFormXObject(PdfStream form)
 {
     if (IsAlreadyChecked(form))
     {
         return;
     }
     if (form.ContainsKey(PdfName.OPI))
     {
         throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY
                                            );
     }
     if (form.ContainsKey(PdfName.PS))
     {
         throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_PS_KEY
                                            );
     }
     if (PdfName.PS.Equals(form.GetAsName(PdfName.Subtype2)))
     {
         throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_SUBTYPE2_KEY_WITH_A_VALUE_OF_PS
                                            );
     }
     if (form.ContainsKey(PdfName.SMask) && !PdfName.None.Equals(form.GetAsName(PdfName.SMask)))
     {
         throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS);
     }
     if (IsContainsTransparencyGroup(form))
     {
         throw new PdfAConformanceException(PdfAConformanceException.A_GROUP_OBJECT_WITH_AN_S_KEY_WITH_A_VALUE_OF_TRANSPARENCY_SHALL_NOT_BE_INCLUDED_IN_A_FORM_XOBJECT
                                            );
     }
     CheckResources(form.GetAsDictionary(PdfName.Resources));
     CheckContentStream(form);
 }
Пример #5
0
        protected internal override void CheckFileSpec(PdfDictionary fileSpec)
        {
            PdfName relationship = fileSpec.GetAsName(PdfName.AFRelationship);

            if (relationship == null || !allowedAFRelationships.Contains(relationship))
            {
                throw new PdfAConformanceException(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_ONE_OF_THE_PREDEFINED_AFRELATIONSHIP_KEYS
                                                   );
            }
            if (fileSpec.ContainsKey(PdfName.EF))
            {
                if (!fileSpec.ContainsKey(PdfName.F) || !fileSpec.ContainsKey(PdfName.UF) || !fileSpec.ContainsKey(PdfName
                                                                                                                   .Desc))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY
                                                       );
                }
                PdfDictionary ef           = fileSpec.GetAsDictionary(PdfName.EF);
                PdfStream     embeddedFile = ef.GetAsStream(PdfName.F);
                if (embeddedFile == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EF_KEY_OF_FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_DICTIONARY_WITH_VALID_F_KEY
                                                       );
                }
                if (!embeddedFile.ContainsKey(PdfName.Subtype))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.MIME_TYPE_SHALL_BE_SPECIFIED_USING_THE_SUBTYPE_KEY_OF_THE_FILE_SPECIFICATION_STREAM_DICTIONARY
                                                       );
                }
                if (embeddedFile.ContainsKey(PdfName.Params))
                {
                    PdfObject @params = embeddedFile.Get(PdfName.Params);
                    if ([email protected]())
                    {
                        throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_DICTIONARY_AS_VALUE
                                                           );
                    }
                    if (((PdfDictionary)@params).GetAsString(PdfName.ModDate) == null)
                    {
                        throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_VALID_MODDATE_KEY
                                                           );
                    }
                }
                else
                {
                    ILog logger = LogManager.GetLogger(typeof(PdfAChecker));
                    logger.Warn(PdfAConformanceLogMessageConstant.EMBEDDED_FILE_SHOULD_CONTAIN_PARAMS_KEY);
                }
            }
        }
Пример #6
0
        // if symbolic font encoding doesn't have differences, itext7 won't write encoding for such font
        protected internal override void CheckImage(PdfStream image, PdfDictionary currentColorSpaces)
        {
            PdfColorSpace colorSpace = null;

            if (IsAlreadyChecked(image))
            {
                colorSpace = checkedObjectsColorspace.Get(image);
                CheckColorSpace(colorSpace, currentColorSpaces, true, null);
                return;
            }
            PdfObject colorSpaceObj = image.Get(PdfName.ColorSpace);

            if (colorSpaceObj != null)
            {
                colorSpace = PdfColorSpace.MakeColorSpace(colorSpaceObj);
                CheckColorSpace(colorSpace, currentColorSpaces, true, null);
                checkedObjectsColorspace.Put(image, colorSpace);
            }
            if (image.ContainsKey(PdfName.Alternates))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_ALTERNATES_KEY
                                                   );
            }
            if (image.ContainsKey(PdfName.OPI))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY);
            }
            if (image.ContainsKey(PdfName.Interpolate) && (bool)image.GetAsBool(PdfName.Interpolate))
            {
                throw new PdfAConformanceException(PdfAConformanceException.THE_VALUE_OF_INTERPOLATE_KEY_SHALL_BE_FALSE);
            }
            CheckRenderingIntent(image.GetAsName(PdfName.Intent));
            if (image.ContainsKey(PdfName.SMask) && !PdfName.None.Equals(image.GetAsName(PdfName.SMask)))
            {
                throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS);
            }
        }
Пример #7
0
        // if symbolic font encoding doesn't have differences, itext7 won't write encoding for such font
        protected internal override void CheckImage(PdfStream image, PdfDictionary currentColorSpaces)
        {
            PdfColorSpace colorSpace = null;

            if (IsAlreadyChecked(image))
            {
                colorSpace = checkedObjectsColorspace.Get(image);
                CheckColorSpace(colorSpace, currentColorSpaces, true, null);
                return;
            }
            PdfObject colorSpaceObj = image.Get(PdfName.ColorSpace);

            if (colorSpaceObj != null)
            {
                colorSpace = PdfColorSpace.MakeColorSpace(colorSpaceObj);
                CheckColorSpace(colorSpace, currentColorSpaces, true, null);
                checkedObjectsColorspace[image] = colorSpace;
            }
            if (image.ContainsKey(PdfName.Alternates))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnImageDictionaryShallNotContainAlternatesKey);
            }
            if (image.ContainsKey(PdfName.OPI))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnImageDictionaryShallNotContainOpiKey);
            }
            if (image.ContainsKey(PdfName.Interpolate) && (bool)image.GetAsBool(PdfName.Interpolate))
            {
                throw new PdfAConformanceException(PdfAConformanceException.TheValueOfInterpolateKeyShallNotBeTrue);
            }
            CheckRenderingIntent(image.GetAsName(PdfName.Intent));
            if (image.ContainsKey(PdfName.SMask) && !PdfName.None.Equals(image.GetAsName(PdfName.SMask)))
            {
                throw new PdfAConformanceException(PdfAConformanceException.TheSmaskKeyIsNotAllowedInXobjects);
            }
        }
Пример #8
0
        protected internal override void CheckFileSpec(PdfDictionary fileSpec)
        {
            PdfName relationship = fileSpec.GetAsName(PdfName.AFRelationship);

            if (relationship == null || !allowedAFRelationships.Contains(relationship))
            {
                throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainOneOfThePredefinedAFRelationshipKeys
                                                   );
            }
            if (fileSpec.ContainsKey(PdfName.EF))
            {
                if (!fileSpec.ContainsKey(PdfName.F) || !fileSpec.ContainsKey(PdfName.UF) || !fileSpec.ContainsKey(PdfName
                                                                                                                   .Desc))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainFKeyUFKeyAndDescKey
                                                       );
                }
                PdfDictionary ef           = fileSpec.GetAsDictionary(PdfName.EF);
                PdfStream     embeddedFile = ef.GetAsStream(PdfName.F);
                if (embeddedFile == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EFKeyOfFileSpecificationDictionaryShallContainDictionaryWithValidFKey
                                                       );
                }
                if (!embeddedFile.ContainsKey(PdfName.Subtype))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.MimeTypeShallBeSpecifiedUsingTheSubtypeKeyOfTheFileSpecificationStreamDictionary
                                                       );
                }
                PdfDictionary @params = embeddedFile.GetAsDictionary(PdfName.Params);
                if (@params == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithDictionaryAsValue
                                                       );
                }
                if (@params.GetAsString(PdfName.ModDate) == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithValidModdateKey
                                                       );
                }
            }
        }
        internal virtual Rectangle CalculateViewPort(SvgDrawContext context)
        {
            float     portX         = 0f;
            float     portY         = 0f;
            float     portWidth     = 0f;
            float     portHeight    = 0f;
            PdfStream contentStream = context.GetCurrentCanvas().GetContentStream();

            if (!contentStream.ContainsKey(PdfName.BBox))
            {
                throw new SvgProcessingException(SvgLogMessageConstant.ROOT_SVG_NO_BBOX);
            }
            PdfArray bboxArray = contentStream.GetAsArray(PdfName.BBox);

            portX      = bboxArray.GetAsNumber(0).FloatValue();
            portY      = bboxArray.GetAsNumber(1).FloatValue();
            portWidth  = bboxArray.GetAsNumber(2).FloatValue() - portX;
            portHeight = bboxArray.GetAsNumber(3).FloatValue() - portY;
            return(new Rectangle(portX, portY, portWidth, portHeight));
        }
Пример #10
0
        private void ButtonStart_Click(object sender, EventArgs e)
        {
            string title    = IniFile.ReadIni("metadane", "Title");
            string author   = IniFile.ReadIni("metadane", "Author");
            string subject  = IniFile.ReadIni("metadane", "Subject");
            string keywords = IniFile.ReadIni("metadane", "Keywords");

            string podstawaPrawna = IniFile.ReadIni("metadane_dod", "podstawa_prawna");

            List <string> inputFolders = new List <string> {
                _inputDir
            };

            inputFolders.AddRange(Directory.GetDirectories(_inputDir, "*", SearchOption.AllDirectories).ToList());

            Dictionary <string, int>    validationErrors = new Dictionary <string, int>();
            Dictionary <string, int>    prefiksErrors    = new Dictionary <string, int>();
            Dictionary <string, string> otherErrors      = new Dictionary <string, string>();

            foreach (string inputFolder in inputFolders)
            {
                string outputFolder = inputFolder.Replace(_inputDir, _outputDir);

                Directory.CreateDirectory(outputFolder);

                string[] inputFiles = Directory.GetFiles(inputFolder, "*.pdf", SearchOption.TopDirectoryOnly);

                foreach (string inputFile in inputFiles)
                {
                    string outputFile = Path.Combine(outputFolder, Path.GetFileName(inputFile) ?? throw new InvalidOperationException());

                    PdfDocument srcPdf = new PdfDocument(new PdfReader(inputFile));

                    PdfOutputIntent rgbIntent = new PdfOutputIntent("Custom",
                                                                    "",
                                                                    "http://www.color.org",
                                                                    "sRGB IEC61966-2.1",
                                                                    new MemoryStream(Resources.sRGB_CS_profile));

                    PdfADocument outputPdf = new PdfADocument(new PdfWriter(outputFile), PdfAConformanceLevel.PDF_A_3B, rgbIntent);

                    try
                    {
                        srcPdf.CopyPagesTo(1, srcPdf.GetNumberOfPages(), outputPdf);
                    }
                    catch (Exception exception)
                    {
                        srcPdf.Close();

                        Document document = new Document(outputPdf);
                        document.Add(new Paragraph());
                        document.Close();
                        outputPdf.Close();

                        otherErrors.Add(inputFile, exception.Message);

                        continue;
                    }

                    srcPdf.Close();

                    PdfDocumentInfo info = outputPdf.GetDocumentInfo();

                    info.SetCreator("GN PDF_A Creator");

                    info.SetTitle(title);
                    info.SetAuthor(author);
                    info.SetSubject(subject);
                    info.SetKeywords(keywords);

                    info.SetMoreInfo(@"Podstawa prawna", podstawaPrawna);

                    string file = Path.GetFileName(outputFile);

                    Dictionary <string, string> rodzajDokumentu = _prefix.Where(p => file.ToUpper().Contains(p.Key.ToUpper())).ToDictionary(x => x.Key, x => x.Value);

                    if (rodzajDokumentu.Count != 1)
                    {
                        prefiksErrors.Add(inputFile, rodzajDokumentu.Count);
                        info.SetMoreInfo(@"Rodzaj Dokumentu", "");
                    }
                    else
                    {
                        info.SetMoreInfo(@"Rodzaj Dokumentu", rodzajDokumentu.Values.ElementAt(0));
                    }

                    info.SetMoreInfo("software", @"The file was created by the PDF_A program using iText 7 library");
                    info.SetMoreInfo("license", @"This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.");
                    info.SetMoreInfo("copyright", @"Copyright (C) 2019 GISNET");

                    for (int i = 0; i <= outputPdf.GetNumberOfPdfObjects(); i++)
                    {
                        PdfObject pdfObject = outputPdf.GetPdfObject(i);

                        if (pdfObject != null && pdfObject.IsStream())
                        {
                            PdfStream pdfStream = (PdfStream)pdfObject;

                            PdfObject subtype = pdfStream.Get(PdfName.Subtype);

                            if (Equals(subtype, PdfName.Image) && pdfStream.ContainsKey(PdfName.Interpolate))
                            {
                                pdfStream.Remove(PdfName.Interpolate);
                            }
                        }
                    }

                    try
                    {
                        outputPdf.Close();
                    }
                    catch (Exception exception)
                    {
                        otherErrors.Add(inputFile, exception.Message);

                        continue;
                    }

                    using (PDFACompliance pdfA = new PDFACompliance(false, outputFile, null, PDFACompliance.Conformance.e_Level3B, null, 10, false))
                    {
                        int errCnt = pdfA.GetErrorCount();

                        if (errCnt > 0)
                        {
                            validationErrors.Add(outputFile, errCnt);
                        }
                    }
                }
            }

            using (StreamWriter errorFile = new StreamWriter(new FileStream("errors_validation.txt", FileMode.Create)))
            {
                foreach (var error in validationErrors)
                {
                    errorFile.WriteLine(error.Key + ": " + error.Value);
                }
            }

            using (StreamWriter errorFile = new StreamWriter(new FileStream("errors_prefix.txt", FileMode.Create)))
            {
                foreach (var error in prefiksErrors)
                {
                    errorFile.WriteLine(error.Key + ": " + error.Value);
                }
            }

            using (StreamWriter errorFile = new StreamWriter(new FileStream("errors_other.txt", FileMode.Create)))
            {
                foreach (var error in otherErrors)
                {
                    errorFile.WriteLine(error.Key + ": " + error.Value);
                }
            }

            MessageBox.Show(@"Przetwarzanie zakończono.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }