コード例 #1
0
        protected override void CheckColor(PdfWriter writer, int key, Object obj1)
        {
            switch (key)
            {
            case PdfIsoKeys.PDFISOKEY_COLOR:
                if (obj1 is ExtendedColor)
                {
                    ExtendedColor ec = (ExtendedColor)obj1;
                    switch (ec.Type)
                    {
                    case ExtendedColor.TYPE_CMYK:
                        CheckColor(writer, PdfIsoKeys.PDFISOKEY_CMYK, obj1);
                        break;

                    case ExtendedColor.TYPE_GRAY:
                        return;

                    case ExtendedColor.TYPE_RGB:
                        CheckColor(writer, PdfIsoKeys.PDFISOKEY_RGB, obj1);
                        break;

                    case ExtendedColor.TYPE_SEPARATION:
                        SpotColor sc = (SpotColor)ec;
                        CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, sc.PdfSpotColor.AlternativeCS);
                        break;

                    case ExtendedColor.TYPE_SHADING:
                        ShadingColor xc = (ShadingColor)ec;
                        CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace);
                        break;

                    case ExtendedColor.TYPE_PATTERN:
                        PatternColor pc = (PatternColor)ec;
                        CheckColor(writer, PdfIsoKeys.PDFISOKEY_COLOR, pc.Painter.DefaultColor);
                        break;
                    }
                }
                else if (obj1 is BaseColor)
                {
                    CheckColor(writer, PdfIsoKeys.PDFISOKEY_RGB, obj1);
                }
                break;

            case PdfIsoKeys.PDFISOKEY_CMYK:
                if (rgbUsed)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.and.devicecmyk.colorspaces.cannot.be.used.both.in.one.file"));
                }
                cmykUsed = true;
                break;

            case PdfIsoKeys.PDFISOKEY_RGB:
                if (cmykUsed)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.and.devicecmyk.colorspaces.cannot.be.used.both.in.one.file"));
                }
                rgbUsed = true;
                break;
            }
        }
コード例 #2
0
        /**
         * Business logic that checks if a certain object is in conformance with PDF/X.
         * @param writer    the writer that is supposed to write the PDF/X file
         * @param key       the type of PDF ISO conformance that has to be checked
         * @param obj1      the object that is checked for conformance
         */
        public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1)
        {
            if (writer == null || !writer.IsPdfX())
            {
                return;
            }
            int conf = writer.PDFXConformance;

            switch (key)
            {
            case PdfIsoKeys.PDFISOKEY_COLOR:
                switch (conf)
                {
                case PdfWriter.PDFX1A2001:
                    if (obj1 is ExtendedColor)
                    {
                        ExtendedColor ec = (ExtendedColor)obj1;
                        switch (ec.Type)
                        {
                        case ExtendedColor.TYPE_CMYK:
                        case ExtendedColor.TYPE_GRAY:
                            return;

                        case ExtendedColor.TYPE_RGB:
                            throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed"));

                        case ExtendedColor.TYPE_SEPARATION:
                            SpotColor sc = (SpotColor)ec;
                            CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, sc.PdfSpotColor.AlternativeCS);
                            break;

                        case ExtendedColor.TYPE_SHADING:
                            ShadingColor xc = (ShadingColor)ec;
                            CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace);
                            break;

                        case ExtendedColor.TYPE_PATTERN:
                            PatternColor pc = (PatternColor)ec;
                            CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, pc.Painter.DefaultColor);
                            break;
                        }
                    }
                    else if (obj1 is BaseColor)
                    {
                        throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed"));
                    }
                    break;
                }
                break;

            case PdfIsoKeys.PDFISOKEY_CMYK:
                break;

            case PdfIsoKeys.PDFISOKEY_RGB:
                if (conf == PdfWriter.PDFX1A2001)
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed"));
                }
                break;

            case PdfIsoKeys.PDFISOKEY_FONT:
                if (!((BaseFont)obj1).IsEmbedded())
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont)obj1).PostscriptFontName));
                }
                break;

            case PdfIsoKeys.PDFISOKEY_IMAGE:
                PdfImage image = (PdfImage)obj1;
                if (image.Get(PdfName.SMASK) != null)
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.images"));
                }
                switch (conf)
                {
                case PdfWriter.PDFX1A2001:
                    PdfObject cs = image.Get(PdfName.COLORSPACE);
                    if (cs == null)
                    {
                        return;
                    }
                    if (cs.IsName())
                    {
                        if (PdfName.DEVICERGB.Equals(cs))
                        {
                            throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed"));
                        }
                    }
                    else if (cs.IsArray())
                    {
                        if (PdfName.CALRGB.Equals(((PdfArray)cs)[0]))
                        {
                            throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.calrgb.is.not.allowed"));
                        }
                    }
                    break;
                }
                break;

            case PdfIsoKeys.PDFISOKEY_GSTATE:
                PdfDictionary gs  = (PdfDictionary)obj1;
                PdfObject     obj = gs.Get(PdfName.BM);
                if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj))
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("blend.mode.1.not.allowed", obj.ToString()));
                }
                obj = gs.Get(PdfName.CA);
                double v = 0.0;
                if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0)
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v));
                }
                obj = gs.Get(PdfName.ca_);
                v   = 0.0;
                if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0)
                {
                    throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v));
                }
                break;

            case PdfIsoKeys.PDFISOKEY_LAYER:
                throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("layers.are.not.allowed"));
            }
        }
コード例 #3
0
        /**
         * Business logic that checks if a certain object is in conformance with PDF/X.
         * @param writer    the writer that is supposed to write the PDF/X file
         * @param key       the type of PDF/X conformance that has to be checked
         * @param obj1      the object that is checked for conformance
         */
        public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1)
        {
            if (writer == null || !writer.IsPdfX())
            {
                return;
            }
            int conf = writer.PDFXConformance;

            switch (key)
            {
            case PDFXKEY_COLOR:
                switch (conf)
                {
                case PdfWriter.PDFX1A2001:
                    if (obj1 is ExtendedColor)
                    {
                        ExtendedColor ec = (ExtendedColor)obj1;
                        switch (ec.Type)
                        {
                        case ExtendedColor.TYPE_CMYK:
                        case ExtendedColor.TYPE_GRAY:
                            return;

                        case ExtendedColor.TYPE_RGB:
                            throw new PdfXConformanceException("Colorspace RGB is not allowed.");

                        case ExtendedColor.TYPE_SEPARATION:
                            SpotColor sc = (SpotColor)ec;
                            CheckPDFXConformance(writer, PDFXKEY_COLOR, sc.PdfSpotColor.AlternativeCS);
                            break;

                        case ExtendedColor.TYPE_SHADING:
                            ShadingColor xc = (ShadingColor)ec;
                            CheckPDFXConformance(writer, PDFXKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace);
                            break;

                        case ExtendedColor.TYPE_PATTERN:
                            PatternColor pc = (PatternColor)ec;
                            CheckPDFXConformance(writer, PDFXKEY_COLOR, pc.Painter.DefaultColor);
                            break;
                        }
                    }
                    else if (obj1 is Color)
                    {
                        throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                    }
                    break;
                }
                break;

            case PDFXKEY_CMYK:
                break;

            case PDFXKEY_RGB:
                if (conf == PdfWriter.PDFX1A2001)
                {
                    throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                }
                break;

            case PDFXKEY_FONT:
                if (!((BaseFont)obj1).IsEmbedded())
                {
                    throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont)obj1).PostscriptFontName);
                }
                break;

            case PDFXKEY_IMAGE:
                PdfImage image = (PdfImage)obj1;
                if (image.Get(PdfName.SMASK) != null)
                {
                    throw new PdfXConformanceException("The /SMask key is not allowed in images.");
                }
                switch (conf)
                {
                case PdfWriter.PDFX1A2001:
                    PdfObject cs = image.Get(PdfName.COLORSPACE);
                    if (cs == null)
                    {
                        return;
                    }
                    if (cs.IsName())
                    {
                        if (PdfName.DEVICERGB.Equals(cs))
                        {
                            throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                        }
                    }
                    else if (cs.IsArray())
                    {
                        if (PdfName.CALRGB.Equals(((PdfArray)cs)[0]))
                        {
                            throw new PdfXConformanceException("Colorspace CalRGB is not allowed.");
                        }
                    }
                    break;
                }
                break;

            case PDFXKEY_GSTATE:
                PdfDictionary gs  = (PdfDictionary)obj1;
                PdfObject     obj = gs.Get(PdfName.BM);
                if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj))
                {
                    throw new PdfXConformanceException("Blend mode " + obj.ToString() + " not allowed.");
                }
                obj = gs.Get(PdfName.CA);
                double v = 0.0;
                if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0)
                {
                    throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v);
                }
                obj = gs.Get(PdfName.ca_);
                v   = 0.0;
                if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0)
                {
                    throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v);
                }
                break;

            case PDFXKEY_LAYER:
                throw new PdfXConformanceException("Layers are not allowed.");
            }
        }
コード例 #4
0
        public Chap1014()
        {
            Console.WriteLine("Chapter 10 Example 14: colored patterns");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            Document.Compress = false;
            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a PDF-stream to a file
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1014.pdf", FileMode.Create));

                // step 3: we open the document
                document.Open();

                // step 4: we add some content
                PdfContentByte    cb  = writer.DirectContent;
                PdfTemplate       tp  = cb.CreateTemplate(400, 300);
                PdfPatternPainter pat = cb.CreatePattern(15, 15, null);
                pat.Rectangle(5, 5, 5, 5);
                pat.Fill();
                PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
                SpotColor    spot     = new SpotColor(spc_cmyk);
                tp.SetPatternFill(pat, spot, .9f);
                tp.Rectangle(0, 0, 400, 300);
                tp.Fill();
                cb.AddTemplate(tp, 50, 50);
                PdfPatternPainter pat2 = cb.CreatePattern(10, 10, null);
                pat2.SetLineWidth(2);
                pat2.MoveTo(-5, 0);
                pat2.LineTo(10, 15);
                pat2.Stroke();
                pat2.MoveTo(0, -5);
                pat2.LineTo(15, 10);
                pat2.Stroke();
                cb.SetLineWidth(1);
                cb.SetColorStroke(new Color(System.Drawing.Color.Black));
                cb.SetPatternFill(pat2, new Color(System.Drawing.Color.Red));
                cb.Rectangle(100, 400, 30, 210);
                cb.FillStroke();
                cb.SetPatternFill(pat2, new Color(System.Drawing.Color.LightGreen));
                cb.Rectangle(150, 400, 30, 100);
                cb.FillStroke();
                cb.SetPatternFill(pat2, new Color(System.Drawing.Color.Blue));
                cb.Rectangle(200, 400, 30, 130);
                cb.FillStroke();
                cb.SetPatternFill(pat2, new GrayColor(0.5f));
                cb.Rectangle(250, 400, 30, 80);
                cb.FillStroke();
                cb.SetPatternFill(pat2, new GrayColor(0.7f));
                cb.Rectangle(300, 400, 30, 170);
                cb.FillStroke();
                cb.SetPatternFill(pat2, new GrayColor(0.9f));
                cb.Rectangle(350, 400, 30, 40);
                cb.FillStroke();
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.Message);
                Console.Error.WriteLine(de.StackTrace);
            }
            // step 5: we close the document
            document.Close();
        }