private void SetColorSameColorSpacesTest(String pdfName, bool pattern)
        {
            String        cmpFile  = sourceFolder + "cmp_" + pdfName;
            String        destFile = destinationFolder + pdfName;
            PdfDocument   document = new PdfDocument(new PdfWriter(destFile));
            PdfPage       page     = document.AddNewPage();
            PdfCanvas     canvas   = new PdfCanvas(page);
            PdfColorSpace space    = pattern ? new PdfSpecialCs.Pattern() : PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB
                                                                                                         );

            float[]    colorValue1 = pattern ? null : new float[] { 1.0f, 0.6f, 0.7f };
            float[]    colorValue2 = pattern ? null : new float[] { 0.1f, 0.9f, 0.9f };
            PdfPattern pattern1    = pattern ? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 750
                                                                                           , ColorConstants.PINK.GetColorValue(), 100, 760, ColorConstants.MAGENTA.GetColorValue())) : null;
            PdfPattern pattern2 = pattern ? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 690
                                                                                        , ColorConstants.BLUE.GetColorValue(), 100, 710, ColorConstants.CYAN.GetColorValue())) : null;

            canvas.SetColor(space, colorValue1, pattern1, true);
            canvas.SaveState();
            canvas.BeginText().MoveText(50, 750).SetFontAndSize(PdfFontFactory.CreateFont(), 16).ShowText("pinkish").EndText
                ();
            canvas.SaveState().BeginText().SetColor(space, colorValue2, pattern2, true).MoveText(50, 720).SetFontAndSize
                (PdfFontFactory.CreateFont(), 16).ShowText("bluish").EndText().RestoreState();
            canvas.RestoreState();
            canvas.SaveState().BeginText().MoveText(50, 690).SetColor(space, colorValue2, pattern2, true).SetFontAndSize
                (PdfFontFactory.CreateFont(), 16).ShowText("bluish").EndText().RestoreState();
            canvas.Release();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destFile, cmpFile, destinationFolder, "diff_"
                                                                             ));
        }
Beispiel #2
0
        public virtual void PdfUncoloredPatternColorSize1Test()
        {
            PdfDocument   pdfDocument       = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            String        contentColorSpace = "/Cs1 cs\n";
            PdfDictionary pageDictionary    = (PdfDictionary) new PdfDictionary().MakeIndirect(pdfDocument);
            PdfStream     contentStream     = new PdfStream(contentColorSpace.GetBytes());

            pageDictionary.Put(PdfName.Contents, contentStream);
            PdfPage page = pdfDocument.AddNewPage();

            page.GetPdfObject().Put(PdfName.Contents, contentStream);
            PdfArray pdfArray = new PdfArray();

            pdfArray.Add(PdfName.Pattern);
            PdfColorSpace space = PdfColorSpace.MakeColorSpace(pdfArray);

            page.GetResources().AddColorSpace(space);
            Rectangle rectangle = new Rectangle(50, 50, 1000, 1000);

            page.SetMediaBox(rectangle);
            PdfCanvasProcessor processor = new PdfCanvasProcessor(new PdfArrayTest.NoOpListener());

            processor.ProcessPageContent(page);
            // Check if we reach the end of the test without failings together with verifying expected color space instance
            NUnit.Framework.Assert.IsTrue(processor.GetGraphicsState().GetFillColor().GetColorSpace() is PdfSpecialCs.Pattern
                                          );
        }
        /// <summary>
        /// Gets the PixelFormat for the specified bits per pixel (bpp) or color depth.
        /// </summary>
        /// <param name="colorSpace">The colorspace of the image.</param>
        /// <param name="bitsPerPixel">The number of bits per pixel.</param>
        /// <param name="isIndexed">Optional parameters indicating if the bits are indexed. If indexed, then bitsPerPixel must be less than or equal to 8. Defaults to false.</param>
        /// <returns>The pixel format to read the data from.</returns>
        private static PixelFormat GetPixelFormat(PdfColorSpace colorSpace, int bitsPerPixel, bool isIndexed = false)
        {
            // The number of bits used to represent each color component.
            // Only a single value may be specified; the number of bits is the same for all color components.
            // Valid values are 1, 2, 4, and 8.
            switch (bitsPerPixel)
            {
            case 1:
                return(PixelFormat.Format1bppIndexed);

            case 2:
                return(PixelFormat.Format4bppIndexed);

            case 4:
                if (isIndexed)
                {
                    return(PixelFormat.Format4bppIndexed);
                }
                break;

            case 8:
                if ((isIndexed) || (colorSpace is PdfGrayColorSpace))
                {
                    return(PixelFormat.Format8bppIndexed);
                }
                return(PixelFormat.Format24bppRgb); // 8 bits per component x 3 (R,G,B) = 24
            }

            throw new ArgumentException(String.Format("The specified pixel depth '{0}' is not supported.", bitsPerPixel), "bitsPerPixel");
        }
            /// <summary>
            /// Initializes the item based on the specified PdfDictionary.
            /// </summary>
            /// <param name="dictionary">The dictionary to use for initialization.</param>
            private void Initialize(PdfDictionary dictionary)
            {
                if (dictionary == null)
                {
                    throw new ArgumentNullException("dictionary", "The PDF dictionary item to extract image meta data from was null.");
                }
                if (!dictionary.IsImage())
                {
                    throw new ArgumentException("The specified dictionary does not represent an image.", "dictionary");
                }

                Height       = dictionary.Elements.GetInteger("/Height");
                Width        = dictionary.Elements.GetInteger("/Width");
                BitsPerPixel = dictionary.Elements.GetInteger("/BitsPerComponent");
                Length       = dictionary.Elements.GetInteger("/Length");

                PdfItem colorSpace = null;

                if (dictionary.Elements.TryGetValue("/ColorSpace", out colorSpace))
                {
                    ColorSpace = PdfDictionaryColorSpace.Parse(colorSpace);
                }
                else
                {
                    ColorSpace = new PdfRGBColorSpace(); // Default to RGB Color Space
                }
            }
Beispiel #5
0
        public virtual void NotEqualsNullObjectTest()
        {
            float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f };
            Color   color1      = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);
            bool    result      = color1.Equals(null);

            NUnit.Framework.Assert.IsFalse(result);
        }
Beispiel #6
0
 private static PdfColorSpace EnsureNotPatternCs(PdfColorSpace underlyingCS)
 {
     if (underlyingCS is PdfSpecialCs.Pattern)
     {
         throw new ArgumentException("underlyingCS");
     }
     return(underlyingCS);
 }
Beispiel #7
0
        public virtual void MakeDeviceCmykNullColorValuesTest()
        {
            PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(PdfName.DeviceCMYK);
            Color         color      = Color.MakeColor(colorSpace);

            NUnit.Framework.Assert.IsTrue(color is DeviceCmyk);
            NUnit.Framework.Assert.AreEqual(new float[] { 0.0f, 0.0f, 0.0f, 1.0f }, color.GetColorValue());
        }
Beispiel #8
0
        public virtual void MakeDeviceCmykTest()
        {
            float[]       colorValues = new float[] { 0.7f, 0.5f, 0.1f, 0.3f };
            PdfColorSpace colorSpace  = PdfColorSpace.MakeColorSpace(PdfName.DeviceCMYK);
            Color         color       = Color.MakeColor(colorSpace, colorValues);

            NUnit.Framework.Assert.IsTrue(color is DeviceCmyk);
            NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue());
        }
Beispiel #9
0
        public virtual void NotEqualsDifferentClassesTest()
        {
            float[]    colorValues = new float[] { 0.0f, 0.5f, 0.1f };
            Color      color1      = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);
            DeviceCmyk cmyk        = new DeviceCmyk(0, 0, 0, 0);
            bool       result      = color1.Equals(cmyk);

            NUnit.Framework.Assert.IsFalse(result);
        }
Beispiel #10
0
 public PdfImageObject(int width, int height, PdfColorSpace colorSpace, int bitsPerComponent, byte[] data, params IPdfEncoder[] encoders)
 {
     Width            = width;
     Height           = height;
     ColorSpace       = colorSpace;
     BitsPerComponent = bitsPerComponent;
     Data             = data;
     Encoders         = encoders.ToList();
 }
Beispiel #11
0
        public virtual void EqualsAndHashCodeNullColorValuesTest()
        {
            Color color1 = new Color(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), null);
            Color color2 = new Color(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), null);
            bool  result = color1.Equals(color2);

            NUnit.Framework.Assert.IsTrue(result);
            NUnit.Framework.Assert.AreEqual(color1.GetHashCode(), color2.GetHashCode());
        }
Beispiel #12
0
        public virtual void SetAndGetInteriorColorFloatArrayTest()
        {
            PdfLineAnnotation pdfLineAnnotation = new PdfLineAnnotation(new PdfDictionary());

            float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f };
            pdfLineAnnotation.SetInteriorColor(colorValues);
            Color expectedColor = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);

            NUnit.Framework.Assert.AreEqual(expectedColor, pdfLineAnnotation.GetInteriorColor());
        }
Beispiel #13
0
 public virtual void SetColorValueIncorrectComponentsNumberTest()
 {
     NUnit.Framework.Assert.That(() => {
         float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f };
         Color color         = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);
         color.SetColorValue(new float[] { 0.1f, 0.2f });
     }
                                 , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(PdfException.IncorrectNumberOfComponents))
     ;
 }
Beispiel #14
0
 public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfColorSpace underlyingCS, float[] colorValue)
     : base(new PdfSpecialCs.UncoloredTilingPattern(underlyingCS), colorValue)
 {
     if (underlyingCS is PdfSpecialCs.Pattern)
     {
         throw new ArgumentException("underlyingCS");
     }
     this.pattern         = uncoloredPattern;
     this.underlyingColor = MakeColor(underlyingCS, colorValue);
 }
Beispiel #15
0
 public virtual void SetDefaultColorSpace(PdfName defaultCsKey, PdfColorSpace defaultCsValue)
 {
     if (!defaultCsKey.Equals(PdfName.DefaultCMYK) && !defaultCsKey.Equals(PdfName.DefaultGray) && !defaultCsKey
         .Equals(PdfName.DefaultRGB))
     {
         throw new PdfException(PdfException.UnsupportedDefaultColorSpaceName1).SetMessageParams(defaultCsKey.ToString
                                                                                                     ());
     }
     AddResource(defaultCsValue.GetPdfObject(), PdfName.ColorSpace, defaultCsKey);
 }
Beispiel #16
0
        public virtual void EqualsAndHashCodeTest()
        {
            float[] colorValues = new float[] { 0.0f, 0.5f, 0.1f };
            Color   color1      = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);
            Color   color2      = Color.MakeColor(PdfColorSpace.MakeColorSpace(PdfName.DeviceRGB), colorValues);
            bool    result      = color1.Equals(color2);

            NUnit.Framework.Assert.IsTrue(result);
            NUnit.Framework.Assert.AreEqual(color1.GetHashCode(), color2.GetHashCode());
        }
Beispiel #17
0
 /// <summary>Creates a Color of certain color space and color value.</summary>
 /// <remarks>
 /// Creates a Color of certain color space and color value.
 /// If color value is set in null, all value components will be initialised with zeroes.
 /// </remarks>
 /// <param name="colorSpace">the color space to which the created Color object relates</param>
 /// <param name="colorValue">the color value of the created Color object</param>
 protected internal Color(PdfColorSpace colorSpace, float[] colorValue)
 {
     this.colorSpace = colorSpace;
     if (colorValue == null)
     {
         this.colorValue = new float[colorSpace.GetNumberOfComponents()];
     }
     else
     {
         this.colorValue = colorValue;
     }
 }
Beispiel #18
0
        public virtual void MakeDeviceNNullColorValuesTest()
        {
            PdfArray deviceN = new PdfArray();

            deviceN.Add(PdfName.DeviceN);
            deviceN.Add(new PdfArray());
            deviceN.Add(null);
            deviceN.Add(null);
            PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(deviceN);
            Color         color      = Color.MakeColor(colorSpace);

            NUnit.Framework.Assert.IsTrue(color is DeviceN);
            NUnit.Framework.Assert.AreEqual(new float[] {  }, color.GetColorValue());
        }
Beispiel #19
0
        public virtual void MakeIndexedNullColorValuesTest()
        {
            PdfArray indexed = new PdfArray();

            indexed.Add(PdfName.Indexed);
            indexed.Add(new PdfArray());
            indexed.Add(null);
            indexed.Add(null);
            PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(indexed);
            Color         color      = Color.MakeColor(colorSpace);

            NUnit.Framework.Assert.IsTrue(color is Indexed);
            NUnit.Framework.Assert.AreEqual(new float[] { 0.0f }, color.GetColorValue());
        }
Beispiel #20
0
 public override void CheckColorSpace(PdfColorSpace colorSpace, PdfDictionary currentColorSpaces, bool checkAlternate
                                      , bool?fill)
 {
     if (colorSpace is PdfSpecialCs.Separation)
     {
         colorSpace = ((PdfSpecialCs.Separation)colorSpace).GetBaseCs();
     }
     else
     {
         if (colorSpace is PdfSpecialCs.DeviceN)
         {
             PdfSpecialCs.DeviceN deviceNColorspace = (PdfSpecialCs.DeviceN)colorSpace;
             if (deviceNColorspace.GetNumberOfComponents() > MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS)
             {
                 throw new PdfAConformanceException(PdfAConformanceException.THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED
                                                    , MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS);
             }
             colorSpace = deviceNColorspace.GetBaseCs();
         }
     }
     if (colorSpace is PdfDeviceCs.Rgb)
     {
         if (cmykIsUsed)
         {
             throw new PdfAConformanceException(PdfAConformanceException.DEVICERGB_AND_DEVICECMYK_COLORSPACES_CANNOT_BE_USED_BOTH_IN_ONE_FILE
                                                );
         }
         rgbIsUsed = true;
     }
     else
     {
         if (colorSpace is PdfDeviceCs.Cmyk)
         {
             if (rgbIsUsed)
             {
                 throw new PdfAConformanceException(PdfAConformanceException.DEVICERGB_AND_DEVICECMYK_COLORSPACES_CANNOT_BE_USED_BOTH_IN_ONE_FILE
                                                    );
             }
             cmykIsUsed = true;
         }
         else
         {
             if (colorSpace is PdfDeviceCs.Gray)
             {
                 grayIsUsed = true;
             }
         }
     }
 }
Beispiel #21
0
        public virtual void MakeLabNullColorValuesTest()
        {
            PdfDictionary dictionary = new PdfDictionary();

            dictionary.Put(PdfName.Gamma, new PdfNumber(2.2));
            PdfArray calLab = new PdfArray();

            calLab.Add(PdfName.Lab);
            calLab.Add(dictionary);
            PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calLab);
            Color         color      = Color.MakeColor(colorSpace);

            NUnit.Framework.Assert.IsTrue(color is Lab);
            NUnit.Framework.Assert.AreEqual(new float[] { 0.0f, 0.0f, 0.0f }, color.GetColorValue());
        }
Beispiel #22
0
        public virtual void MakeCalRgbTest()
        {
            PdfDictionary dictionary = new PdfDictionary();

            dictionary.Put(PdfName.Gamma, new PdfNumber(2.2));
            PdfArray calRgb = new PdfArray();

            calRgb.Add(PdfName.CalRGB);
            calRgb.Add(dictionary);
            PdfColorSpace colorSpace = PdfColorSpace.MakeColorSpace(calRgb);

            float[] colorValues = new float[] { 0.7f, 0.5f, 0.1f };
            Color   color       = Color.MakeColor(colorSpace, colorValues);

            NUnit.Framework.Assert.IsTrue(color is CalRgb);
            NUnit.Framework.Assert.AreEqual(colorValues, color.GetColorValue());
        }
Beispiel #23
0
        public static string ToPatternName(this PdfColorSpace colorSpace)
        {
            switch (colorSpace)
            {
            case PdfColorSpace.DeviceGray:
                return("DeviceGray");

            case PdfColorSpace.DeviceRGB:
                return("DeviceRGB");

            case PdfColorSpace.DeviceCMYK:
                return("DeviceCMYK");

            default:
                throw new ArgumentException(nameof(colorSpace));
            }
        }
Beispiel #24
0
        public virtual void PatternColorParsingValidPdfTest()
        {
            String      inputFile   = sourceFolder + "patternColorParsingValidPdfTest.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile));

            for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
            {
                PdfPage page = pdfDocument.GetPage(i);
                PdfCanvasProcessorIntegrationTest.ColorParsingEventListener colorParsingEventListener = new PdfCanvasProcessorIntegrationTest.ColorParsingEventListener
                                                                                                            ();
                PdfCanvasProcessor processor = new PdfCanvasProcessor(colorParsingEventListener);
                processor.ProcessPageContent(page);
                PathRenderInfo renderInfo = colorParsingEventListener.GetEncounteredPath();
                PdfColorSpace  colorSpace = renderInfo.GetGraphicsState().GetFillColor().GetColorSpace();
                NUnit.Framework.Assert.IsTrue(colorSpace is PdfSpecialCs.Pattern);
            }
        }
Beispiel #25
0
 public override void CheckColorSpace(PdfColorSpace colorSpace, PdfDictionary currentColorSpaces, bool checkAlternate
                                      , bool?fill)
 {
     if (colorSpace is PdfSpecialCs.Separation)
     {
         colorSpace = ((PdfSpecialCs.Separation)colorSpace).GetBaseCs();
     }
     else
     {
         if (colorSpace is PdfSpecialCs.DeviceN)
         {
             colorSpace = ((PdfSpecialCs.DeviceN)colorSpace).GetBaseCs();
         }
     }
     if (colorSpace is PdfDeviceCs.Rgb)
     {
         if (cmykIsUsed)
         {
             throw new PdfAConformanceException(PdfAConformanceException.DevicergbAndDevicecmykColorspacesCannotBeUsedBothInOneFile
                                                );
         }
         rgbIsUsed = true;
     }
     else
     {
         if (colorSpace is PdfDeviceCs.Cmyk)
         {
             if (rgbIsUsed)
             {
                 throw new PdfAConformanceException(PdfAConformanceException.DevicergbAndDevicecmykColorspacesCannotBeUsedBothInOneFile
                                                    );
             }
             cmykIsUsed = true;
         }
         else
         {
             if (colorSpace is PdfDeviceCs.Gray)
             {
                 grayIsUsed = true;
             }
         }
     }
 }
Beispiel #26
0
 public override void CheckColorSpace(PdfColorSpace colorSpace, PdfDictionary currentColorSpaces, bool checkAlternate
                                      , bool?fill)
 {
     if (colorSpace is PdfSpecialCs.Separation)
     {
         colorSpace = ((PdfSpecialCs.Separation)colorSpace).GetBaseCs();
     }
     else
     {
         if (colorSpace is PdfSpecialCs.DeviceN)
         {
             colorSpace = ((PdfSpecialCs.DeviceN)colorSpace).GetBaseCs();
         }
     }
     if (colorSpace is PdfDeviceCs.Rgb)
     {
         if (cmykIsUsed)
         {
             throw new PdfAConformanceException(PdfAConformanceException.DEVICERGB_AND_DEVICECMYK_COLORSPACES_CANNOT_BE_USED_BOTH_IN_ONE_FILE
                                                );
         }
         rgbIsUsed = true;
     }
     else
     {
         if (colorSpace is PdfDeviceCs.Cmyk)
         {
             if (rgbIsUsed)
             {
                 throw new PdfAConformanceException(PdfAConformanceException.DEVICERGB_AND_DEVICECMYK_COLORSPACES_CANNOT_BE_USED_BOTH_IN_ONE_FILE
                                                    );
             }
             cmykIsUsed = true;
         }
         else
         {
             if (colorSpace is PdfDeviceCs.Gray)
             {
                 grayIsUsed = true;
             }
         }
     }
 }
    /// <summary>
    /// Gets the PixelFormat for the specified bits per pixel (bpp) or color depth.
    /// </summary>
    /// <param name="colorSpace">The colorspace of the image.</param>
    /// <param name="bitsPerPixel">The number of bits per pixel.</param>
    /// <param name="isIndexed">Optional parameters indicating if the bits are indexed. If indexed, then bitsPerPixel must be less than or equal to 8. Defaults to false.</param>
    /// <returns>The pixel format to read the data from.</returns>
    private static PixelFormat GetPixelFormat(PdfColorSpace colorSpace, int bitsPerPixel, bool isIndexed = false)
    {
      // The number of bits used to represent each color component. 
      // Only a single value may be specified; the number of bits is the same for all color components. 
      // Valid values are 1, 2, 4, and 8.
      switch(bitsPerPixel) {
        case 1:
          return (PixelFormat.Format1bppIndexed);
        case 2:
          return (PixelFormat.Format4bppIndexed);
        case 4:
          if (isIndexed) { return (PixelFormat.Format4bppIndexed); }
          break;
        case 8:
          if ((isIndexed) || (colorSpace is PdfGrayColorSpace)) return (PixelFormat.Format8bppIndexed);
          return (PixelFormat.Format24bppRgb); // 8 bits per component x 3 (R,G,B) = 24
      }

      throw new ArgumentException(String.Format("The specified pixel depth '{0}' is not supported.", bitsPerPixel), "bitsPerPixel");
    }
Beispiel #28
0
        protected internal virtual void CheckResources(PdfDictionary resources)
        {
            if (resources == null)
            {
                return;
            }
            PdfDictionary xObjects = resources.GetAsDictionary(PdfName.XObject);
            PdfDictionary shadings = resources.GetAsDictionary(PdfName.Shading);

            if (xObjects != null)
            {
                foreach (PdfObject xObject in xObjects.Values())
                {
                    PdfStream xObjStream = (PdfStream)xObject;
                    PdfObject subtype    = xObjStream.Get(PdfName.Subtype);
                    if (PdfName.Image.Equals(subtype))
                    {
                        CheckImage(xObjStream, resources.GetAsDictionary(PdfName.ColorSpace));
                    }
                    else
                    {
                        if (PdfName.Form.Equals(subtype))
                        {
                            CheckFormXObject(xObjStream);
                        }
                    }
                }
            }
            if (shadings != null)
            {
                foreach (PdfObject shading in shadings.Values())
                {
                    PdfDictionary shadingDict = (PdfDictionary)shading;
                    CheckColorSpace(PdfColorSpace.MakeColorSpace(shadingDict.Get(PdfName.ColorSpace)), resources.GetAsDictionary
                                        (PdfName.ColorSpace), true, null);
                }
            }
        }
Beispiel #29
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);
            }
        }
Beispiel #30
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);
            }
        }
Beispiel #31
0
        protected internal virtual void CheckResources(PdfDictionary resources)
        {
            if (resources == null)
            {
                return;
            }
            PdfDictionary xObjects = resources.GetAsDictionary(PdfName.XObject);
            PdfDictionary shadings = resources.GetAsDictionary(PdfName.Shading);
            PdfDictionary patterns = resources.GetAsDictionary(PdfName.Pattern);

            if (xObjects != null)
            {
                foreach (PdfObject xObject in xObjects.Values())
                {
                    PdfStream xObjStream = (PdfStream)xObject;
                    PdfObject subtype    = null;
                    bool      isFlushed  = xObjStream.IsFlushed();
                    if (!isFlushed)
                    {
                        subtype = xObjStream.Get(PdfName.Subtype);
                    }
                    if (PdfName.Image.Equals(subtype) || isFlushed)
                    {
                        // if flushed still may be need to check colorspace in given context
                        CheckImage(xObjStream, resources.GetAsDictionary(PdfName.ColorSpace));
                    }
                    else
                    {
                        if (PdfName.Form.Equals(subtype))
                        {
                            CheckFormXObject(xObjStream);
                        }
                    }
                }
            }
            if (shadings != null)
            {
                foreach (PdfObject shading in shadings.Values())
                {
                    PdfDictionary shadingDict = (PdfDictionary)shading;
                    if (!IsAlreadyChecked(shadingDict))
                    {
                        CheckColorSpace(PdfColorSpace.MakeColorSpace(shadingDict.Get(PdfName.ColorSpace)), resources.GetAsDictionary
                                            (PdfName.ColorSpace), true, null);
                    }
                }
            }
            if (patterns != null)
            {
                foreach (PdfObject p in patterns.Values())
                {
                    if (p.IsStream())
                    {
                        PdfStream pStream = (PdfStream)p;
                        if (!IsAlreadyChecked(pStream))
                        {
                            CheckResources(pStream.GetAsDictionary(PdfName.Resources));
                        }
                    }
                }
            }
        }
      /// <summary>
      /// Initializes the item based on the specified PdfDictionary.
      /// </summary>
      /// <param name="dictionary">The dictionary to use for initialization.</param>
      private void Initialize(PdfDictionary dictionary)
      {
        if (dictionary == null) throw new ArgumentNullException("dictionary", "The PDF dictionary item to extract image meta data from was null.");
        if (!dictionary.IsImage()) throw new ArgumentException("The specified dictionary does not represent an image.", "dictionary");

        Height = dictionary.Elements.GetInteger("/Height");
        Width = dictionary.Elements.GetInteger("/Width");
        BitsPerPixel = dictionary.Elements.GetInteger("/BitsPerComponent");
        Length = dictionary.Elements.GetInteger("/Length");

        PdfItem colorSpace = null;
        if (dictionary.Elements.TryGetValue("/ColorSpace", out colorSpace)) {
          ColorSpace = PdfDictionaryColorSpace.Parse(colorSpace);
        }
        else ColorSpace = new PdfRGBColorSpace(); // Default to RGB Color Space
      }