Example #1
0
        ////////////////////////////////////////////////////////////////////
        // Draw frame around example area
        ////////////////////////////////////////////////////////////////////

        private void DrawFrameAndBackgroundWaterMark()
        {
            // save graphics state
            Contents.SaveGraphicsState();

            // Draw frame around the page
            // Set line width to 0.02"
            Contents.SetLineWidth(0.02);

            // set frame color dark blue
            Contents.SetColorStroking(Color.DarkBlue);

            // use water mark tiling pattern to fill the frame
            Contents.SetPatternNonStroking(WaterMark);

            // rectangle position: x=1.0", y=1.0", width=6.5", height=9.0"
            Contents.DrawRectangle(1.0, 1.0, 6.5, 9.0, PaintOp.CloseFillStroke);

            // restore graphics sate
            Contents.RestoreGraphicsState();

            // draw article name under the frame
            // Note: the \u00a4 is character 164 that was substituted during Font resource definition
            // this character is a solid circle it is normally Unicode 9679 or \u25cf in the Arial family
            Contents.DrawText(ArialNormal, 9.0, 1.1, 0.85, "PdfFileWriter \u25cf PDF File Writer C# Class Library \u25cf Author: Uzi Granot");

            // draw web link to the article
            Contents.DrawWebLink(Page, ArialNormal, 9.0, 7.4, 0.85, TextJustify.Right, DrawStyle.Underline, Color.Blue, "Click to view article",
                                 "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version");

            return;
        }
        ////////////////////////////////////////////////////////////////////
        // Draw rectangle with rounded corners and filled with brick pattern
        ////////////////////////////////////////////////////////////////////

        private void DrawBrickPattern
        (
            PdfDocument Document,
            PdfContents Contents
        )
        {
            // Define brick tilling pattern resource
            // Arguments: PdfDocument class, Scale factor (0.25), Stroking color (lines between bricks), Nonstroking color (brick)
            // Return value: tilling pattern resource
            PdfTilingPattern BrickPattern = PdfTilingPattern.SetBrickPattern(Document, 0.25, Color.LightYellow, Color.SandyBrown);

            // save graphics state
            Contents.SaveGraphicsState();

            // set outline width 0.04"
            Contents.SetLineWidth(0.04);

            // set outline color to purple
            Contents.SetColorStroking(Color.Purple);

            // set fill pattern to brick
            Contents.SetPatternNonStroking(BrickPattern);

            // draw rounded rectangle filled with brick pattern
            Contents.DrawRoundedRectangle(1.13, 5.0, 1.4, 1.4, 0.2, PaintOp.CloseFillStroke);

            // restore graphics sate
            Contents.RestoreGraphicsState();
            return;
        }
        ////////////////////////////////////////////////////////////////////
        // Draw frame around example area
        ////////////////////////////////////////////////////////////////////

        private void DrawFrameAndBackgroundWaterMark
        (
            PdfContents Contents
        )
        {
            // save graphics state
            Contents.SaveGraphicsState();

            // Draw frame around the page
            // Set line width to 0.02"
            Contents.SetLineWidth(0.02);

            // set frame color dark blue
            Contents.SetColorStroking(Color.DarkBlue);

            // use water mark tiling pattern to fill the frame
            Contents.SetPatternNonStroking(WaterMark);

            // rectangle position: x=1.0", y=1.0", width=6.5", height=9.0"
            Contents.DrawRectangle(1.0, 1.0, 6.5, 9.0, PaintOp.CloseFillStroke);

            // restore graphics sate
            Contents.RestoreGraphicsState();

            // draw article name under the frame
            // Note: the \u00a4 is character 164 that was substituted during Font resource definition
            // this character is a solid circle it is normally unicode 9679 or \u25cf in the Arial family
            Contents.DrawText(ArialNormal, 9.0, 1.1, 0.85, "PdfFileWriter \u00a4 PDF File Writer C# Class Library \u00a4 Author: Uzi Granot");
            return;
        }
        internal void Draw
        (
            PdfContents Contents,
            double DrawRectX,
            double DrawRectY,
            double DrawRectWidth,
            double DrawRectHeight,
            ContentAlignment Alignment = (ContentAlignment)0
        )
        {
            // save drawing rectangle in user coordinates
            this.DrawRectX      = DrawRectX;
            this.DrawRectY      = DrawRectY;
            this.DrawRectWidth  = DrawRectWidth;
            this.DrawRectHeight = DrawRectHeight;

            // test arguments
            if (DrawRectWidth == 0 && DrawRectHeight == 0 || DrawRectWidth == 0 && PathBBoxWidth != 0 || DrawRectHeight == 0 && PathBBoxHeight != 0)
            {
                throw new ApplicationException("DrawWPFPath: Drawing rectangle is empty");
            }

            // set transformation matrix
            SetTransformation(Alignment);

            // clip
            if (Stroking == null && NonStroking == null)
            {
                // build clipping path
                BuildPath(Contents, FillRule == FillRule.EvenOdd ? PaintOp.ClipPathEor : PaintOp.ClipPathWnr);
                return;
            }

            // paint operator
            PaintOp PaintOperator;

            // brush is defined as shading
            if (NonStroking != null && (NonStroking.GetType() == typeof(SysMedia.LinearGradientBrush) || NonStroking.GetType() == typeof(SysMedia.RadialGradientBrush) ||
                                        NonStroking.GetType() == typeof(PdfAxialShading) || NonStroking.GetType() == typeof(PdfRadialShading)))
            {
                // save graphics state
                Contents.SaveGraphicsState();

                // build clipping path
                BuildPath(Contents, FillRule == FillRule.EvenOdd ? PaintOp.ClipPathEor : PaintOp.ClipPathWnr);

                // set bland mode
                if (BlendMode != BlendMode.Normal)
                {
                    Contents.SetBlendMode(BlendMode);
                }

                // set opacity
                Contents.SetAlphaNonStroking(BrushOpacity);

                // draw linera gradient brush shading bounded by clip path
                if (NonStroking.GetType() == typeof(SysMedia.LinearGradientBrush))
                {
                    PdfAxialShading AxialShading = new PdfAxialShading(Contents.Document, (SysMedia.LinearGradientBrush)NonStroking);
                    AxialShading.SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading(AxialShading);
                }

                // draw axial shading bounded by clip path
                else if (NonStroking.GetType() == typeof(PdfAxialShading))
                {
                    ((PdfAxialShading)NonStroking).SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading((PdfAxialShading)NonStroking);
                }

                // draw radial gradient brush shading bounded by clip path
                else if (NonStroking.GetType() == typeof(SysMedia.RadialGradientBrush))
                {
                    PdfRadialShading RadialShading = new PdfRadialShading(Contents.Document, (SysMedia.RadialGradientBrush)NonStroking);
                    RadialShading.SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading(RadialShading);
                }

                // draw radial shading bounded by clip path
                else
                {
                    ((PdfRadialShading)NonStroking).SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading((PdfRadialShading)NonStroking);
                }

                // remove clipping path
                Contents.RestoreGraphicsState();

                // no pen defined
                if (Stroking == null)
                {
                    return;
                }

                // pen is defined
                PaintOperator = PaintOp.Stroke;
            }

            // set paint operator for all other cases (no shading)
            else
            {
                // we have pen and no brush
                if (NonStroking == null)
                {
                    PaintOperator = PaintOp.Stroke;
                }
                // we have brush but no pen
                else if (Stroking == null)
                {
                    PaintOperator = FillRule == FillRule.EvenOdd ? PaintOp.FillEor : PaintOp.Fill;
                }
                // we have brush and pen
                else
                {
                    PaintOperator = FillRule == FillRule.EvenOdd ? PaintOp.CloseFillStrokeEor: PaintOp.CloseFillStroke;
                }
            }

            // save graphics state
            Contents.SaveGraphicsState();

            // set bland mode
            if (BlendMode != BlendMode.Normal)
            {
                Contents.SetBlendMode(BlendMode);
            }

            // stroking (pen) is defined
            if (Stroking != null)
            {
                if (Stroking.GetType() == typeof(Color))
                {
                    // pen color
                    Contents.SetColorStroking((Color)Stroking);

                    // set opacity
                    if (PenOpacity != 1.0)
                    {
                        Contents.SetAlphaStroking(PenOpacity);
                    }

                    // pen width
                    if (PenWidth >= 0)
                    {
                        Contents.SetLineWidth(PenWidth);
                    }

                    // line cap
                    if (LineCap != (PdfLineCap)(-1))
                    {
                        Contents.SetLineCap(LineCap);
                    }

                    // line join
                    if (LineJoin != (PdfLineJoin)(-1))
                    {
                        Contents.SetLineJoin(LineJoin);
                    }

                    // Miter
                    if (MiterLimit != -1)
                    {
                        Contents.SetMiterLimit(MiterLimit);
                    }

                    // line is made of dashes
                    if (DashArray != null)
                    {
                        Contents.SetDashLine(DashArray, DashPhase);
                    }
                }

                else if (Stroking.GetType() == typeof(SysMedia.Pen))
                {
                    // media pen short cut
                    SysMedia.Pen Pen = (SysMedia.Pen)Stroking;

                    // media brush shortcut
                    SysMedia.SolidColorBrush Brush = (SysMedia.SolidColorBrush)Pen.Brush;

                    // media pen color short cut
                    SysMedia.Color PenColor = Brush.Color;

                    // pen color
                    Contents.SetColorStroking(Color.FromArgb(PenColor.R, PenColor.G, PenColor.B));

                    // pen opacity
                    if (PenColor.A != 255 || Brush.Opacity != 1.0)
                    {
                        Contents.SetAlphaStroking((PenColor.A / 255.0) * Brush.Opacity);
                    }

                    // pen thickness converted to user units
                    double Thickness = Pen.Thickness * Math.Max(Math.Abs(ScaleX), Math.Abs(ScaleY));
                    Contents.SetLineWidth(Thickness);

                    // line cap
                    // Note: PDF line cap is the same for start and end. We will ignore EndLineCap
                    // Triangle line cap will be round
                    switch (Pen.StartLineCap)
                    {
                    case SysMedia.PenLineCap.Flat: Contents.SetLineCap(PdfLineCap.Butt); break;

                    case SysMedia.PenLineCap.Square: Contents.SetLineCap(PdfLineCap.Square); break;

                    default: Contents.SetLineCap(PdfLineCap.Round); break;
                    }

                    // line join
                    switch (Pen.LineJoin)
                    {
                    case SysMedia.PenLineJoin.Bevel: Contents.SetLineJoin(PdfLineJoin.Bevel); break;

                    case SysMedia.PenLineJoin.Miter: Contents.SetLineJoin(PdfLineJoin.Miter); break;

                    default: Contents.SetLineJoin(PdfLineJoin.Round); break;
                    }

                    // Miter
                    Contents.SetMiterLimit(Pen.MiterLimit);

                    // dash pattern
                    if (Pen.DashStyle.Dashes.Count > 0)
                    {
                        int      End          = Pen.DashStyle.Dashes.Count;
                        double[] PenDashArray = new double[End];
                        for (int Index = 0; Index < End; Index++)
                        {
                            PenDashArray[Index] = Thickness * Pen.DashStyle.Dashes[Index];
                        }
                        Contents.SetDashLine(PenDashArray, Thickness * Pen.DashStyle.Offset);
                    }
                }
            }

            // non-stroking (brush) is defined
            // note shading brush was handled above
            if (NonStroking != null)
            {
                // set opacity
                if (BrushOpacity != 1.0)
                {
                    Contents.SetAlphaNonStroking(BrushOpacity);
                }

                // brush color
                if (NonStroking.GetType() == typeof(Color))
                {
                    Contents.SetColorNonStroking((Color)NonStroking);
                }

                else if (NonStroking.GetType() == typeof(PdfTilingPattern))
                {
                    Contents.SetPatternNonStroking((PdfTilingPattern)NonStroking);
                }
            }

            // build path
            BuildPath(Contents, PaintOperator);

            // restore graphics state
            Contents.RestoreGraphicsState();
            return;
        }