Beispiel #1
0
        ////////////////////////////////////////////////////////////////////
        // Draw heading
        ////////////////////////////////////////////////////////////////////

        private void DrawTwoLinesOfHeading()
        {
            // page heading
            // Arguments: Font: ArialBold, size: 36 points, Position: X = 4.25", Y = 9.5"
            // Text Justify: Center (text center will be at X position)
            // Stoking color: R=128, G=0, B=255 (text outline)
            // Nonstroking color: R=255, G=0, B=128 (text body)

            double y = (11.69 - 2.0) * 72;
            //double x = 1.5;
            double x = 36;

            //Contents.DrawText(TimesNormal, 9.0, x, y, TextJustify.Center, 0.02, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128), "PDF FILE WRITER");
            Contents.DrawText(TimesNormal, 9.0, x, y, TextJustify.Left, 0.02, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128), ".");
            //Contents.DrawText(TimesNormal, 9.0, x, y, TextJustify.Center, DrawStyle.Normal, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128), "PDF FILE WRITER");

            // save graphics state
            Contents.SaveGraphicsState();

            // change nonstroking (fill) color to purple
            Contents.SetColorNonStroking(Color.Purple);
            PdfFont pdf = PdfFont.CreatePdfFont(Document, "Times New Roman", FontStyle.Regular, true);
            PaintOp p   = PaintOp.CloseStroke;

            // Draw second line of heading text
            // arguments: Handwriting font, Font size 30 point, Position X=4.25", Y=9.0"
            // Text Justify: Center (text center will be at X position)
            //PdfRectangle rect = pdf.TextBoundingBox(30.0, "\n");
            PdfRectangle rect1 = pdf.TextBoundingBox(30.0, "ExampleCenter");
            PdfRectangle rect2 = pdf.TextBoundingBox(30.0, "ExampleLeft");
            TextBox      box   = new TextBox(10.0 * 72, 0.25);

            box.AddText(TimesNormal, 30, ArticleString);
            double y1 = 8.75 * 72;
            // double value = Contents.DrawText(TimesNormal, 30.0, (4.25 * 72)+rect.Width/2  , 8.75 * 72, TextJustify.Right, "ExampleRight \n");
            double value  = Contents.DrawText((4.25 * 72), ref y1, 0, 0, box);
            double value1 = Contents.DrawText(TimesNormal, 30.0, 4.25 * 72, 6.75 * 72, TextJustify.Left, "ExampleLeft");
            double value2 = Contents.DrawText(TimesNormal, 30.0, 4.25 * 72 + (rect1.Width), 4.75 * 72, TextJustify.Center, "ExampleCenter");

            Contents.DrawRectangle(4.25 * 72, 6.75 * 72, value1, rect1.Height, p);
            Contents.DrawRectangle((4.25 * 72), y1, box.BoxWidth, box.BoxHeight, p);
            Contents.DrawRectangle((4.25 * 72), 4.75 * 72, value2, rect2.Height, p);
            // restore graphics sate (non stroking color will be restored to default)
            Contents.RestoreGraphicsState();
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw star
        /// </summary>
        /// <param name="Center">Center point</param>
        /// <param name="Radius1">Radius 1</param>
        /// <param name="Radius2">Radius 2</param>
        /// <param name="Alpha">Initial angle</param>
        /// <param name="Sides">Number of sides</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawStar(
			PointD		Center,
			Double		Radius1,
			Double		Radius2,
			Double		Alpha,
			Int32		Sides,
			PaintOp		PP
			)
        {
            // validate sides
            if(Sides < 3) throw new ApplicationException("Draw star. Number of sides must be 3 or more");

            // move to first point
            MoveTo(new PointD(Center, Radius1, Alpha));

            // increment angle
            Double DeltaAlpha = Math.PI / Sides;

            // double number of sides
            Sides *= 2;

            // line to the rest of the points
            for(Int32 Side = 1; Side < Sides; Side++)
            {
            Alpha += DeltaAlpha;
            LineTo(new PointD(Center, (Side & 1) != 0 ? Radius2 : Radius1, Alpha));
            }

            // set paint operator
            SetPaintOp(PP);
             		return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw star
        /// </summary>
        /// <param name="CenterX">Center X</param>
        /// <param name="CenterY">Center Y</param>
        /// <param name="Radius1">Radius 1</param>
        /// <param name="Radius2">Radius 2</param>
        /// <param name="Alpha">Initial angle</param>
        /// <param name="Sides">Number of sides</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawStar(
			Double		CenterX,
			Double		CenterY,
			Double		Radius1,
			Double		Radius2,
			Double		Alpha,
			Int32		Sides,
			PaintOp		PP
			)
        {
            DrawStar(new PointD(CenterX, CenterY), Radius1, Radius2, Alpha, Sides, PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw star
        /// </summary>
        /// <param name="Center">Center position</param>
        /// <param name="Radius">Radius</param>
        /// <param name="Alpha">Initial angle</param>
        /// <param name="Sides">Number of sides</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawStar(
			PointD		Center,
			Double		Radius,
			Double		Alpha,
			Int32		Sides,
			PaintOp		PP
			)
        {
            // inner radius
            Double Radius1 = 0;

            // for polygon with less than 5, set inner radius to half the main radius
            if(Sides < 5)
            {
            Radius1 = 0.5 * Radius;
            }

            // for polygons with 5 sides, calculate inner radius
            else
            {
            // polygon angle
            Double DeltaAlpha = 2.0 * Math.PI / Sides;

            // first line
            LineD L1 = new LineD(new PointD(Center, Radius, Alpha), new PointD(Center, Radius, Alpha + 2.0 * DeltaAlpha));

            // second line
            LineD L2 = new LineD(new PointD(Center, Radius, Alpha - DeltaAlpha), new PointD(Center, Radius, Alpha + DeltaAlpha));

            // inner radius
            Radius1 = (new PointD(L1, L2)).Distance(Center);
            }

            // draw star
            DrawStar(Center, Radius, Radius1, Alpha, Sides, PP);
             		return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw Rounded Rectangle
        /// </summary>
        /// <param name="OriginX">Origin X (left)</param>
        /// <param name="OriginY">Origin Y (right)</param>
        /// <param name="Width">Width</param>
        /// <param name="Height">Height</param>
        /// <param name="Radius">Radius</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRoundedRectangle(
			Double		OriginX,
			Double		OriginY,
			Double		Width,
			Double		Height,
			Double		Radius,
			PaintOp		PP
			)
        {
            // make sure radius is not too big
            if(Radius > 0.5 * Width) Radius = 0.5 * Width;
            if(Radius > 0.5 * Height) Radius = 0.5 * Height;

            // draw path
            MoveTo(OriginX + Radius, OriginY);
            DrawBezier(BezierD.CircleFourthQuarter(OriginX + Width - Radius, OriginY + Radius, Radius), BezierPointOne.LineTo);
            DrawBezier(BezierD.CircleFirstQuarter(OriginX + Width - Radius, OriginY + Height - Radius, Radius), BezierPointOne.LineTo);
            DrawBezier(BezierD.CircleSecondQuarter(OriginX + Radius, OriginY + Height - Radius, Radius), BezierPointOne.LineTo);
            DrawBezier(BezierD.CircleThirdQuarter(OriginX + Radius, OriginY + Radius, Radius), BezierPointOne.LineTo);

            // set paint operator
            SetPaintOp(PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw Rounded Rectangle
        /// </summary>
        /// <param name="Origin">Origin (left-bottom)</param>
        /// <param name="Size">Size</param>
        /// <param name="Radius">Radius</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRoundedRectangle(
			PointD		Origin,
			SizeD		Size,
			Double		Radius,
			PaintOp		PP
			)
        {
            DrawRoundedRectangle(Origin.X, Origin.Y, Size.Width, Size.Height, Radius, PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw heart
        /// </summary>
        /// <param name="CenterLine">Center line</param>
        /// <param name="PP">Paint operator</param>
        /// <remarks>
        /// <para>
        /// <a href="http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version#DrawHeart">For example of drawing heart see 3.10. Draw Heart</a>
        /// </para>
        /// </remarks>
        ////////////////////////////////////////////////////////////////////
        public void DrawHeart(
			LineD		CenterLine,
			PaintOp		PP
			)
        {
            // PI / 1.5 = 120 deg and PI / 2 = 90 deg
            DrawDoubleBezierPath(CenterLine, 1.0, Math.PI / 1.5, 1.0, 0.5 * Math.PI, PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw double Bezier path
        /// </summary>
        /// <param name="CenterLine">Center line</param>
        /// <param name="Factor1">Factor 1</param>
        /// <param name="Alpha1">Alpha 1</param>
        /// <param name="Factor2">Factor 2</param>
        /// <param name="Alpha2">Alpha 2</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawDoubleBezierPath(
			LineD		CenterLine,
			Double		Factor1,
			Double		Alpha1,
			Double		Factor2,
			Double		Alpha2,
			PaintOp		PP
			)
        {
            // two symmetric Bezier curves
            DrawBezier(new BezierD(CenterLine.P1, Factor1, -0.5 * Alpha1, Factor2, -0.5 * Alpha2, CenterLine.P2), BezierPointOne.MoveTo);
            DrawBezier(new BezierD(CenterLine.P2, Factor2, Math.PI + 0.5 * Alpha2, Factor1, Math.PI + 0.5 * Alpha1, CenterLine.P1), BezierPointOne.Ignore);

            // set paint operator
            SetPaintOp(PP);
             		return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw Rectangle
        /// </summary>
        /// <param name="OriginX">Origin X (left)</param>
        /// <param name="OriginY">Origin Y (bottom)</param>
        /// <param name="Width">Width</param>
        /// <param name="Height">Height</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRectangle(
			Double		OriginX,
			Double		OriginY,
			Double		Width,
			Double		Height,
			PaintOp		PP
			)
        {
            // draw rectangle
            ContentsString.AppendFormat(NFI.PeriodDecSep, "{0} {1} {2} {3} re {4}\n", ToPt(OriginX), ToPt(OriginY), ToPt(Width), ToPt(Height), PaintOpStr(PP));
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw rectangle
        /// </summary>
        /// <param name="Origin">Origin (left-bottom)</param>
        /// <param name="Size">Size</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRectangle(
			PointD		Origin,
			SizeD		Size,
			PaintOp		PP
			)
        {
            DrawRectangle(Origin.X, Origin.Y, Size.Width, Size.Height, PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw polygon
        /// </summary>
        /// <param name="PathArray">Path array of X and Y values (min 4 and even)</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawPolygon(
			Single[]	PathArray,	// pairs of x and y values
			PaintOp		PP
			)
        {
            // program error
            if(PathArray.Length < 4) throw new ApplicationException("Draw polygon error: path array must have at least 4 items");

            // program error
            if((PathArray.Length & 1) != 0) throw new ApplicationException("Draw polygon error: path array must have even number of items");

            // move to first point
            ContentsString.AppendFormat(NFI.PeriodDecSep, "{0} {1} m\n", ToPt(PathArray[0]), ToPt(PathArray[1]));

            // draw lines
            for(Int32 Index = 2; Index < PathArray.Length; Index += 2)
            {
            ContentsString.AppendFormat(NFI.PeriodDecSep, "{0} {1} l\n", ToPt(PathArray[Index]), ToPt(PathArray[Index + 1]));
            }

            // set paint operator
            SetPaintOp(PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw polygon
        /// </summary>
        /// <param name="PathArray">Path array (min 2 points)</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawPolygon(
			PointF[]	PathArray,
			PaintOp		PP
			)
        {
            // program error
            if(PathArray.Length < 2) throw new ApplicationException("Draw polygon error: path array must have at least two points");

            // move to first point
            ContentsString.AppendFormat(NFI.PeriodDecSep, "{0} {1} m\n", ToPt(PathArray[0].X), ToPt(PathArray[0].Y));

            // draw lines
            for(Int32 Index = 1; Index < PathArray.Length; Index++)
            {
            ContentsString.AppendFormat(NFI.PeriodDecSep, "{0} {1} l\n", ToPt(PathArray[Index].X), ToPt(PathArray[Index].Y));
            }

            // set paint operator
            SetPaintOp(PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw oval
        /// </summary>
        /// <param name="OriginX">Origin X (left)</param>
        /// <param name="OriginY">Origin Y (bottom)</param>
        /// <param name="Width">Width</param>
        /// <param name="Height">Height</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawOval(
			Double		OriginX,
			Double		OriginY,
			Double		Width,
			Double		Height,
			PaintOp		PP
			)
        {
            Width /= 2;
            Height /= 2;
            OriginX += Width;
            OriginY += Height;
            DrawBezier(BezierD.OvalFirstQuarter(OriginX, OriginY, Width, Height), BezierPointOne.MoveTo);
            DrawBezier(BezierD.OvalSecondQuarter(OriginX, OriginY, Width, Height), BezierPointOne.Ignore);
            DrawBezier(BezierD.OvalThirdQuarter(OriginX, OriginY, Width, Height), BezierPointOne.Ignore);
            DrawBezier(BezierD.OvalFourthQuarter(OriginX, OriginY, Width, Height), BezierPointOne.Ignore);
            SetPaintOp(PP);
             		return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw heart
        /// </summary>
        /// <param name="CenterLineTopX">Center line top X</param>
        /// <param name="CenterLineTopY">Center line top Y</param>
        /// <param name="CenterLineBottomX">Center line bottom X</param>
        /// <param name="CenterLineBottomY">Center line bottom Y</param>
        /// <param name="PP">Paint operator</param>
        /// <remarks>
        /// <para>
        /// <a href="http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version#DrawHeart">For example of drawing heart see 3.10. Draw Heart</a>
        /// </para>
        /// </remarks>
        ////////////////////////////////////////////////////////////////////
        public void DrawHeart(
			Double		CenterLineTopX,
			Double		CenterLineTopY,
			Double		CenterLineBottomX,
			Double		CenterLineBottomY,
			PaintOp		PP
			)
        {
            DrawHeart(new LineD(CenterLineTopX, CenterLineTopY, CenterLineBottomX, CenterLineBottomY), PP);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Convert PaintOp enumeration to String
        /// </summary>
        /// <param name="PP">Paint operator</param>
        /// <returns>Paint operator string</returns>
        ////////////////////////////////////////////////////////////////////
        public String PaintOpStr(
			PaintOp		PP
			)
        {
            // apply paint operator
            return(PaintStr[(Int32) PP]);
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Set paint operator
        /// </summary>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void SetPaintOp(
			PaintOp		PP
			)
        {
            // apply paint operator
            if(PP != PaintOp.NoOperator) ContentsString.AppendFormat("{0}\n", PaintStr[(Int32) PP]);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw regular polygon
        /// </summary>
        /// <param name="CenterX">Center X</param>
        /// <param name="CenterY">Center Y</param>
        /// <param name="Radius">Radius</param>
        /// <param name="Alpha">Initial angle</param>
        /// <param name="Sides">Number of sides</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRegularPolygon(
			Double		CenterX,
			Double		CenterY,
			Double		Radius,
			Double		Alpha,
			Int32		Sides,
			PaintOp		PP
			)
        {
            DrawRegularPolygon(new PointD(CenterX, CenterY), Radius, Alpha, Sides, PP);
            return;
        }
        private void BuildPath
        (
            PdfContents Contents,
            PaintOp PaintOperator
        )
        {
            // every figure is a separated subpath and contains some segments
            foreach (SysMedia.PathFigure SubPath in MediaPath.Figures)
            {
                // get start of sub-path point
                PointD CurPoint   = PathToDrawing(SubPath.StartPoint);
                PointD StartPoint = CurPoint;
                Contents.MoveTo(CurPoint);

                // process all points of one sub-path
                foreach (SysMedia.PathSegment Seg in SubPath.Segments)
                {
                    // line segment
                    if (Seg.GetType() == typeof(SysMedia.LineSegment))
                    {
                        CurPoint = PathToDrawing(((SysMedia.LineSegment)Seg).Point);
                        Contents.LineTo(CurPoint);
                    }

                    // polygon
                    else if (Seg.GetType() == typeof(SysMedia.PolyLineSegment))
                    {
                        SysMedia.PolyLineSegment LineSegArray = (SysMedia.PolyLineSegment)Seg;
                        foreach (SysWin.Point PolyPoint in LineSegArray.Points)
                        {
                            CurPoint = PathToDrawing(PolyPoint);
                            Contents.LineTo(CurPoint);
                        }
                    }

                    // cubic bezier segment
                    else if (Seg.GetType() == typeof(SysMedia.BezierSegment))
                    {
                        SysMedia.BezierSegment BezierSeg = (SysMedia.BezierSegment)Seg;
                        CurPoint = PathToDrawing(BezierSeg.Point3);
                        Contents.DrawBezier(PathToDrawing(BezierSeg.Point1), PathToDrawing(BezierSeg.Point2), CurPoint);
                    }

                    // cubic bezier multi segments
                    else if (Seg.GetType() == typeof(SysMedia.PolyBezierSegment))
                    {
                        SysMedia.PolyBezierSegment BezierSegArray = (SysMedia.PolyBezierSegment)Seg;
                        int Count = BezierSegArray.Points.Count;
                        for (int Index = 0; Index < Count; Index += 3)
                        {
                            CurPoint = PathToDrawing(BezierSegArray.Points[Index + 2]);
                            Contents.DrawBezier(PathToDrawing(BezierSegArray.Points[Index]), PathToDrawing(BezierSegArray.Points[Index + 1]), CurPoint);
                        }
                    }

                    // quadratic bezier segment
                    else if (Seg.GetType() == typeof(SysMedia.QuadraticBezierSegment))
                    {
                        SysMedia.QuadraticBezierSegment BezierSeg = (SysMedia.QuadraticBezierSegment)Seg;
                        PointD NextPoint = PathToDrawing(BezierSeg.Point2);
                        Contents.DrawBezier(new BezierD(CurPoint, PathToDrawing(BezierSeg.Point1), NextPoint), BezierPointOne.Ignore);
                        CurPoint = NextPoint;
                    }

                    // quadratic bezier multi segments
                    else if (Seg.GetType() == typeof(SysMedia.PolyQuadraticBezierSegment))
                    {
                        SysMedia.PolyQuadraticBezierSegment BezierSegArray = (SysMedia.PolyQuadraticBezierSegment)Seg;
                        int Count = BezierSegArray.Points.Count;
                        for (int Index = 0; Index < Count; Index += 2)
                        {
                            PointD NextPoint = PathToDrawing(BezierSegArray.Points[Index + 1]);
                            Contents.DrawBezier(new BezierD(CurPoint, PathToDrawing(BezierSegArray.Points[Index]), NextPoint), BezierPointOne.Ignore);
                            CurPoint = NextPoint;
                        }
                    }

                    // draw arc
                    else if (Seg.GetType() == typeof(SysMedia.ArcSegment))
                    {
                        SysMedia.ArcSegment Arc = (SysMedia.ArcSegment)Seg;
                        PointD  NextPoint       = PathToDrawing(Arc.Point);
                        ArcType ArcType;
                        if (Arc.SweepDirection == (PathYAxis == YAxisDirection.Down ? SysMedia.SweepDirection.Counterclockwise : SysMedia.SweepDirection.Clockwise))
                        {
                            ArcType = Arc.IsLargeArc ? ArcType.LargeCounterClockWise : ArcType.SmallCounterClockWise;
                        }
                        else
                        {
                            ArcType = Arc.IsLargeArc ? ArcType.LargeClockWise : ArcType.SmallClockWise;
                        }
                        Contents.DrawArc(CurPoint, NextPoint, SizeToDrawing(Arc.Size), Arc.RotationAngle, ArcType, BezierPointOne.Ignore);
                        CurPoint = NextPoint;
                    }

                    // should no happen
                    else
                    {
                        throw new ApplicationException("Windows Media path: unknown path segment.");
                    }
                }

                // for stroke set paint operator for each sub-path
                if (SubPath.IsClosed)
                {
                    Contents.SetPaintOp(PaintOp.CloseSubPath);
                }
            }

            // paint operator
            Contents.SetPaintOp(PaintOperator);
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw regular polygon
        /// </summary>
        /// <param name="Center">Center position</param>
        /// <param name="Radius">Radius</param>
        /// <param name="Alpha">Initial angle</param>
        /// <param name="Sides">Number of sides</param>
        /// <param name="PP">Paint operator</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawRegularPolygon(
			PointD		Center,
			Double		Radius,
			Double		Alpha,
			Int32		Sides,
			PaintOp		PP
			)
        {
            // validate sides
            if(Sides < 3) throw new ApplicationException("Draw regular polygon. Number of sides must be 3 or more");

            // polygon angle
            Double DeltaAlpha = 2.0 * Math.PI / Sides;

            // first corner coordinates
            MoveTo(new PointD(Center, Radius, Alpha));

            for(Int32 Side = 1; Side < Sides; Side++)
            {
            Alpha += DeltaAlpha;
            LineTo(new PointD(Center, Radius, Alpha));
            }

            // set paint operator
            SetPaintOp(PP);
             		return;
        }