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_" )); }
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 ); }
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()); }
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); }
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()); }
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); }
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()); }
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()); }
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()); }
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)) ; }
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()); }
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()); }
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()); }
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()); }
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); } } }
// 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); } }
// 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); } }
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)); } } } } }
public virtual PdfColorSpace GetColorSpace(PdfName name) { PdfObject colorSpace = GetResourceObject(PdfName.ColorSpace, name); return(colorSpace != null?PdfColorSpace.MakeColorSpace(colorSpace) : null); }
private static PdfStream CreatePdfStream(ImageData image, iText.Kernel.Pdf.Xobject.PdfImageXObject imageMask ) { PdfStream stream; if (image.GetOriginalType() == ImageType.RAW) { RawImageHelper.UpdateImageAttributes((RawImageData)image, null); } stream = new PdfStream(image.GetData()); String filter = image.GetFilter(); if (filter != null && filter.Equals("JPXDecode") && image.GetColorSpace() <= 0) { stream.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); image.SetBpc(0); } stream.Put(PdfName.Type, PdfName.XObject); stream.Put(PdfName.Subtype, PdfName.Image); PdfDictionary decodeParms = CreateDictionaryFromMap(stream, image.GetDecodeParms()); if (decodeParms != null) { stream.Put(PdfName.DecodeParms, decodeParms); } PdfName colorSpace; switch (image.GetColorSpace()) { case 1: { colorSpace = PdfName.DeviceGray; break; } case 3: { colorSpace = PdfName.DeviceRGB; break; } default: { colorSpace = PdfName.DeviceCMYK; break; } } stream.Put(PdfName.ColorSpace, colorSpace); if (image.GetBpc() != 0) { stream.Put(PdfName.BitsPerComponent, new PdfNumber(image.GetBpc())); } if (image.GetFilter() != null) { stream.Put(PdfName.Filter, new PdfName(image.GetFilter())); } //TODO: return to this later // if (image.getLayer() != null) // put(PdfName.OC, image.getLayer().getRef()); if (image.GetColorSpace() == -1) { stream.Remove(PdfName.ColorSpace); } PdfDictionary additional = CreateDictionaryFromMap(stream, image.GetImageAttributes()); if (additional != null) { stream.PutAll(additional); } IccProfile iccProfile = image.GetProfile(); if (iccProfile != null) { PdfStream iccProfileStream = PdfCieBasedCs.IccBased.GetIccProfileStream(iccProfile); PdfArray iccBasedColorSpace = new PdfArray(); iccBasedColorSpace.Add(PdfName.ICCBased); iccBasedColorSpace.Add(iccProfileStream); PdfObject colorSpaceObject = stream.Get(PdfName.ColorSpace); bool iccProfileShouldBeApplied = true; if (colorSpaceObject != null) { PdfColorSpace cs = PdfColorSpace.MakeColorSpace(colorSpaceObject); if (cs == null) { LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE ); } else { if (cs is PdfSpecialCs.Indexed) { PdfColorSpace baseCs = ((PdfSpecialCs.Indexed)cs).GetBaseCs(); if (baseCs == null) { LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE ); } else { if (baseCs.GetNumberOfComponents() != iccProfile.GetNumComponents()) { LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE ); iccProfileShouldBeApplied = false; } else { iccProfileStream.Put(PdfName.Alternate, baseCs.GetPdfObject()); } } if (iccProfileShouldBeApplied) { ((PdfArray)colorSpaceObject).Set(1, iccBasedColorSpace); iccProfileShouldBeApplied = false; } } else { if (cs.GetNumberOfComponents() != iccProfile.GetNumComponents()) { LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_COLOR_SPACE ); iccProfileShouldBeApplied = false; } else { iccProfileStream.Put(PdfName.Alternate, colorSpaceObject); } } } } if (iccProfileShouldBeApplied) { stream.Put(PdfName.ColorSpace, iccBasedColorSpace); } } if (image.IsMask() && (image.GetBpc() == 1 || image.GetBpc() > 0xff)) { stream.Put(PdfName.ImageMask, PdfBoolean.TRUE); } if (imageMask != null) { if (imageMask.softMask) { stream.Put(PdfName.SMask, imageMask.GetPdfObject()); } else { if (imageMask.mask) { stream.Put(PdfName.Mask, imageMask.GetPdfObject()); } } } ImageData mask = image.GetImageMask(); if (mask != null) { if (mask.IsSoftMask()) { stream.Put(PdfName.SMask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject( )); } else { if (mask.IsMask()) { stream.Put(PdfName.Mask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject() ); } } } if (image.GetDecode() != null) { stream.Put(PdfName.Decode, new PdfArray(image.GetDecode())); } if (image.IsMask() && image.IsInverted()) { stream.Put(PdfName.Decode, new PdfArray(new float[] { 1, 0 })); } if (image.IsInterpolation()) { stream.Put(PdfName.Interpolate, PdfBoolean.TRUE); } // deal with transparency int[] transparency = image.GetTransparency(); if (transparency != null && !image.IsMask() && imageMask == null) { PdfArray t = new PdfArray(); foreach (int transparencyItem in transparency) { t.Add(new PdfNumber(transparencyItem)); } stream.Put(PdfName.Mask, t); } stream.Put(PdfName.Width, new PdfNumber(image.GetWidth())); stream.Put(PdfName.Height, new PdfNumber(image.GetHeight())); return(stream); }