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; }
//////////////////////////////////////////////////////////////////// // Draw example of order form //////////////////////////////////////////////////////////////////// private void DrawBookOrderForm() { // Define constants to make the code readable const Double Left = 4.35; const Double Top = 4.65; const Double Bottom = 1.1; const Double Right = 7.4; const Double FontSize = 9.0; const Double MarginHor = 0.04; const Double MarginVer = 0.04; const Double FrameWidth = 0.015; const Double GridWidth = 0.01; // column widths Double ColWidthPrice = ArialNormal.TextWidth(FontSize, "9999.99") + 2.0 * MarginHor; Double ColWidthQty = ArialNormal.TextWidth(FontSize, "Qty") + 2.0 * MarginHor; Double ColWidthDesc = Right - Left - FrameWidth - 3 * GridWidth - 2 * ColWidthPrice - ColWidthQty; // define table PdfTable Table = new PdfTable(Page, Contents, ArialNormal, FontSize); Table.TableArea = new PdfRectangle(Left, Bottom, Right, Top); Table.SetColumnWidth(new Double[] { ColWidthDesc, ColWidthPrice, ColWidthQty, ColWidthPrice }); // define borders Table.Borders.SetAllBorders(FrameWidth, GridWidth); // margin PdfRectangle Margin = new PdfRectangle(MarginHor, MarginVer); // default header style Table.DefaultHeaderStyle.Margin = Margin; Table.DefaultHeaderStyle.BackgroundColor = Color.FromArgb(255, 196, 255); Table.DefaultHeaderStyle.Alignment = ContentAlignment.MiddleRight; // private header style for description Table.Header[0].Style = Table.HeaderStyle; Table.Header[0].Style.Alignment = ContentAlignment.MiddleLeft; // table heading Table.Header[0].Value = "Description"; Table.Header[1].Value = "Price"; Table.Header[2].Value = "Qty"; Table.Header[3].Value = "Total"; // default style Table.DefaultCellStyle.Margin = Margin; // description column style Table.Cell[0].Style = Table.CellStyle; Table.Cell[0].Style.MultiLineText = true; // qty column style Table.Cell[2].Style = Table.CellStyle; Table.Cell[2].Style.Alignment = ContentAlignment.BottomRight; Table.DefaultCellStyle.Format = "#,##0.00"; Table.DefaultCellStyle.Alignment = ContentAlignment.BottomRight; Contents.DrawText(ArialBold, FontSize, 0.5 * (Left + Right), Top + MarginVer + Table.DefaultCellStyle.FontDescent, TextJustify.Center, DrawStyle.Normal, Color.Purple, "Example of PdfTable support"); // reset order total Double Total = 0; // loop for all items in the order // Order class is a atabase simulation for this example foreach (Order Book in Order.OrderList) { Table.Cell[0].Value = Book.Title + ". By: " + Book.Authors; Table.Cell[1].Value = Book.Price; Table.Cell[2].Value = Book.Qty; Table.Cell[3].Value = Book.Total; Table.DrawRow(); // accumulate total Total += Book.Total; } Table.Close(); // save graphics state Contents.SaveGraphicsState(); // form line width 0.01" Contents.SetLineWidth(FrameWidth); Contents.SetLineCap(PdfLineCap.Square); // draw total before tax Double[] ColumnPosition = Table.ColumnPosition; Double TotalDesc = ColumnPosition[3] - MarginHor; Double TotalValue = ColumnPosition[4] - MarginHor; Double PosY = Table.RowTopPosition - 2.0 * MarginVer - Table.DefaultCellStyle.FontAscent; Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Total before tax"); Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Total.ToString("#.00")); // draw tax (Ontario Canada HST) PosY -= Table.DefaultCellStyle.FontLineSpacing; Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Tax (13%)"); Double Tax = Math.Round(0.13 * Total, 2, MidpointRounding.AwayFromZero); Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Tax.ToString("#.00")); // draw total line PosY -= Table.DefaultCellStyle.FontDescent + 0.5 * MarginVer; Contents.DrawLine(ColumnPosition[3], PosY, ColumnPosition[4], PosY); // draw final total PosY -= Table.DefaultCellStyle.FontAscent + 0.5 * MarginVer; Contents.DrawText(ArialNormal, FontSize, TotalDesc, PosY, TextJustify.Right, "Total payable"); Total += Tax; Contents.DrawText(ArialNormal, FontSize, TotalValue, PosY, TextJustify.Right, Total.ToString("#.00")); PosY -= Table.DefaultCellStyle.FontDescent + MarginVer; Contents.DrawLine(ColumnPosition[0], Table.RowTopPosition, ColumnPosition[0], PosY); Contents.DrawLine(ColumnPosition[0], PosY, ColumnPosition[4], PosY); Contents.DrawLine(ColumnPosition[4], Table.RowTopPosition, ColumnPosition[4], PosY); // restore graphics state Contents.RestoreGraphicsState(); return; }