Beispiel #1
0
        private static void DrawImageMasks(PdfPage page, Stream imageStream, Stream softMaskStream, Stream stencilMaskStream, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();

            page.Graphics.DrawString("Images Masks", titleFont, brush, 20, 50);

            page.Graphics.DrawString("Soft mask:", sectionFont, brush, 20, 70);
            PdfPngImage softMaskImage = new PdfPngImage(softMaskStream);
            PdfSoftMask softMask      = new PdfSoftMask(softMaskImage);

            imageStream.Position = 0;
            PdfJpegImage softMaskJpeg = new PdfJpegImage(imageStream);

            softMaskJpeg.Mask = softMask;
            // Draw the image with a soft mask.
            page.Graphics.DrawImage(softMaskJpeg, 20, 90, 280, 0);

            page.Graphics.DrawString("Stencil mask:", sectionFont, brush, 320, 70);
            PdfPngImage    stencilMaskImage = new PdfPngImage(stencilMaskStream);
            PdfStencilMask stencilMask      = new PdfStencilMask(stencilMaskImage);

            imageStream.Position = 0;
            PdfJpegImage stencilMaskJpeg = new PdfJpegImage(imageStream);

            stencilMaskJpeg.Mask = stencilMask;
            // Draw the image with a stencil mask.
            page.Graphics.DrawImage(stencilMaskJpeg, 320, 90, 280, 0);

            page.Graphics.DrawString("Stencil mask painting:", sectionFont, brush, 20, 320);
            PdfBrush redBrush = new PdfBrush(PdfRgbColor.DarkRed);

            page.Graphics.DrawStencilMask(stencilMask, redBrush, 20, 340, 280, 0);
            PdfBrush blueBrush = new PdfBrush(PdfRgbColor.DarkBlue);

            page.Graphics.DrawStencilMask(stencilMask, blueBrush, 320, 340, 280, 0);
            PdfBrush greenBrush = new PdfBrush(PdfRgbColor.DarkGreen);

            page.Graphics.DrawStencilMask(stencilMask, greenBrush, 20, 550, 280, 0);
            PdfBrush yellowBrush = new PdfBrush(PdfRgbColor.YellowGreen);

            page.Graphics.DrawStencilMask(stencilMask, yellowBrush, 320, 550, 280, 0);

            page.Graphics.CompressAndClose();
        }
Beispiel #2
0
        /// <summary>
        /// Builds the soft mask.
        /// </summary>
        PdfSoftMask BuildSoftMask(RadialGradientBrush brush)
        {
            Debug.Assert(brush.GradientStops.HasTransparency);

            XRect viewBox = new XRect(0, 0, 360, 480); // HACK
            //XForm xform = new XForm(Context.PdfDocument, viewBox);

            PdfFormXObject form = Context.PdfDocument.Internals.CreateIndirectObject <PdfFormXObject>();

#if DEBUG
            if (DevHelper.RenderComments)
            {
                form.Elements.SetString("/@comment", "This is the Form XObject of the soft mask");
            }
#endif
            form.Elements.SetRectangle(PdfFormXObject.Keys.BBox, new PdfRectangle(viewBox));


            // Transparency group of mask form
            //<<
            //  /CS /DeviceGray
            //  /I false
            //  /K false
            //  /S /Transparency
            //  /Type /Group
            //>>
            PdfTransparencyGroupAttributes tgAttributes = Context.PdfDocument.Internals.CreateIndirectObject <PdfTransparencyGroupAttributes>();
            tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceGray");
            tgAttributes.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.I, false);
            tgAttributes.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.K, false);

            // ExtGState of mask form
            //<<
            //  /AIS false
            //  /BM /Normal
            //  /ca 1
            //  /CA 1
            //  /op false
            //  /OP false
            //  /OPM 1
            //  /SA true
            //  /SMask /None
            //  /Type /ExtGState
            //>>
            PdfExtGState pdfStateMaskFrom = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
            pdfStateMaskFrom.SetDefault1();

            // Shading of mask form
            PdfShading shadingFrom = BuildShadingForSoftMask(brush);

            ////// Set reference to transparency group attributes
            ////pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes);
            ////pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]");

            // Soft mask
            //<<
            //  /G 21 0 R   % form
            //  /S /Luminosity
            //  /Type /Mask
            //>>
            PdfSoftMask softmask = Context.PdfDocument.Internals.CreateIndirectObject <PdfSoftMask>(); // new PdfSoftMask(this.writer.Owner);
            //extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask);
            //this.writer.Owner.Internals.AddObject(softmask);
#if DEBUG
            if (DevHelper.RenderComments)
            {
                softmask.Elements.SetString("/@comment", "This is the soft mask");
            }
#endif
            softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity");
            softmask.Elements.SetReference(PdfSoftMask.Keys.G, form);

            // Create content of mask form
            //<<
            //  /BBox [200.118 369.142 582.795 -141.094]
            //  /Group 16 0 R
            //  /Length 121
            //  /Matrix [1 0 0 1 0 0]
            //  /Resources
            //  <<
            //    /ExtGState
            //    <<
            //      /GS0 20 0 R
            //    >>
            //    /Shading
            //    <<
            //      /Sh0 19 0 R
            //    >>
            //  >>
            //  /Subtype /Form
            //>>
            //stream
            //  q
            //    200.118 369.142 382.677 -510.236 re
            //    W n
            //  q
            //    0 g
            //    1 i
            //    GS0 gs
            //    0.75 0 0 -0.75 200.1181183 369.1417236 cm
            //   BX /Sh0 sh EX Q
            //  Q
            //endstream
            form.Elements.SetReference(PdfFormXObject.Keys.Group, tgAttributes);
            PdfContentWriter writer = new PdfContentWriter(Context, form);
            writer.BeginContentRaw();
            // Acrobat 8 clips to bounding box, so we should do
            // why   0 480 360 -480 re ??
            //writer.WriteClip(bbox);
            //writer.WriteGraphicsState(extGState);
            writer.WriteLiteral("1 i 0 g\n");
            writer.WriteLiteral(writer.Resources.AddExtGState(pdfStateMaskFrom) + " gs\n");

            XMatrix transform = new XMatrix(); //(brush.Viewport.Width / viewBoxForm.width, 0, 0, brush.Viewport.Height / viewBoxForm.height, 0, 0);
            writer.WriteMatrix(transform);
            writer.WriteLiteral("BX " + writer.Resources.AddShading(shadingFrom) + " sh EX\n");
            writer.EndContent();

            return(softmask);
        }
Beispiel #3
0
        /// <summary>
        /// Builds a PdfFormXObject from the specified brush.
        /// // If a gradient contains transparency, a soft mask is created an added to the specified graphic state.
        /// </summary>
        PdfFormXObject BuildForm(RadialGradientBrush brush, PathGeometry geometry)
        {
            PdfFormXObject pdfForm = Context.PdfDocument.Internals.CreateIndirectObject <PdfFormXObject>();

            // HACK
            pdfForm.Elements.SetRectangle(PdfFormXObject.Keys.BBox, new PdfRectangle(0, 640, 480, 0));

            // Transparency group of the form
            //<<
            //  /I true
            //  /K false
            //  /S /Transparency
            //  /Type /Group
            //>>
            PdfTransparencyGroupAttributes tgPrimaryForm = Context.PdfDocument.Internals.CreateIndirectObject <PdfTransparencyGroupAttributes>();

            // not set by Acrobat: tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB");
            tgPrimaryForm.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.I, true);
            tgPrimaryForm.Elements.SetBoolean(PdfTransparencyGroupAttributes.Keys.K, false);
            pdfForm.Elements.SetReference(PdfFormXObject.Keys.Group, tgPrimaryForm);

            // Shading
            PdfShading shading = BuildShading(brush, 1, 1);

            // ExtGState
            //<<
            //  /AIS false
            //  /BM /Normal
            //  /ca 1
            //  /CA 1
            //  /op false
            //  /OP false
            //  /OPM 1
            //  /SA true
            //  /SMask 22 0 R
            //  /Type /ExtGState
            //>>
            PdfExtGState pdfExtGState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();

            pdfExtGState.SetDefault1();

            // Soft mask
            PdfSoftMask softmask = BuildSoftMask(brush);

            pdfExtGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask);

            // PdfFormXObject
            //<<
            //  /BBox [200.118 369.142 582.795 -141.094]
            //  /Group 11 0 R
            //  /Length 117
            //  /Matrix [1 0 0 1 0 0]
            //  /Resources
            //  <<
            //    /ColorSpace
            //    <<
            //      /CS0 8 0 R
            //    >>
            //    /ExtGState
            //    <<
            //      /GS0 23 0 R
            //    >>
            //    /Shading
            //    <<
            //      /Sh0 14 0 R
            //    >>
            //  >>
            //  /Subtype /Form
            //>>
            //stream
            //q
            //203.868 365.392 157.5 -97.5 re
            //W* n
            //q
            //0 g
            //1 i
            ///GS0 gs
            //0.75 0 0 -0.75 200.1181183 369.1417236 cm
            //BX /Sh0 sh EX Q
            //Q
            //endstream
            PdfContentWriter writer = new PdfContentWriter(Context, pdfForm);

            writer.BeginContentRaw();
            // Acrobat 8 clips to bounding box, so we should do
            writer.WriteClip(geometry);
            //writer.WriteGraphicsState(extGState);
            //writer.WriteLiteral("0 g\n");
            writer.WriteLiteral(writer.Resources.AddExtGState(pdfExtGState) + " gs\n");

            XMatrix transform = new XMatrix(); //(brush.Viewport.Width / viewBoxForm.width, 0, 0, brush.Viewport.Height / viewBoxForm.height, 0, 0);

            writer.WriteMatrix(transform);
            writer.WriteLiteral("BX " + writer.Resources.AddShading(shading) + " sh EX\n");
            writer.EndContent();

            return(pdfForm);
        }
Beispiel #4
0
        void RealizeLinearGradientBrush(LinearGradientBrush brush, XForm xform)
        {
            XMatrix           matrix  = currentTransform;
            PdfShadingPattern pattern = new PdfShadingPattern(writer.Owner);

            pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern

            // Setup shading
            PdfShading   shading   = new PdfShading(writer.Owner);
            PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode;

            PdfDictionary function = BuildShadingFunction(brush.GradientStops, colorMode);

            function.Elements.SetString("/@", "This is the shading function of a LinearGradientBrush");
            shading.Elements[PdfShading.Keys.Function] = function;

            shading.Elements[PdfShading.Keys.ShadingType] = new PdfInteger(2); // Axial shading
            //if (colorMode != PdfColorMode.Cmyk)
            shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB");
            //else
            //shading.Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");

            //double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
            double x1 = brush.StartPoint.X;
            double y1 = brush.StartPoint.Y;
            double x2 = brush.EndPoint.X;
            double y2 = brush.EndPoint.Y;

            shading.Elements[PdfShading.Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", x1, y1, x2, y2);

            // old: Elements[Keys.Background] = new PdfRawItem("[0 1 1]");
            // old: Elements[Keys.Domain] =
            shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]");

            // Setup pattern
            pattern.Elements[PdfShadingPattern.Keys.Shading] = shading;
            pattern.Elements[PdfShadingPattern.Keys.Matrix]  = PdfLiteral.FromMatrix(matrix); // new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]");

            string name = writer.Resources.AddPattern(pattern);

            writer.WriteLiteral("/Pattern cs\n", name);
            writer.WriteLiteral("{0} scn\n", name);

            double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha();

            if (alpha < 1 && writer.renderMode == RenderMode.Default)
            {
#if true
                PdfExtGState extGState = writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha);
                string       gs        = writer.Resources.AddExtGState(extGState);
                writer.WriteLiteral("{0} gs\n", gs);
#else
#if true
                if (xform == null)
                {
                    PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha);
                    string       gs        = this.writer.Resources.AddExtGState(extGState);
                    this.writer.WriteGraphicState(extGState);
                }
                else
                {
                    //PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form);
                    PdfFormXObject pdfForm = xform.pdfForm;
                    pdfForm.Elements.SetString("/@", "This is the Form XObject of the soft mask");

                    string formName = this.writer.Resources.AddForm(pdfForm);

                    PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner);
                    //this.writer.Owner.Internals.AddObject(tgAttributes);
                    tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB");

                    // Set reference to transparency group attributes
                    pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes);
                    pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]");


                    PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner);
                    this.writer.Owner.Internals.AddObject(softmask);
                    softmask.Elements.SetString("/@", "This is the soft mask");
                    softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity");
                    softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm);
                    //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group");
                    //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group");

                    PdfExtGState extGState = new PdfExtGState(this.writer.Owner);
                    this.writer.Owner.Internals.AddObject(extGState);
                    extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask);
                    this.writer.WriteGraphicState(extGState);
                }
#else
                XForm     form    = new XForm(this.writer.Owner, 220, 140);
                XGraphics formGfx = XGraphics.FromForm(form);

                // draw something
                //XSolidBrush xbrush = new XSolidBrush(XColor.FromArgb(128, 128, 128));
                XLinearGradientBrush xbrush = new XLinearGradientBrush(new XPoint(0, 0), new XPoint(220, 0), XColors.White, XColors.Black);
                formGfx.DrawRectangle(xbrush, -10000, -10000, 20000, 20000);
                //formGfx.DrawString("Text, Graphics, Images, and Forms", new XFont("Verdana", 10, XFontStyle.Regular), XBrushes.Navy, 3, 0, XStringFormat.TopLeft);
                formGfx.Dispose();

                // Close form
                form.Finish();
                PdfFormXObject pdfForm  = this.writer.Owner.FormTable.GetForm(form);
                string         formName = this.writer.Resources.AddForm(pdfForm);

                //double x = 20, y = 20;
                //double cx = 1;
                //double cy = 1;

                //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n",
                //  x, y, cx, cy, y + 0, formName);

                //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n",
                //  x, y, cx, cy, y + 220/1.5, formName);

                PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner);
                this.writer.Owner.Internals.AddObject(tgAttributes);

                tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB");


                // Set reference to transparency group attributes
                pdfForm.Elements.SetReference(PdfFormXObject.Keys.Group, tgAttributes);


                PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner);
                this.writer.Owner.Internals.AddObject(softmask);
                softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity");
                softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm);
                //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group");
                //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group");

                PdfExtGState extGState = new PdfExtGState(this.writer.Owner);
                this.writer.Owner.Internals.AddObject(extGState);
                extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask);
                this.writer.WriteGraphicState(extGState);
#endif
#endif
            }
        }
Beispiel #5
0
        void RealizeRadialGradientBrush(RadialGradientBrush brush, XForm xform)
        {
            XMatrix matrix = new XMatrix(); // this.renderer.defaultViewMatrix;

            //matrix.MultiplyPrepend(this.Transform);
            matrix = currentTransform;
            //matrix.MultiplyPrepend(XMatrix.CreateScaling(1.3, 1));
            PdfShadingPattern pattern = new PdfShadingPattern(writer.Owner);

            pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern

            // Setup shading
            PdfShading shading = new PdfShading(writer.Owner);

            PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode;

            PdfDictionary function = BuildShadingFunction(brush.GradientStops, colorMode);

            shading.Elements[PdfShading.Keys.ShadingType] = new PdfInteger(3); // Radial shading
            //if (colorMode != PdfColorMode.Cmyk)
            shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB");
            //else
            //shading.Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");

            double x0 = brush.GradientOrigin.X;
            double y0 = brush.GradientOrigin.Y;
            double r0 = 0;
            double x1 = brush.Center.X;
            double y1 = brush.Center.Y;
            double r1 = brush.RadiusX;

            shading.Elements[PdfShading.Keys.Coords] =
                new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###} {4:0.###} {5:0.###}]", x0, y0, r0, x1, y1, r1);

            // old: Elements[Keys.Background] = new PdfRawItem("[0 1 1]");
            // old: Elements[Keys.Domain] =
            shading.Elements[PdfShading.Keys.Function] = function;
            shading.Elements[PdfShading.Keys.Extend]   = new PdfLiteral("[true true]");

            // Setup pattern
            pattern.Elements[PdfShadingPattern.Keys.Shading] = shading;
            pattern.Elements[PdfShadingPattern.Keys.Matrix]  = PdfLiteral.FromMatrix(matrix); // new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]");

            string name = writer.Resources.AddPattern(pattern);

            writer.WriteLiteral("/Pattern cs\n", name);
            writer.WriteLiteral("{0} scn\n", name);

            double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha();

            if (alpha < 1)
            {
#if true
                PdfExtGState extGState = writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha);
                string       gs        = writer.Resources.AddExtGState(extGState);
                writer.WriteLiteral("{0} gs\n", gs);
#else
                if (xform == null)
                {
                    PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha);
                    string       gs        = this.writer.Resources.AddExtGState(extGState);
                    this.writer.WriteGraphicState(extGState);
                }
                else
                {
                    //PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form);
                    PdfFormXObject pdfForm = xform.pdfForm;
                    pdfForm.Elements.SetString("/@", "This is the Form XObject of the soft mask");

                    string formName = this.writer.Resources.AddForm(pdfForm);

                    PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner);
                    //this.writer.Owner.Internals.AddObject(tgAttributes);
                    tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB");

                    // Set reference to transparency group attributes
                    pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes);
                    pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]");


                    PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner);
                    this.writer.Owner.Internals.AddObject(softmask);
                    softmask.Elements.SetString("/@", "This is the soft mask");
                    softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity");
                    softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm);
                    //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group");
                    //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group");

                    PdfExtGState extGState = new PdfExtGState(this.writer.Owner);
                    this.writer.Owner.Internals.AddObject(extGState);
                    extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask);
                    this.writer.WriteGraphicState(extGState);
                }
#endif
            }
        }