void RealizeClipPath(XGraphicsPath clipPath)
        {
#if CORE
            DiagnosticsHelper.HandleNotImplemented("RealizeClipPath");
#endif
#if GDI
            // Do not render an empty path.
            if (clipPath._gdipPath.PointCount < 0)
            {
                return;
            }
#endif
#if WPF
            // Do not render an empty path.
            if (clipPath._pathGeometry.Bounds.IsEmpty)
            {
                return;
            }
#endif
            _renderer.BeginGraphicMode();
            RealizeCtm();
#if CORE
            _renderer.AppendPath(clipPath._corePath);
#endif
#if GDI && !WPF
            _renderer.AppendPath(clipPath._gdipPath);
#endif
#if WPF && !GDI
            _renderer.AppendPath(clipPath._pathGeometry);
#endif
#if WPF && GDI
            if (_renderer.Gfx.TargetContext == XGraphicTargetContext.GDI)
            {
                _renderer.AppendPath(clipPath._gdipPath);
            }
            else
            {
                _renderer.AppendPath(clipPath._pathGeometry);
            }
#endif
            _renderer.Append(clipPath.FillMode == XFillMode.Winding ? "W n\n" : "W* n\n");
        }
Example #2
0
        internal void Render(XUnit x, XUnit y, XUnit width, XUnit height, RoundedCorner roundedCorner)
        {
            // If there is no rounded corner, we can use the usual Render method.
            if (roundedCorner == RoundedCorner.None)
            {
                Render(x, y, width, height);
                return;
            }

            if (_shading == null || _brush == null)
            {
                return;
            }

            XGraphicsPath path = new XGraphicsPath();

            switch (roundedCorner)
            {
            case RoundedCorner.TopLeft:
                path.AddArc(new XRect(x, y, width * 2, height * 2), 180, 90);     // Error in CORE: _corePath.AddArc().
                path.AddLine(new XPoint(x + width, y), new XPoint(x + width, y + height));
                break;

            case RoundedCorner.TopRight:
                path.AddArc(new XRect(x - width, y, width * 2, height * 2), 270, 90);     // Error in CORE: _corePath.AddArc().
                path.AddLine(new XPoint(x + width, y + height), new XPoint(x, y + height));
                break;

            case RoundedCorner.BottomRight:
                path.AddArc(new XRect(x - width, y - height, width * 2, height * 2), 0, 90);     // Error in CORE: _corePath.AddArc().
                path.AddLine(new XPoint(x, y + height), new XPoint(x, y));
                break;

            case RoundedCorner.BottomLeft:
                path.AddArc(new XRect(x, y - height, width * 2, height * 2), 90, 90);     // Error in CORE: _corePath.AddArc().
                path.AddLine(new XPoint(x, y), new XPoint(x + width, y));
                break;
            }

            path.CloseFigure();
            _gfx.DrawPath(_brush, path);
        }
Example #3
0
        public static void AddLine(XGraphicsPath path, LineSegment segment, Random random, bool straightEdges)
        {
//      if (Settings.HandDrawnDoc && !straightEdges)
            if (!straightEdges)
            {
                var dx       = segment.End.X - segment.Start.X;
                var dy       = segment.End.Y - segment.Start.Y;
                var distance = (float)Math.Sqrt(dx * dx + dy * dy);
                var points   = random.Next(Math.Max(3, (int)(distance / 15)), Math.Max(6, (int)(distance / 8)));
                var lines    = points - 1;
                var last     = segment.Start;
                for (var line = 0; line < lines; ++line)
                {
                    Vector next;
                    if (line == 0)
                    {
                        next = last;
                    }
                    else if (line == lines - 1)
                    {
                        next = segment.End;
                    }
                    else
                    {
                        var fraction = line / (float)(lines - 1);
                        var x        = segment.Start.X + (segment.End.X - segment.Start.X) * fraction;
                        var y        = segment.Start.Y + (segment.End.Y - segment.Start.Y) * fraction;

                        x   += random.Next(-1, 2);
                        y   += random.Next(-1, 2);
                        next = new Vector(x, y);
                    }

                    path.AddLine(last.ToPointF(), next.ToPointF());
                    last = next;
                }
            }
            else
            {
                path.AddLine(segment.Start.ToPointF(), segment.End.ToPointF());
            }
        }
Example #4
0
        public static void DrawChevron(XGraphics graphics, PointF pos, float angle, float size, Brush fillBrush)
        {
            if (m_chevronPath == null)
            {
                var apex        = new PointF(0.5f, 0);
                var leftCorner  = new PointF(-0.5f, 0.5f);
                var rightCorner = new PointF(-0.5f, -0.5f);
                m_chevronPath = new XGraphicsPath();
                m_chevronPath.AddLine(apex, rightCorner);
                m_chevronPath.AddLine(rightCorner, leftCorner);
                m_chevronPath.AddLine(leftCorner, apex);
            }
            var state = graphics.Save();

            graphics.TranslateTransform(pos.X, pos.Y);
            graphics.RotateTransform(angle);
            graphics.ScaleTransform(size, size);
            graphics.DrawPath(fillBrush, m_chevronPath);
            graphics.Restore(state);
        }
Example #5
0
        public static void AddLine(XGraphicsPath path, LineSegment segment, Random random)
        {
            if (Settings.HandDrawn)
            {
                float  dx       = segment.End.X - segment.Start.X;
                float  dy       = segment.End.Y - segment.Start.Y;
                float  distance = (float)Math.Sqrt(dx * dx + dy * dy);
                int    points   = random.Next(Math.Max(3, (int)(distance / 15)), Math.Max(6, (int)(distance / 8)));
                int    lines    = points - 1;
                Vector last     = segment.Start;
                for (int line = 0; line < lines; ++line)
                {
                    Vector next;
                    if (line == 0)
                    {
                        next = last;
                    }
                    else if (line == lines - 1)
                    {
                        next = segment.End;
                    }
                    else
                    {
                        float fraction = (float)line / (float)(lines - 1);
                        float x        = segment.Start.X + (segment.End.X - segment.Start.X) * fraction;
                        float y        = segment.Start.Y + (segment.End.Y - segment.Start.Y) * fraction;

                        x   += random.Next(-1, 2);
                        y   += random.Next(-1, 2);
                        next = new Vector(x, y);
                    }

                    path.AddLine(last.ToPointF(), next.ToPointF());
                    last = next;
                }
            }
            else
            {
                path.AddLine(segment.Start.ToPointF(), segment.End.ToPointF());
            }
        }
Example #6
0
        private void DrawPathAlternateAndWinding(XGraphics gfx, int number)
        {
            base.BeginBox(gfx, number, "DrawPath (alternate / winding)");
            XPen          pen           = new XPen(XColors.Navy, 2.5);
            XGraphicsPath xGraphicsPath = new XGraphicsPath();

            xGraphicsPath.FillMode = XFillMode.Alternate;
            xGraphicsPath.AddLine(10, 130, 10, 40);
            xGraphicsPath.AddBeziers(new XPoint[]
            {
                new XPoint(10.0, 40.0),
                new XPoint(30.0, 0.0),
                new XPoint(40.0, 20.0),
                new XPoint(60.0, 40.0),
                new XPoint(80.0, 60.0),
                new XPoint(100.0, 60.0),
                new XPoint(120.0, 40.0)
            });
            xGraphicsPath.AddLine(120, 40, 120, 130);
            xGraphicsPath.CloseFigure();
            xGraphicsPath.AddEllipse(40, 80, 50, 40);
            gfx.DrawPath(pen, XBrushes.DarkOrange, xGraphicsPath);
            xGraphicsPath          = new XGraphicsPath();
            xGraphicsPath.FillMode = XFillMode.Winding;
            xGraphicsPath.AddLine(130, 130, 130, 40);
            xGraphicsPath.AddBeziers(new XPoint[]
            {
                new XPoint(130.0, 40.0),
                new XPoint(150.0, 0.0),
                new XPoint(160.0, 20.0),
                new XPoint(180.0, 40.0),
                new XPoint(200.0, 60.0),
                new XPoint(220.0, 60.0),
                new XPoint(240.0, 40.0)
            });
            xGraphicsPath.AddLine(240, 40, 240, 130);
            xGraphicsPath.CloseFigure();
            xGraphicsPath.AddEllipse(160, 80, 50, 40);
            gfx.DrawPath(pen, XBrushes.DarkOrange, xGraphicsPath);
            base.EndBox(gfx);
        }
Example #7
0
        void RenderClipPath(XGraphics gfx)
        {
            gfx.TranslateTransform(15, 20);

            XGraphicsPath path = new XGraphicsPath();

            path.AddString("Clip!", new XFontFamily("Verdana"), XFontStyle.Bold, 90, new XRect(0, 0, 250, 140),
                           XStringFormats.Center);

            gfx.IntersectClip(path);

            gfx.DrawRectangle(XBrushes.LightSalmon, new XRect(0, 0, 10000, 10000));
            // Draw a beam of dotted lines
            XPen pen = XPens.DarkRed.Clone();

            pen.DashStyle = XDashStyle.Dot;
            for (double r = 0; r <= 90; r += 0.5)
            {
                gfx.DrawLine(pen, 0, 0, 1000 * Math.Cos(r / 90 * Math.PI), 1000 * Math.Sin(r / 90 * Math.PI));
            }
        }
Example #8
0
        public static XGraphicsPath GetPath(PathEx pathEx)
        {
            var path = new XGraphicsPath();

            path.FillMode = (XFillMode)Enum.Parse(typeof(XFillMode), pathEx.FillMode.ToString());
            path.StartFigure();
            var start = new XPoint();

            foreach (var segment in pathEx.Segments)
            {
                switch (segment.Type)
                {
                case PathEx.SegmentType.MoveTo:
                    start = PdfConverter.Convert(segment.Point1.ToPoint());
                    break;

                case PathEx.SegmentType.LineTo:
                    var end = PdfConverter.Convert(segment.Point1.ToPoint());
                    path.AddLine(start, end);
                    start = end;
                    break;

                case PathEx.SegmentType.BezierTo:
                    var p1   = PdfConverter.Convert(segment.Point1.ToPoint());
                    var p2   = PdfConverter.Convert(segment.Point2.ToPoint());
                    var bEnd = PdfConverter.Convert(segment.Point3.ToPoint());
                    path.AddBezier(start, p1, p2, bEnd);
                    start = bEnd;
                    break;

                case PathEx.SegmentType.FigureEnd:
                    if (segment.Closed)
                    {
                        path.CloseFigure();
                    }
                    break;
                }
            }
            return(path);
        }
Example #9
0
        /// <summary>
        /// Clips through path.
        /// </summary>
        void DrawClipPath(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "Clip through Path");

#if CORE
            DrawMessage(gfx, "AddString is not implemented in PDFsharp Core.");
#else
            var path = new XGraphicsPath();
            path.AddString("Clip!", new XFontFamily("Verdana"), XFontStyle.Bold, 90, new XRect(0, 0, 250, 140), XStringFormats.Center);
            gfx.IntersectClip(path);

            // Draw a beam of dotted lines.
            var pen = XPens.DarkRed.Clone();
            pen.DashStyle = XDashStyle.Dot;
            for (double r = 0; r <= 90; r += 0.5)
            {
                gfx.DrawLine(pen, 0, 0, 250 * Math.Cos(r / 90 * Math.PI), 250 * Math.Sin(r / 90 * Math.PI));
            }
#endif

            EndBox(gfx);
        }
Example #10
0
        static void DrawClipPath(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "Clip through Path");

            XGraphicsPath path = new XGraphicsPath();

            path.AddString("Clip!", new XFontFamily("Verdana"), XFontStyle.Bold, 90, new XRect(0, 0, 250, 140),
                           XStringFormats.Center);

            gfx.IntersectClip(path);

            // Draw a beam of dotted lines
            XPen pen = XPens.DarkRed.Clone();

            pen.DashStyle = XDashStyle.Dot;
            for (double r = 0; r <= 90; r += 0.5)
            {
                gfx.DrawLine(pen, 0, 0, 250 * System.Math.Cos(r / 90 * System.Math.PI), 250 * System.Math.Sin(r / 90 * System.Math.PI));
            }

            EndBox(gfx);
        }
        public static void Variant3(PdfPage page, XFont font, string watermark)
        {
            // Variation 3: Draw a watermark as a transparent graphical path above text.
            // NYI: Does not work in Core build.

            // Get an XGraphics object for drawing above the existing content.
            var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);

            // Get the size (in points) of the text.
            var size = gfx.MeasureString(watermark, font);

            // Define a rotation transformation at the center of the page.
            gfx.TranslateTransform(page.Width / 2, page.Height / 2);
            gfx.RotateTransform(-System.Math.Atan(page.Height / page.Width) * 180 / System.Math.PI);
            gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

            // Create a graphical path.
            var path = new XGraphicsPath();

            // Create a string format.
            var format = new XStringFormat();

            format.Alignment     = XStringAlignment.Near;
            format.LineAlignment = XLineAlignment.Near;

            // Add the text to the path.
            // AddString is not implemented in PDFsharp Core.
            path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
                           new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                           format);

            // Create a dimmed red pen and brush.
            var    pen   = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
            XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));

            // Stroke the outline of the path.
            gfx.DrawPath(pen, brush, path);
        }
Example #12
0
        public override XPoint AddToPath(XGraphicsPath path, XPoint cursor, Base lastPathCommand = null)
        {
            var previousControlPoint = new PointF((float)cursor.X, (float)cursor.Y);

            if (lastPathCommand is BezierCurve bezierCurve)
            {
                previousControlPoint = bezierCurve.Control2;
            }
            else if (lastPathCommand is QuadraticCurve quadraticCurve)
            {
                previousControlPoint = quadraticCurve.Control1;
            }
            else if (lastPathCommand is ShortcutBezierCurve shortcutBezierCurve)
            {
                previousControlPoint = shortcutBezierCurve.Control2;
            }
            else if (lastPathCommand is ShortcutQuadraticCurve shortcutQuadraticCurve)
            {
                previousControlPoint = shortcutQuadraticCurve.Control2;
            }
            var deltaX   = cursor.X - previousControlPoint.X;
            var deltaY   = cursor.Y - previousControlPoint.Y;
            var control1 = new XPoint(cursor.X + deltaX, cursor.Y + deltaY);

            var cp1x = cursor.X + 2.0 / 3.0 * (control1.X - cursor.X);
            var cp1y = cursor.Y + 2.0 / 3.0 * (control1.Y - cursor.Y);

            var cp2x = End.X + 2.0 / 3.0 * (control1.X - End.X);
            var cp2y = End.Y + 2.0 / 3.0 * (control1.Y - End.Y);

            path.AddBezier(cursor, new XPoint(cp1x, cp1y), new XPoint(cp2x, cp2y), End.ToXPoint());
            //path.AddBezier(cursor, control1, control1, End.ToXPoint());

            // for reference by future ShortcutQuadraticCurve elements.
            Control2 = new PointF((float)control1.X, (float)control1.Y);

            return(End.ToXPoint());
        }
        public void Fill(XGraphics graphics, RectangleF rect, Brush fillBrush)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            RectangleF bounds = TransformedBounds;

            if (bounds.IntersectsWith(rect))
            {
                XGraphicsPath path = Path;

                using (RenderUtil.SaveState(graphics))
                {
                    XMatrix matrix = new XMatrix();
                    matrix.ScalePrepend(ScaleX, ScaleY);
                    matrix.TranslatePrepend(-OriginX, -OriginY);
                    graphics.MultiplyTransform(matrix);
                    graphics.DrawPath(fillBrush, path);
                }
            }
        }
Example #14
0
        private void DrawVectorLayer(XGraphics gfx, Map map, double scaleFactor, VectorLayer lyr, string lineColor, double lineWidth, string fillColor)
        {
            TransformsProvider tp      = new TransformsProvider(gfx);
            BoundingBox        geombox = map.GetExtents();

            XPen        xp = new XPen(XColor.FromName(lineColor), lineWidth);
            XSolidBrush xb = new XSolidBrush(XColor.FromName(fillColor));

            foreach (Geometry geom in (lyr as VectorLayer).DataSource.GetGeometriesInView(geombox))
            {
                if ((geom) is MultiLineString)
                {
                    XGraphicsPath g = tp.TransformGeom(scaleFactor, (MultiLineString)geom, geombox);
                    this._geomCache.Add(new PDFGeom(g, xp, null));
                    gfx.DrawPath(xp, g);
                }
                if ((geom) is MultiPolygon)
                {
                    XGraphicsPath g = tp.TransformGeom(scaleFactor, (MultiPolygon)geom, geombox);
                    this._geomCache.Add(new PDFGeom(g, xp, xb));
                    gfx.DrawPath(xp, xb, g);
                }
            }
        }
        public void Draw(XGraphics graphics, RectangleF rect, MapOptions options, XPen pen)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            RectangleF bounds = TransformedBounds;

            //graphics.DrawRectangle( new XPen(XColors.Yellow, 1), bounds.X, bounds.Y, bounds.Width, bounds.Height );

            if (bounds.IntersectsWith(rect))
            {
                XGraphicsPath path = Path;
                using (RenderUtil.SaveState(graphics))
                {
                    XMatrix matrix = new XMatrix();
                    matrix.ScalePrepend(ScaleX, ScaleY);
                    matrix.TranslatePrepend(-OriginX, -OriginY);
                    graphics.MultiplyTransform(matrix, XMatrixOrder.Prepend);
                    graphics.DrawPath(pen, path);
                }
            }
        }
Example #16
0
        static void Main()
        {
            const string watermark = "PDFsharp";
            const int    emSize    = 150;

            // Get a fresh copy of the sample PDF file
            const string filename = "Portable Document Format.pdf";

            File.Copy(Path.Combine("../../../../../PDFs/", filename),
                      Path.Combine(Directory.GetCurrentDirectory(), filename), true);

            // Create the font for drawing the watermark
            XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);

            // Open an existing document for editing and loop through its pages
            PdfDocument document = PdfReader.Open(filename);

            // Set version to PDF 1.4 (Acrobat 5) because we use transparency.
            if (document.Version < 14)
            {
                document.Version = 14;
            }

            for (int idx = 0; idx < document.Pages.Count; idx++)
            {
                //if (idx == 1) break;
                PdfPage page = document.Pages[idx];

                switch (idx % 3)
                {
                case 0:
                {
                    // Variation 1: Draw watermark as text string

                    // Get an XGraphics object for drawing beneath the existing content
                    XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

#if true_
                    // Fill background with linear gradient color
                    XRect rect = page.MediaBox.ToXRect();
                    XLinearGradientBrush gbrush = new XLinearGradientBrush(rect,
                                                                           XColors.LightSalmon, XColors.WhiteSmoke, XLinearGradientMode.Vertical);
                    gfx.DrawRectangle(gbrush, rect);
#endif

                    // Get the size (in point) of the text
                    XSize size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a string format
                    XStringFormat format = new XStringFormat();
                    format.Alignment     = XStringAlignment.Near;
                    format.LineAlignment = XLineAlignment.Near;

                    // Create a dimmed red brush
                    XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

                    // Draw the string
                    gfx.DrawString(watermark, font, brush,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   format);
                }
                break;

                case 1:
                {
                    // Variation 2: Draw watermark as outlined graphical path

                    // Get an XGraphics object for drawing beneath the existing content
                    XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

                    // Get the size (in point) of the text
                    XSize size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a graphical path
                    XGraphicsPath path = new XGraphicsPath();

                    // Add the text to the path
                    path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   XStringFormats.Default);

                    // Create a dimmed red pen
                    XPen pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2);

                    // Stroke the outline of the path
                    gfx.DrawPath(pen, path);
                }
                break;

                case 2:
                {
                    // Variation 3: Draw watermark as transparent graphical path above text

                    // Get an XGraphics object for drawing above the existing content
                    XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);

                    // Get the size (in point) of the text
                    XSize size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a graphical path
                    XGraphicsPath path = new XGraphicsPath();

                    // Add the text to the path
                    path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   XStringFormats.Default);

                    // Create a dimmed red pen and brush
                    XPen   pen   = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
                    XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));

                    // Stroke the outline of the path
                    gfx.DrawPath(pen, brush, path);
                }
                break;
                }
            }
            // Save the document...
            document.Save(filename);
            // ...and start a viewer
            Process.Start(filename);
        }
Example #17
0
 public virtual XPoint AddToPath(XGraphicsPath path, XPoint cursor, Base lastPathCommand = null)
 => throw new NotImplementedException();
Example #18
0
        void RealizeClipPath(XGraphicsPath clipPath)
        {
#if CORE
            DiagnosticsHelper.HandleNotImplemented("RealizeClipPath");
#endif
#if GDI
            // Do not render an empty path.
            if (clipPath._gdipPath.PointCount < 0)
                return;
#endif
#if WPF
            // Do not render an empty path.
            if (clipPath._pathGeometry.Bounds.IsEmpty)
                return;
#endif
            _renderer.BeginGraphicMode();
            RealizeCtm();
#if CORE
            _renderer.AppendPath(clipPath._corePath);
#endif
#if GDI && !WPF
            _renderer.AppendPath(clipPath._gdipPath);
#endif
#if WPF && !GDI
            _renderer.AppendPath(clipPath._pathGeometry);
#endif
#if WPF && GDI
            if (_renderer.Gfx.TargetContext == XGraphicTargetContext.GDI)
                _renderer.AppendPath(clipPath._gdipPath);
            else
                _renderer.AppendPath(clipPath._pathGeometry);
#endif
            _renderer.Append(clipPath.FillMode == XFillMode.Winding ? "W n\n" : "W* n\n");
        }
 internal XGraphicsPathInternals(XGraphicsPath path)
 {
     _path = path;
 }
    // ----- DrawPath -----------------------------------------------------------------------------

    public void DrawPath(XPen pen, XBrush brush, XGraphicsPath path)
    {
      if (pen == null && brush == null)
        throw new ArgumentNullException("pen and brush");

      Realize(pen, brush);
      AppendPath(path.gdipPath);
      AppendStrokeFill(pen, brush, path.FillMode, false);
    }
    void RealizeClipPath(XGraphicsPath clipPath)
    {
      this.renderer.BeginGraphic();
      RealizeCtm();
#if GDI && !WPF
      this.renderer.AppendPath(clipPath.gdipPath);
#endif
#if WPF &&!GDI
      this.renderer.AppendPath(clipPath.pathGeometry);
#endif
#if WPF && GDI
      if (this.renderer.Gfx.targetContext == XGraphicTargetContext.GDI)
      {
        this.renderer.AppendPath(clipPath.gdipPath);
      }
      else
      {
        this.renderer.AppendPath(clipPath.pathGeometry);
      }
#endif
      if (clipPath.FillMode == XFillMode.Winding)
        this.renderer.Append("W n\n");
      else
        this.renderer.Append("W* n\n");
    }
 public void SetAndRealizeClipRect(XRect clipRect)
 {
   XGraphicsPath clipPath = new XGraphicsPath();
   clipPath.AddRectangle(clipRect);
   RealizeClipPath(clipPath);
 }
Example #23
0
    // --------------------------------------------------------------------------------------------

    #region Clipping

    public void SetClip(XGraphicsPath path, XCombineMode combineMode)
    {
      if (path == null)
        throw new NotImplementedException("SetClip with no path.");

      // Ensure that the graphics state stack level is at least 2, because otherwise an error
      // occurs when someone set the clip region before something was drawn.
      if (this.gfxState.Level < GraphicsStackLevelWorldSpace)
        RealizeTransform();  // TODO: refactor this function

      if (combineMode == XCombineMode.Replace)
      {
        if (this.clipLevel != 0)
        {
          if (this.clipLevel != this.gfxState.Level)
            throw new NotImplementedException("Cannot set new clip region in an inner graphic state level.");
          else
            ResetClip();
        }
        this.clipLevel = this.gfxState.Level;
      }
      else if (combineMode == XCombineMode.Intersect)
      {
        if (this.clipLevel == 0)
          this.clipLevel = this.gfxState.Level;
      }
      else
      {
        Debug.Assert(false, "Invalid XCombineMode in internal function.");
      }
      this.gfxState.SetAndRealizeClipPath(path);
    }
Example #24
0
    // ----- DrawPath -----------------------------------------------------------------------------

    public void DrawPath(XPen pen, XBrush brush, XGraphicsPath path)
    {
      if (pen == null && brush == null)
        throw new ArgumentNullException("pen");

#if GDI && !WPF
      Realize(pen, brush);
      AppendPath(path.gdipPath);
      AppendStrokeFill(pen, brush, path.FillMode, false);
#endif
#if WPF && !GDI
      Realize(pen, brush);
      AppendPath(path.pathGeometry);
      AppendStrokeFill(pen, brush, path.FillMode, false);
#endif
#if WPF && GDI
      Realize(pen, brush);
      if (this.gfx.targetContext == XGraphicTargetContext.GDI)
        AppendPath(path.gdipPath);
      else
        AppendPath(path.pathGeometry);
      AppendStrokeFill(pen, brush, path.FillMode, false);
#endif
    }
Example #25
0
        static void Main()
        {
            const string watermark = "PDFsharp";
            const int    emSize    = 150;

            // Get a fresh copy of the sample PDF file.
            const string filename = "Portable Document Format.pdf";
            var          file     = Path.Combine(Directory.GetCurrentDirectory(), filename);

            File.Copy(Path.Combine("../../../../assets/PDFs/", filename), file, true);

            // Remove ReadOnly attribute from the copy.
            File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);

            // Create the font for drawing the watermark.
            var font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);

            // Open an existing document for editing and loop through its pages.
            var document = PdfReader.Open(filename);

            // Set version to PDF 1.4 (Acrobat 5) because we use transparency.
            if (document.Version < 14)
            {
                document.Version = 14;
            }

            for (var idx = 0; idx < document.Pages.Count; idx++)
            {
                var page = document.Pages[idx];

                switch (idx % 3)
                {
                case 0:
                {
                    // Variation 1: Draw a watermark as a text string.

                    // Get an XGraphics object for drawing beneath the existing content.
                    var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

                    // Get the size (in points) of the text.
                    var size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page.
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a string format.
                    var format = new XStringFormat();
                    format.Alignment     = XStringAlignment.Near;
                    format.LineAlignment = XLineAlignment.Near;

                    // Create a dimmed red brush.
                    XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

                    // Draw the string.
                    gfx.DrawString(watermark, font, brush,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   format);
                }
                break;

                case 1:
                {
                    // Variation 2: Draw a watermark as an outlined graphical path.
                    // NYI: Does not work in Core build.

                    // Get an XGraphics object for drawing beneath the existing content.
                    var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

                    // Get the size (in points) of the text.
                    var size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page.
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a graphical path.
                    var path = new XGraphicsPath();

                    // Create a string format.
                    var format = new XStringFormat();
                    format.Alignment     = XStringAlignment.Near;
                    format.LineAlignment = XLineAlignment.Near;

                    // Add the text to the path.
                    // AddString is not implemented in PDFsharp Core.
                    path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   format);

                    // Create a dimmed red pen.
                    var pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2);

                    // Stroke the outline of the path.
                    gfx.DrawPath(pen, path);
                }
                break;

                case 2:
                {
                    // Variation 3: Draw a watermark as a transparent graphical path above text.
                    // NYI: Does not work in Core build.

                    // Get an XGraphics object for drawing above the existing content.
                    var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);

                    // Get the size (in points) of the text.
                    var size = gfx.MeasureString(watermark, font);

                    // Define a rotation transformation at the center of the page.
                    gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                    gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                    gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                    // Create a graphical path.
                    var path = new XGraphicsPath();

                    // Create a string format.
                    var format = new XStringFormat();
                    format.Alignment     = XStringAlignment.Near;
                    format.LineAlignment = XLineAlignment.Near;

                    // Add the text to the path.
                    // AddString is not implemented in PDFsharp Core.
                    path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
                                   new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                                   format);

                    // Create a dimmed red pen and brush.
                    var    pen   = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
                    XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));

                    // Stroke the outline of the path.
                    gfx.DrawPath(pen, brush, path);
                }
                break;
                }
            }
            // Save the document...
            document.Save(filename);
            // ...and start a viewer
            Process.Start(filename);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="pg"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static XGraphicsPath ToXGraphicsPath(this Test2d.XPathGeometry pg, double dx, double dy, Func <double, double> scale)
        {
            var gp = new XGraphicsPath();

            gp.FillMode = pg.FillRule == Test2d.XFillRule.EvenOdd ? XFillMode.Alternate : XFillMode.Winding;

            foreach (var pf in pg.Figures)
            {
                var startPoint = pf.StartPoint;

                foreach (var segment in pf.Segments)
                {
                    if (segment is Test2d.XArcSegment)
                    {
#if CORE
                        //var arcSegment = segment as Test2d.XArcSegment;
                        // TODO: Convert WPF/SVG elliptical arc segment format to GDI+ bezier curves.
                        //startPoint = arcSegment.Point;
#endif
#if WPF
                        var arcSegment = segment as Test2d.XArcSegment;
                        var point1     = new XPoint(
                            scale(startPoint.X),
                            scale(startPoint.Y));
                        var point2 = new XPoint(
                            scale(arcSegment.Point.X),
                            scale(arcSegment.Point.Y));
                        var size = new XSize(
                            scale(arcSegment.Size.Width),
                            scale(arcSegment.Size.Height));
                        gp.AddArc(
                            point1,
                            point2,
                            size, arcSegment.RotationAngle, arcSegment.IsLargeArc,
                            arcSegment.SweepDirection == Test2d.XSweepDirection.Clockwise ? XSweepDirection.Clockwise : XSweepDirection.Counterclockwise);
                        startPoint = arcSegment.Point;
#endif
                    }
                    else if (segment is Test2d.XBezierSegment)
                    {
                        var bezierSegment = segment as Test2d.XBezierSegment;
                        gp.AddBezier(
                            scale(startPoint.X),
                            scale(startPoint.Y),
                            scale(bezierSegment.Point1.X),
                            scale(bezierSegment.Point1.Y),
                            scale(bezierSegment.Point2.X),
                            scale(bezierSegment.Point2.Y),
                            scale(bezierSegment.Point3.X),
                            scale(bezierSegment.Point3.Y));
                        startPoint = bezierSegment.Point3;
                    }
                    else if (segment is Test2d.XLineSegment)
                    {
                        var lineSegment = segment as Test2d.XLineSegment;
                        gp.AddLine(
                            scale(startPoint.X),
                            scale(startPoint.Y),
                            scale(lineSegment.Point.X),
                            scale(lineSegment.Point.Y));
                        startPoint = lineSegment.Point;
                    }
                    else if (segment is Test2d.XPolyBezierSegment)
                    {
                        var polyBezierSegment = segment as Test2d.XPolyBezierSegment;
                        if (polyBezierSegment.Points.Count >= 3)
                        {
                            gp.AddBezier(
                                scale(startPoint.X),
                                scale(startPoint.Y),
                                scale(polyBezierSegment.Points[0].X),
                                scale(polyBezierSegment.Points[0].Y),
                                scale(polyBezierSegment.Points[1].X),
                                scale(polyBezierSegment.Points[1].Y),
                                scale(polyBezierSegment.Points[2].X),
                                scale(polyBezierSegment.Points[2].Y));
                        }

                        if (polyBezierSegment.Points.Count > 3 &&
                            polyBezierSegment.Points.Count % 3 == 0)
                        {
                            for (int i = 3; i < polyBezierSegment.Points.Count; i += 3)
                            {
                                gp.AddBezier(
                                    scale(polyBezierSegment.Points[i - 1].X),
                                    scale(polyBezierSegment.Points[i - 1].Y),
                                    scale(polyBezierSegment.Points[i].X),
                                    scale(polyBezierSegment.Points[i].Y),
                                    scale(polyBezierSegment.Points[i + 1].X),
                                    scale(polyBezierSegment.Points[i + 1].Y),
                                    scale(polyBezierSegment.Points[i + 2].X),
                                    scale(polyBezierSegment.Points[i + 2].Y));
                            }
                        }

                        startPoint = polyBezierSegment.Points.Last();
                    }
                    else if (segment is Test2d.XPolyLineSegment)
                    {
                        var polyLineSegment = segment as Test2d.XPolyLineSegment;
                        if (polyLineSegment.Points.Count >= 1)
                        {
                            gp.AddLine(
                                scale(startPoint.X),
                                scale(startPoint.Y),
                                scale(polyLineSegment.Points[0].X),
                                scale(polyLineSegment.Points[0].Y));
                        }

                        if (polyLineSegment.Points.Count > 1)
                        {
                            for (int i = 1; i < polyLineSegment.Points.Count; i++)
                            {
                                gp.AddLine(
                                    scale(polyLineSegment.Points[i - 1].X),
                                    scale(polyLineSegment.Points[i - 1].Y),
                                    scale(polyLineSegment.Points[i].X),
                                    scale(polyLineSegment.Points[i].Y));
                            }
                        }

                        startPoint = polyLineSegment.Points.Last();
                    }
                    else if (segment is Test2d.XPolyQuadraticBezierSegment)
                    {
                        var polyQuadraticSegment = segment as Test2d.XPolyQuadraticBezierSegment;
                        if (polyQuadraticSegment.Points.Count >= 2)
                        {
                            var    p1 = startPoint;
                            var    p2 = polyQuadraticSegment.Points[0];
                            var    p3 = polyQuadraticSegment.Points[1];
                            double x1 = p1.X;
                            double y1 = p1.Y;
                            double x2 = p1.X + (2.0 * (p2.X - p1.X)) / 3.0;
                            double y2 = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0;
                            double x3 = x2 + (p3.X - p1.X) / 3.0;
                            double y3 = y2 + (p3.Y - p1.Y) / 3.0;
                            double x4 = p3.X;
                            double y4 = p3.Y;
                            gp.AddBezier(
                                scale(x1 + dx),
                                scale(y1 + dy),
                                scale(x2 + dx),
                                scale(y2 + dy),
                                scale(x3 + dx),
                                scale(y3 + dy),
                                scale(x4 + dx),
                                scale(y4 + dy));
                        }

                        if (polyQuadraticSegment.Points.Count > 2 &&
                            polyQuadraticSegment.Points.Count % 2 == 0)
                        {
                            for (int i = 3; i < polyQuadraticSegment.Points.Count; i += 3)
                            {
                                var    p1 = polyQuadraticSegment.Points[i - 1];
                                var    p2 = polyQuadraticSegment.Points[i];
                                var    p3 = polyQuadraticSegment.Points[i + 1];
                                double x1 = p1.X;
                                double y1 = p1.Y;
                                double x2 = p1.X + (2.0 * (p2.X - p1.X)) / 3.0;
                                double y2 = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0;
                                double x3 = x2 + (p3.X - p1.X) / 3.0;
                                double y3 = y2 + (p3.Y - p1.Y) / 3.0;
                                double x4 = p3.X;
                                double y4 = p3.Y;
                                gp.AddBezier(
                                    scale(x1 + dx),
                                    scale(y1 + dy),
                                    scale(x2 + dx),
                                    scale(y2 + dy),
                                    scale(x3 + dx),
                                    scale(y3 + dy),
                                    scale(x4 + dx),
                                    scale(y4 + dy));
                            }
                        }

                        startPoint = polyQuadraticSegment.Points.Last();
                    }
                    else if (segment is Test2d.XQuadraticBezierSegment)
                    {
                        var    qbezierSegment = segment as Test2d.XQuadraticBezierSegment;
                        var    p1             = startPoint;
                        var    p2             = qbezierSegment.Point1;
                        var    p3             = qbezierSegment.Point2;
                        double x1             = p1.X;
                        double y1             = p1.Y;
                        double x2             = p1.X + (2.0 * (p2.X - p1.X)) / 3.0;
                        double y2             = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0;
                        double x3             = x2 + (p3.X - p1.X) / 3.0;
                        double y3             = y2 + (p3.Y - p1.Y) / 3.0;
                        double x4             = p3.X;
                        double y4             = p3.Y;
                        gp.AddBezier(
                            scale(x1 + dx),
                            scale(y1 + dy),
                            scale(x2 + dx),
                            scale(y2 + dy),
                            scale(x3 + dx),
                            scale(y3 + dy),
                            scale(x4 + dx),
                            scale(y4 + dy));
                        startPoint = qbezierSegment.Point2;
                    }
                    else
                    {
                        throw new NotSupportedException("Not supported segment type: " + segment.GetType());
                    }
                }

                if (pf.IsClosed)
                {
                    gp.CloseFigure();
                }
                else
                {
                    gp.StartFigure();
                }
            }

            return(gp);
        }
Example #27
0
        /// <summary>
        /// Demonstrates the use of XGraphics.Transform.
        /// </summary>
        public override void RenderPage(XGraphics gfx)
        {
            XGraphicsState state1, state2;

            base.RenderPage(gfx);

            state1 = gfx.Save(); // Level 1
            gfx.TranslateTransform(20, 50);
            gfx.DrawLine(XPens.Blue, 0, 0, 10, 10);
            gfx.Restore(state1);

            state1 = gfx.Save(); // Level 2
            gfx.TranslateTransform(220, 50);
            gfx.DrawLine(XPens.Blue, 0, 0, 10, 10);
            XGraphicsPath clipPath = new XGraphicsPath();

            clipPath.AddPie(0, 10, 150, 100, -50, 100);
            gfx.IntersectClip(clipPath);
            gfx.DrawRectangle(XBrushes.LightYellow, 0, 0, 1000, 1000);

            state2 = gfx.Save(); // Level 3
            gfx.ScaleTransform(10);
            gfx.DrawLine(XPens.Red, 1, 1, 10, 10);

            //gfx.ResetClip();
            gfx.Restore(state2); // Level 2

            gfx.DrawLine(XPens.Red, 1, 1, 10, 10);

            gfx.Restore(state1);

#if true_
            gfx.SetClip(new XRect(20, 20, 300, 500));
            gfx.DrawRectangle(XBrushes.Yellow, 0, 0, gfx.PageSize.Width, gfx.PageSize.Height);

            gfx.SetClip(new XRect(100, 200, 300, 500), XCombineMode.Intersect);
            gfx.DrawRectangle(XBrushes.LightBlue, 0, 0, gfx.PageSize.Width, gfx.PageSize.Height);

            gfx.DrawLine(XPens.MediumSlateBlue, 0, 0, 150, 200);
            gfx.DrawPolygon(properties.Pen1.Pen, GetPentagram(75, new PointF(150, 200)));


            Matrix matrix = new Matrix();
            //matrix.Scale(2f, 1.5f);
            //matrix.Translate(-200, -400);
            //matrix.Rotate(45);
            //matrix.Translate(200, 400);
            //gfx.Transform = matrix;
            //gfx.TranslateTransform(50, 30);

#if true
            gfx.TranslateTransform(30, 40, XMatrixOrder.Prepend);
            gfx.ScaleTransform(2.0f, 2.0f, XMatrixOrder.Prepend);
            gfx.RotateTransform(15, XMatrixOrder.Prepend);
#else
            gfx.TranslateTransform(30, 40, XMatrixOrder.Append);
            gfx.ScaleTransform(2.0f, 2.0f, XMatrixOrder.Append);
            gfx.RotateTransform(15, XMatrixOrder.Append);
#endif
            bool id = matrix.IsIdentity;
            matrix.Scale(2.0f, 2.0f, MatrixOrder.Prepend);
            //matrix.Translate(30, -50);
            matrix.Rotate(15, MatrixOrder.Prepend);
            Matrix mtx = gfx.Transform.ToMatrix();
            //gfx.Transform = matrix;

            gfx.DrawLine(XPens.MediumSlateBlue, 0, 0, 150, 200);
            gfx.DrawPolygon(properties.Pen2.Pen, GetPentagram(75, new PointF(150, 200)));

            gfx.ResetClip();

            gfx.DrawLine(XPens.Red, 0, 0, 1000, 1000);

            gfx.DrawPolygon(XPens.SandyBrown, GetPentagram(75, new PointF(150, 200)));
#endif
        }
Example #28
0
 public abstract void Add(XGraphicsPath path);
 /// <summary>
 /// Draws a line specified by path.
 /// </summary>
 public void DrawPath(XGraphicsPath path)
 {
     if (_pen != null)
         _gfx.DrawPath(_pen, path);
 }
Example #30
0
        /// <summary>
        /// Draws the marker given through rendererInfo at the specified position. Position specifies
        /// the center of the marker.
        /// </summary>
        internal static void Draw(XGraphics graphics, XPoint pos, MarkerRendererInfo rendererInfo)
        {
#if SILVERLIGHT
            return; // BUG: Code crashs Silverlight Path class.

#pragma warning disable 0162
#endif

            if (rendererInfo.MarkerStyle == MarkerStyle.None)
            {
                return;
            }

            double size = rendererInfo.MarkerSize;
            double size2 = size / 2;
            double x0, y0, x1, y1;
            double g;

            XPen   foreground = new XPen(rendererInfo.MarkerForegroundColor, 0.5);
            XBrush background = new XSolidBrush(rendererInfo.MarkerBackgroundColor);

            XGraphicsPath gp = new XGraphicsPath();
            switch (rendererInfo.MarkerStyle)
            {
            case MarkerStyle.Square:
                x0 = pos.X - size2;
                y0 = pos.Y - size2;
                x1 = pos.X + size2;
                y1 = pos.Y + size2;
                gp.AddLine(x0, y0, x1, y0);
                gp.AddLine(x1, y0, x1, y1);
                gp.AddLine(x1, y1, x0, y1);
                gp.AddLine(x0, y1, x0, y0);
                break;

            case MarkerStyle.Diamond:
                gp.AddLine(x1                   = pos.X + size2, pos.Y, pos.X, y0 = pos.Y - size2);
                gp.AddLine(pos.X, y0, x0        = pos.X - size2, pos.Y);
                gp.AddLine(x0, pos.Y, pos.X, y1 = pos.Y + size2);
                gp.AddLine(pos.X, y1, x1, pos.Y);
                break;

            case MarkerStyle.Triangle:
                y0 = pos.Y + size / 2;
                y1 = pos.Y - size / 2;
                g  = Math.Sqrt(size * size * 4 / 3) / 2;
                gp.AddLine(pos.X, y1, pos.X + g, y0);
                gp.AddLine(pos.X + g, y0, pos.X - g, y0);
                gp.AddLine(pos.X - g, y0, pos.X, y1);
                break;

            case MarkerStyle.Plus:
                g = size2 / 4;
                gp.AddLine(pos.X - size2, pos.Y + g, pos.X - g, pos.Y + g);
                gp.AddLine(pos.X - g, pos.Y + g, pos.X - g, pos.Y + size2);
                gp.AddLine(pos.X - g, pos.Y + size2, pos.X + g, pos.Y + size2);
                gp.AddLine(pos.X + g, pos.Y + size2, pos.X + g, pos.Y + g);
                gp.AddLine(pos.X + g, pos.Y + g, pos.X + size2, pos.Y + g);
                gp.AddLine(pos.X + size2, pos.Y + g, pos.X + size2, pos.Y - g);
                gp.AddLine(pos.X + size2, pos.Y - g, pos.X + g, pos.Y - g);
                gp.AddLine(pos.X + g, pos.Y - g, pos.X + g, pos.Y - size2);
                gp.AddLine(pos.X + g, pos.Y - size2, pos.X - g, pos.Y - size2);
                gp.AddLine(pos.X - g, pos.Y - size2, pos.X - g, pos.Y - g);
                gp.AddLine(pos.X - g, pos.Y - g, pos.X - size2, pos.Y - g);
                gp.AddLine(pos.X - size2, pos.Y - g, pos.X - size2, pos.Y + g);
                break;

            case MarkerStyle.Circle:
            case MarkerStyle.Dot:
                x0 = pos.X - size2;
                y0 = pos.Y - size2;
                gp.AddEllipse(x0, y0, size, size);
                break;

            case MarkerStyle.Dash:
                x0 = pos.X - size2;
                y0 = pos.Y - size2 / 3;
                x1 = pos.X + size2;
                y1 = pos.Y + size2 / 3;
                gp.AddLine(x0, y0, x1, y0);
                gp.AddLine(x1, y0, x1, y1);
                gp.AddLine(x1, y1, x0, y1);
                gp.AddLine(x0, y1, x0, y0);
                break;

            case MarkerStyle.X:
                g = size / 4;
                gp.AddLine(pos.X - size2 + g, pos.Y - size2, pos.X, pos.Y - g);
                gp.AddLine(pos.X, pos.Y - g, pos.X + size2 - g, pos.Y - size2);
                gp.AddLine(pos.X + size2 - g, pos.Y - size2, pos.X + size2, pos.Y - size2 + g);
                gp.AddLine(pos.X + size2, pos.Y - size2 + g, pos.X + g, pos.Y);
                gp.AddLine(pos.X + g, pos.Y, pos.X + size2, pos.Y + size2 - g);
                gp.AddLine(pos.X + size2, pos.Y + size2 - g, pos.X + size2 - g, pos.Y + size2);
                gp.AddLine(pos.X + size2 - g, pos.Y + size2, pos.X, pos.Y + g);
                gp.AddLine(pos.X, pos.Y + g, pos.X - size2 + g, pos.Y + size2);
                gp.AddLine(pos.X - size2 + g, pos.Y + size2, pos.X - size2, pos.Y + size2 - g);
                gp.AddLine(pos.X - size2, pos.Y + size2 - g, pos.X - g, pos.Y);
                gp.AddLine(pos.X - g, pos.Y, pos.X - size2, pos.Y - size2 + g);
                break;

            case MarkerStyle.Star:
            {
                XPoint[] points = new XPoint[10];

                double radStep     = 2 * Math.PI / 5;
                double outerCircle = size / 2;
                double innerCircle = size / 5;
                // outer circle
                double rad = -(Math.PI / 2);         // 90�
                for (int idx = 0; idx < 10; idx += 2)
                {
                    points[idx].X = pos.X + outerCircle * Math.Cos(rad);
                    points[idx].Y = pos.Y + outerCircle * Math.Sin(rad);
                    rad          += radStep;
                }

                // inner circle
                rad = -(Math.PI / 4);         // 45�
                double x = innerCircle * Math.Cos(rad);
                double y = innerCircle * Math.Sin(rad);
                points[1].X = pos.X + x;
                points[1].Y = pos.Y + y;
                points[9].X = pos.X - x;
                points[9].Y = pos.Y + y;
                rad        += radStep;
                x           = innerCircle * Math.Cos(rad);
                y           = innerCircle * Math.Sin(rad);
                points[3].X = pos.X + x;
                points[3].Y = pos.Y + y;
                points[7].X = pos.X - x;
                points[7].Y = pos.Y + y;
                rad        += radStep;
                y           = innerCircle * Math.Sin(rad);
                points[5].X = pos.X;
                points[5].Y = pos.Y + y;
                gp.AddLines(points);
            }
            break;
            }

            gp.CloseFigure();
            if (rendererInfo.MarkerStyle != MarkerStyle.Dot)
            {
                graphics.DrawPath(background, gp);
                graphics.DrawPath(foreground, gp);
            }
        }
Example #31
0
        internal static void SvgPath2Pdf(SvgPath element, XGraphics graphics)
        {
            if (element.Display == "none")
            {
                return;
            }
            var path = new XGraphicsPath {
                FillMode = XFillMode.Winding
            };

            foreach (var segment in element.PathData)
            {
                switch (segment)
                {
                case SvgMoveToSegment svgMoveToSegment:
                    Console.WriteLine($"{segment.GetType()}, start: {svgMoveToSegment.Start}, end: {svgMoveToSegment.End}");
                    path.StartFigure();
                    break;

                case SvgCubicCurveSegment svgCubicCurveSegment:
                    Console.WriteLine(
                        $"{segment.GetType()}, start: {svgCubicCurveSegment.Start}, first control: {svgCubicCurveSegment.FirstControlPoint}, second control: {svgCubicCurveSegment.SecondControlPoint}, end: {svgCubicCurveSegment.End}");
                    path.AddBezier(ConvertHelper.Point2XPoint(svgCubicCurveSegment.Start),
                                   ConvertHelper.Point2XPoint(svgCubicCurveSegment.FirstControlPoint),
                                   ConvertHelper.Point2XPoint(svgCubicCurveSegment.SecondControlPoint),
                                   ConvertHelper.Point2XPoint(svgCubicCurveSegment.End));
                    break;

                case SvgQuadraticCurveSegment svgQuadraticCurveSegment:
                    Console.WriteLine($"{segment.GetType()}, start: {svgQuadraticCurveSegment.Start}, control: {svgQuadraticCurveSegment.ControlPoint}, end: {svgQuadraticCurveSegment.End}");
                    var(start, control1, control2, end) = MathHelper.Quadratic2Cubic(svgQuadraticCurveSegment.Start, svgQuadraticCurveSegment.ControlPoint, svgQuadraticCurveSegment.End);
                    path.AddBezier(ConvertHelper.Point2XPoint(start),
                                   ConvertHelper.Point2XPoint(control1),
                                   ConvertHelper.Point2XPoint(control2),
                                   ConvertHelper.Point2XPoint(end));
                    break;

                case SvgLineSegment svgLineSegment:
                    Console.WriteLine($"{segment.GetType()}, start: {svgLineSegment.Start}, end: {svgLineSegment.End}");
                    path.AddLine(ConvertHelper.Point2XPoint(svgLineSegment.Start), ConvertHelper.Point2XPoint(svgLineSegment.End));
                    break;

                case SvgClosePathSegment svgClosePathSegment:
                    Console.WriteLine($"{segment.GetType()}, start: {svgClosePathSegment.Start}, end: {svgClosePathSegment.End}");
                    path.CloseFigure();
                    break;

                default:
                    Console.WriteLine(segment.GetType());
                    break;
                }
            }

            if (element.Fill != SvgPaintServer.None)
            {
                var brush = ConvertHelper.Fill2XBrush(element.Fill);
                graphics.DrawPath(brush, path);
            }

            if (element.Stroke != null)
            {
                var pen = ConvertHelper.Stroke2XPen(element.Stroke, element.StrokeWidth, element.StrokeLineCap, element.StrokeLineJoin);
                graphics.DrawPath(pen, path);
            }
        }
Example #32
0
        public Stream GeneratePdfLabels(List <string> Addresses, LabelFormat lf, int QtyEachLabel)
        {
            Stream generatePdfLabels = new MemoryStream();
            // The label sheet is basically a table and each cell is a single label
            // Format related
            int CellsPerPage  = lf.RowCount * lf.ColumnCount;
            int CellsThisPage = 0;
            //XRect ContentRectangle = new XRect();       // A single cell content rectangle. This is the rectangle that can be used for contents and accounts for margins and padding.
            XSize  ContentSize = new XSize(); // Size of content area inside a cell.
            double ContentLeftPos;            // left edge of current content area.
            double ContentTopPos;             // Top edge of current content area

            // Layout related
            XColor        StrokeColor = XColors.DarkBlue;
            XColor        FillColor   = XColors.DarkBlue;
            XPen          Pen         = new XPen(StrokeColor, 0.1);
            XBrush        Brush       = new XSolidBrush(FillColor);
            XGraphics     Gfx;
            XGraphicsPath Path = new XGraphicsPath();

            //int LoopTemp = 0;         // Counts each itteration. Used with QtyEachLabel
            int         CurrentColumn = 1;
            int         CurrentRow    = 1;
            PdfDocument Doc           = new PdfDocument();
            PdfPage     page          = new PdfPage();

            //AddPage(Doc, page, lf);
            Doc.AddPage(page);
            Gfx = XGraphics.FromPdfPage(page);

            // Ensure that at least 1 of each label is printed.
            if (QtyEachLabel < 1)
            {
                QtyEachLabel = 1;
            }

            // Define the content area size
            ContentSize = new XSize(XUnit.FromMillimeter(lf.LabelWidth - lf.LabelPaddingLeft - lf.LabelPaddingRight).Point,
                                    XUnit.FromMillimeter(lf.LabelHeight - lf.LabelPaddingTop - lf.LabelPaddingBottom).Point);

            if (Addresses != null)
            {
                if (Addresses.Count > 0)
                // We actually have addresses to output.
                {
                    WriteLine();
                    foreach (string Address in Addresses)
                    {
                        // Once for each address
                        for (int LoopTemp = 0; LoopTemp < QtyEachLabel; LoopTemp++)
                        // Once for each copy of this address.
                        {
                            WriteLine($"Obradjuje se : {Address} ");
                            //WriteLine($"Nalepnica: {CellsThisPage} od :{CellsPerPage} ");
                            if (CellsThisPage == CellsPerPage)
                            {
                                //AddPage(Doc, page, lf);
                                //Gfx = XGraphics.FromPdfPage(page);
                                page          = Doc.AddPage();
                                Gfx           = XGraphics.FromPdfPage(page);
                                CellsThisPage = 0;
                            }
                            // This pages worth of cells are filled up. Create a new page


                            // Calculate which row and column we are working on.
                            CurrentColumn = (CellsThisPage + 1) % lf.ColumnCount;
                            double a = (CellsThisPage + 1) / lf.ColumnCount;
                            CurrentRow = (int)Math.Truncate(a);
                            //WriteLine($"Tekuci red: {CurrentRow} tekuca kolona :{CurrentColumn} ");


                            if (CurrentColumn == 0)
                            {
                                // This occurs when you are working on the last column of the row.
                                // This affects the count for column and row
                                CurrentColumn = lf.ColumnCount;
                            }
                            else
                            {
                                // We are not viewing the last column so this number will be decremented by one.
                                CurrentRow = CurrentRow + 1;
                            }



                            // Calculate the left position of the current cell.
                            ContentLeftPos = ((CurrentColumn - 1) * lf.HorizontalPitch) + lf.LeftMargin + lf.LabelPaddingLeft;


                            // Calculate the top position of the current cell.
                            ContentTopPos = ((CurrentRow - 1) * lf.VerticalPitch) + lf.TopMargin + lf.LabelPaddingTop;
                            //WriteLine($"Leva pozicija: {ContentLeftPos} Pozicija od vrha :{ContentTopPos} Velicina sadrzaja : {ContentSize} ");


                            // Define the content rectangle.
                            XPoint xpoint1 = new XPoint(XUnit.FromMillimeter(ContentLeftPos).Point, XUnit.FromMillimeter(ContentTopPos).Point);

                            XRect ContentRectangle = new XRect(xpoint1, ContentSize);


                            Path = new XGraphicsPath();


                            // Add the address string to the page.
                            Path.AddString(Address, new XFontFamily("Arial"),
                                           XFontStyle.Regular,
                                           9,
                                           ContentRectangle,
                                           XStringFormats.TopLeft);


                            Gfx.DrawPath(Pen, Brush, Path);


                            // Increment the cell count
                            CellsThisPage = CellsThisPage + 1;
                        }
                    }
                    Doc.Save(generatePdfLabels, false);
                }
            }
            return(generatePdfLabels);
        }
 public void SetAndRealizeClipPath(XGraphicsPath clipPath)
 {
   RealizeClipPath(clipPath);
 }
Example #34
0
 public override XPoint AddToPath(XGraphicsPath path, XPoint cursor, Base lastPathCommand = null)
 {
     path.AddBezier(cursor, Control1.ToXPoint(), Control2.ToXPoint(), End.ToXPoint());
     return(End.ToXPoint());
 }
Example #35
0
 public void SetAndRealizeClipPath(XGraphicsPath clipPath)
 {
     RealizeClipPath(clipPath);
 }
    // ----- DrawEllipse --------------------------------------------------------------------------

    //public void DrawEllipse(XPen pen, Rectangle rect);
    //public void DrawEllipse(XPen pen, RectangleF rect);
    //public void DrawEllipse(XPen pen, int x, int y, int width, int height);
    //public void DrawEllipse(XPen pen, float x, float y, float width, float height);

    // ----- DrawPolygon --------------------------------------------------------------------------

    //public void DrawPolygon(XPen pen, Point[] points);
    //public void DrawPolygon(XPen pen, PointF[] points);

    // ----- DrawPath -----------------------------------------------------------------------------

    public void DrawPath(XPen pen, XSolidBrush brush, XGraphicsPath path)
    {
      if (this.gfx != null)
      {
        this.gfx.FillPath(brush.RealizeGdiBrush(), path.RealizeGdiPath());
        this.gfx.DrawPath(pen.RealizeGdiPen(), path.RealizeGdiPath());
      }
      if (this.pdfPage != null)
        this.pdfPage.PageContent.DrawPath(pen, brush, path);
    }
Example #37
0
 public override XPoint AddToPath(XGraphicsPath path, XPoint cursor, Base lastPathCommand = null)
 {
     path.CloseFigure();
     return(cursor);
 }
Example #38
0
 public override XPoint AddToPath(XGraphicsPath path, XPoint cursor, Base lastPathCommand = null)
 {
     path.AddLine(cursor, End.ToXPoint());
     return(End.ToXPoint());
 }
Example #39
0
    // ----- DrawRoundedRectangle -----------------------------------------------------------------

    public void DrawRoundedRectangle(XPen pen, XBrush brush, double x, double y, double width, double height, double ellipseWidth, double ellipseHeight)
    {
      XGraphicsPath path = new XGraphicsPath();
      path.AddRoundedRectangle(x, y, width, height, ellipseWidth, ellipseHeight);
      DrawPath(pen, brush, path);
    }
 void RealizeClipPath(XGraphicsPath clipPath)
 {
   this.renderer.BeginGraphic();
   RealizeCtm();
   this.renderer.AppendPath(clipPath.gdipPath);
   if (clipPath.FillMode == XFillMode.Winding)
     this.renderer.Append("W n\n");
   else
     this.renderer.Append("W* n\n");
 }