/// <summary> /// Demonstrates the use of XLinearGradientBrush. /// </summary> public override void RenderPage(XGraphics gfx) { base.RenderPage(gfx); XRect rect; XLinearGradientBrush brush; Graphics grfx = gfx.Internals.Graphics; XLinearGradientBrush brush2 = new XLinearGradientBrush( new XPoint(100, 100), new XPoint(300, 300), XColors.DarkRed, XColors.Yellow); //gfx.FillRectangle(brush, 0, 0, 600, 600); //gfx.TranslateTransform(35, 200); //gfx.RotateTransform(17); rect = new XRect(20, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.DarkRed, XColors.Yellow, XLinearGradientMode.Horizontal); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(140, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.DarkRed, XColors.Yellow, XLinearGradientMode.Vertical); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(260, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.DarkRed, XColors.Yellow, XLinearGradientMode.ForwardDiagonal); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(380, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.DarkRed, XColors.Yellow, XLinearGradientMode.BackwardDiagonal); gfx.DrawRectangle(XPens.Red, brush, rect); gfx.TranslateTransform(80, 250); gfx.ScaleTransform(1.1); gfx.RotateTransform(20); rect = new XRect(20, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.Orange, XColors.DarkBlue, XLinearGradientMode.Horizontal); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(140, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.Orange, XColors.DarkBlue, XLinearGradientMode.Vertical); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(260, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.Orange, XColors.DarkBlue, XLinearGradientMode.ForwardDiagonal); gfx.DrawRectangle(XPens.Red, brush, rect); rect = new XRect(380, 50, 100, 200); brush = new XLinearGradientBrush(rect, XColors.Orange, XColors.DarkBlue, XLinearGradientMode.BackwardDiagonal); gfx.DrawRectangle(XPens.Red, brush, rect); }
/// <summary> /// Setups the shading pattern from the specified brush. /// </summary> public void SetupFromBrush(XLinearGradientBrush brush, XMatrix matrix) { if (brush == null) throw new ArgumentNullException("brush"); PdfShading shading = new PdfShading(this.document); shading.SetupFromBrush(brush); Elements[Keys.Shading] = shading; Elements[Keys.Matrix] = new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]"); }
/// <summary> /// Setups the shading pattern from the specified brush. /// </summary> internal void SetupFromBrush(XLinearGradientBrush brush, XMatrix matrix, XGraphicsPdfRenderer renderer) { if (brush == null) throw new ArgumentNullException("brush"); PdfShading shading = new PdfShading(_document); shading.SetupFromBrush(brush, renderer); Elements[Keys.Shading] = shading; //Elements[Keys.Matrix] = new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]"); Elements.SetMatrix(Keys.Matrix, matrix); }
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); }
public XBrush Gradient(Rect rect, Color color1, Color color2) { var brush = new XLinearGradientBrush(rect.ToRectangleF(), color1, color2, XLinearGradientMode.ForwardDiagonal); //m_items.Add(brush); return brush; }
/// <summary> /// Renders the content of the page. /// </summary> public void Render(XGraphics gfx) { XRect rect; XPen pen; double x = 50, y = 100; XFont fontH1 = new XFont("Times", 18, XFontStyle.Bold); XFont font = new XFont("Times", 12); XFont fontItalic = new XFont("Times", 12, XFontStyle.BoldItalic); double ls = font.GetHeight(gfx); // Draw some text gfx.DrawString("Create PDF on the fly with PDFsharp", fontH1, XBrushes.Black, x, x); gfx.DrawString("With PDFsharp you can use the same code to draw graphic, " + "text and images on different targets.", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("The object used for drawing is the XGraphics object.", font, XBrushes.Black, x, y); y += 2 * ls; // Draw an arc pen = new XPen(XColors.Red, 4); pen.DashStyle = XDashStyle.Dash; gfx.DrawArc(pen, x + 20, y, 100, 60, 150, 120); // Draw a star XGraphicsState gs = gfx.Save(); gfx.TranslateTransform(x + 140, y + 30); for (int idx = 0; idx < 360; idx += 10) { gfx.RotateTransform(10); gfx.DrawLine(XPens.DarkGreen, 0, 0, 30, 0); } gfx.Restore(gs); // Draw a rounded rectangle rect = new XRect(x + 230, y, 100, 60); pen = new XPen(XColors.DarkBlue, 2.5); XColor color1 = XColor.FromKnownColor(KnownColor.DarkBlue); XColor color2 = XColors.Red; XLinearGradientBrush lbrush = new XLinearGradientBrush(rect, color1, color2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(pen, lbrush, rect, new XSize(10, 10)); // Draw a pie pen = new XPen(XColors.DarkOrange, 1.5); pen.DashStyle = XDashStyle.Dot; gfx.DrawPie(pen, XBrushes.Blue, x + 360, y, 100, 60, -130, 135); // Draw some more text y += 60 + 2 * ls; gfx.DrawString("With XGraphics you can draw on a PDF page as well as " + "on any System.Drawing.Graphics object.", font, XBrushes.Black, x, y); y += ls * 1.1; gfx.DrawString("Use the same code to", font, XBrushes.Black, x, y); x += 10; y += ls * 1.1; gfx.DrawString("• draw on a newly created PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw above or beneath of the content of an existing PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a window", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw on a printer", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a bitmap image", font, XBrushes.Black, x, y); x -= 10; y += ls * 1.1; gfx.DrawString("You can also import an existing PDF page and use it like " + "an image, e.g. draw it on another PDF page.", font, XBrushes.Black, x, y); y += ls * 1.1 * 2; gfx.DrawString("Imported PDF pages are neither drawn nor printed; create a " + "PDF file to see or print them!", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; gfx.DrawString("Below this text is a PDF form that will be visible when " + "viewed or printed with a PDF viewer.", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; XGraphicsState state = gfx.Save(); XRect rcImage = new XRect(100, y, 100, 100 * Math.Sqrt(2)); gfx.DrawRectangle(XBrushes.Snow, rcImage); gfx.DrawImage(XPdfForm.FromFile("../../../../../PDFs/SomeLayout.pdf"), rcImage); gfx.Restore(state); }
void RealizeLinearGradientBrush(LinearGradientBrush brush, XForm xform) { XMatrix matrix = this.currentTransform; PdfShadingPattern pattern = new PdfShadingPattern(this.writer.Owner); pattern.Elements[PdfShadingPattern.Keys.PatternType] = new PdfInteger(2); // shading pattern // Setup shading PdfShading shading = new PdfShading(this.writer.Owner); PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode; PdfDictionary function = BuildShadingFunction(brush.GradientStops, colorMode); function.Elements.SetString("/@", "This is the shading function of a LinearGradientBrush"); shading.Elements[PdfShading.Keys.Function] = function; shading.Elements[PdfShading.Keys.ShadingType] = new PdfInteger(2); // Axial shading //if (colorMode != PdfColorMode.Cmyk) shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB"); //else //shading.Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK"); //double x1 = 0, y1 = 0, x2 = 0, y2 = 0; double x1 = brush.StartPoint.X; double y1 = brush.StartPoint.Y; double x2 = brush.EndPoint.X; double y2 = brush.EndPoint.Y; shading.Elements[PdfShading.Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", x1, y1, x2, y2); // old: Elements[Keys.Background] = new PdfRawItem("[0 1 1]"); // old: Elements[Keys.Domain] = shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[true true]"); // Setup pattern pattern.Elements[PdfShadingPattern.Keys.Shading] = shading; pattern.Elements[PdfShadingPattern.Keys.Matrix] = PdfLiteral.FromMatrix(matrix); // new PdfLiteral("[" + PdfEncoders.ToString(matrix) + "]"); string name = this.writer.Resources.AddPattern(pattern); this.writer.WriteLiteral("/Pattern cs\n", name); this.writer.WriteLiteral("{0} scn\n", name); double alpha = brush.Opacity * brush.GradientStops.GetAverageAlpha(); if (alpha < 1 && this.writer.renderMode == RenderMode.Default) { #if true PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteLiteral("{0} gs\n", gs); #else #if true if (xform == null) { PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(alpha); string gs = this.writer.Resources.AddExtGState(extGState); this.writer.WriteGraphicState(extGState); } else { //PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form); PdfFormXObject pdfForm = xform.pdfForm; pdfForm.Elements.SetString("/@", "This is the Form XObject of the soft mask"); string formName = this.writer.Resources.AddForm(pdfForm); PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner); //this.writer.Owner.Internals.AddObject(tgAttributes); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); // Set reference to transparency group attributes pdfForm.Elements.SetObject(PdfFormXObject.Keys.Group, tgAttributes); pdfForm.Elements[PdfFormXObject.Keys.Matrix] = new PdfLiteral("[1.001 0 0 1.001 0.001 0.001]"); PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner); this.writer.Owner.Internals.AddObject(softmask); softmask.Elements.SetString("/@", "This is the soft mask"); softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm); //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group"); //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group"); PdfExtGState extGState = new PdfExtGState(this.writer.Owner); this.writer.Owner.Internals.AddObject(extGState); extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); this.writer.WriteGraphicState(extGState); } #else XForm form = new XForm(this.writer.Owner, 220, 140); XGraphics formGfx = XGraphics.FromForm(form); // draw something //XSolidBrush xbrush = new XSolidBrush(XColor.FromArgb(128, 128, 128)); XLinearGradientBrush xbrush = new XLinearGradientBrush(new XPoint(0, 0), new XPoint(220,0), XColors.White, XColors.Black); formGfx.DrawRectangle(xbrush, -10000, -10000, 20000, 20000); //formGfx.DrawString("Text, Graphics, Images, and Forms", new XFont("Verdana", 10, XFontStyle.Regular), XBrushes.Navy, 3, 0, XStringFormat.TopLeft); formGfx.Dispose(); // Close form form.Finish(); PdfFormXObject pdfForm = this.writer.Owner.FormTable.GetForm(form); string formName = this.writer.Resources.AddForm(pdfForm); //double x = 20, y = 20; //double cx = 1; //double cy = 1; //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n", // x, y, cx, cy, y + 0, formName); //this.writer.AppendFormat("q {2:0.###} 0 0 -{3:0.###} {0:0.###} {4:0.###} cm 100 Tz {5} Do Q\n", // x, y, cx, cy, y + 220/1.5, formName); PdfTransparencyGroupAttributes tgAttributes = new PdfTransparencyGroupAttributes(this.writer.Owner); this.writer.Owner.Internals.AddObject(tgAttributes); tgAttributes.Elements.SetName(PdfTransparencyGroupAttributes.Keys.CS, "/DeviceRGB"); // Set reference to transparency group attributes pdfForm.Elements.SetReference(PdfFormXObject.Keys.Group, tgAttributes); PdfSoftMask softmask = new PdfSoftMask(this.writer.Owner); this.writer.Owner.Internals.AddObject(softmask); softmask.Elements.SetName(PdfSoftMask.Keys.S, "/Luminosity"); softmask.Elements.SetReference(PdfSoftMask.Keys.G, pdfForm); //pdfForm.Elements.SetName(PdfFormXObject.Keys.Type, "Group"); //pdfForm.Elements.SetName(PdfFormXObject.Keys.ss.Ss.Type, "Group"); PdfExtGState extGState = new PdfExtGState(this.writer.Owner); this.writer.Owner.Internals.AddObject(extGState); extGState.Elements.SetReference(PdfExtGState.Keys.SMask, softmask); this.writer.WriteGraphicState(extGState); #endif #endif } }
/// <summary> /// Setups the shading from the specified brush. /// </summary> public void SetupFromBrush(XLinearGradientBrush brush) { if (brush == null) throw new ArgumentNullException("brush"); PdfColorMode colorMode = this.document.Options.ColorMode; XColor color1 = ColorSpaceHelper.EnsureColorMode(colorMode, brush.color1); XColor color2 = ColorSpaceHelper.EnsureColorMode(colorMode, brush.color2); PdfDictionary function = new PdfDictionary(); Elements[Keys.ShadingType] = new PdfInteger(2); if (colorMode != PdfColorMode.Cmyk) Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB"); else Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK"); double x1 = 0, y1 = 0, x2 = 0, y2 = 0; if (brush.useRect) { switch (brush.linearGradientMode) { case XLinearGradientMode.Horizontal: x1 = brush.rect.x; y1 = brush.rect.y; x2 = brush.rect.x + brush.rect.width; y2 = brush.rect.y; break; case XLinearGradientMode.Vertical: x1 = brush.rect.x; y1 = brush.rect.y; x2 = brush.rect.x; y2 = brush.rect.y + brush.rect.height; break; case XLinearGradientMode.ForwardDiagonal: x1 = brush.rect.x; y1 = brush.rect.y; x2 = brush.rect.x + brush.rect.width; y2 = brush.rect.y + brush.rect.height; break; case XLinearGradientMode.BackwardDiagonal: x1 = brush.rect.x + brush.rect.width; y1 = brush.rect.y; x2 = brush.rect.x; y2 = brush.rect.y + brush.rect.height; break; } } else { x1 = brush.point1.x; y1 = brush.point1.y; x2 = brush.point2.x; y2 = brush.point2.y; } Elements[Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", x1, y1, x2, y2); //Elements[Keys.Background] = new PdfRawItem("[0 1 1]"); //Elements[Keys.Domain] = Elements[Keys.Function] = function; //Elements[Keys.Extend] = new PdfRawItem("[true true]"); string clr1 = "[" + PdfEncoders.ToString(color1, colorMode) + "]"; string clr2 = "[" + PdfEncoders.ToString(color2, colorMode) + "]"; function.Elements["/FunctionType"] = new PdfInteger(2); function.Elements["/C0"] = new PdfLiteral(clr1); function.Elements["/C1"] = new PdfLiteral(clr2); function.Elements["/Domain"] = new PdfLiteral("[0 1]"); function.Elements["/N"] = new PdfInteger(1); }
/// <summary> /// Draws all charts inside the ChartFrame. /// </summary> public void Draw(XGraphics gfx) { // Draw frame of ChartFrame. First shadow frame. int dx = 5; int dy = 5; gfx.DrawRoundedRectangle(XBrushes.Gainsboro, this.location.X + dx, this.location.Y + dy, this.size.Width, this.size.Height, 20, 20); XRect chartRect = new XRect(this.location.X, this.location.Y, this.size.Width, this.size.Height); XLinearGradientBrush brush = new XLinearGradientBrush(chartRect, XColor.FromArgb(0xFFD0DEEF), XColors.White, XLinearGradientMode.Vertical); XPen penBorder = new XPen(XColors.SteelBlue, 2.5); gfx.DrawRoundedRectangle(penBorder, brush, this.location.X, this.location.Y, this.size.Width, this.size.Height, 15, 15); XGraphicsState state = gfx.Save(); gfx.TranslateTransform(this.location.X, this.location.Y); // Calculate rectangle for all charts. Y-Position will be moved for each chart. int charts = this.chartList.Count; uint dxChart = 20; uint dyChart = 20; uint dyBetweenCharts = 30; XRect rect = new XRect(dxChart, dyChart, this.size.Width - 2 * dxChart, (this.size.Height - (charts - 1) * dyBetweenCharts - 2 * dyChart) / charts); // draw each chart in list foreach (Chart chart in this.chartList) { RendererParameters parms = new RendererParameters(gfx, rect); parms.DrawingItem = chart; ChartRenderer renderer = GetChartRenderer(chart, parms); renderer.Init(); renderer.Format(); renderer.Draw(); rect.Y += rect.Height + dyBetweenCharts; } gfx.Restore(state); // // Calculate rectangle for all charts. Y-Position will be moved for each chart. // int charts = this.chartList.Count; // uint dxChart = 0; // uint dyChart = 0; // uint dyBetweenCharts = 0; // XRect rect = new XRect(dxChart, dyChart, // this.size.Width - 2 * dxChart, // (this.size.Height - (charts - 1) * dyBetweenCharts - 2 * dyChart) / charts); // // // draw each chart in list // foreach (Chart chart in this.chartList) // { // RendererParameters parms = new RendererParameters(gfx, rect); // parms.DrawingItem = chart; // // ChartRenderer renderer = GetChartRenderer(chart, parms); // renderer.Init(); // renderer.Format(); // renderer.Draw(); // // rect.Y += rect.Height + dyBetweenCharts; // } }
/// <summary> /// Setups the shading from the specified brush. /// </summary> internal void SetupFromBrush(XLinearGradientBrush brush, XGraphicsPdfRenderer renderer) { if (brush == null) throw new ArgumentNullException("brush"); PdfColorMode colorMode = _document.Options.ColorMode; XColor color1 = ColorSpaceHelper.EnsureColorMode(colorMode, brush._color1); XColor color2 = ColorSpaceHelper.EnsureColorMode(colorMode, brush._color2); PdfDictionary function = new PdfDictionary(); Elements[Keys.ShadingType] = new PdfInteger(2); if (colorMode != PdfColorMode.Cmyk) Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB"); else Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK"); double x1 = 0, y1 = 0, x2 = 0, y2 = 0; if (brush._useRect) { XPoint pt1 = renderer.WorldToView(brush._rect.TopLeft); XPoint pt2 = renderer.WorldToView(brush._rect.BottomRight); switch (brush._linearGradientMode) { case XLinearGradientMode.Horizontal: x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt1.Y; break; case XLinearGradientMode.Vertical: x1 = pt1.X; y1 = pt1.Y; x2 = pt1.X; y2 = pt2.Y; break; case XLinearGradientMode.ForwardDiagonal: x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt2.Y; break; case XLinearGradientMode.BackwardDiagonal: x1 = pt2.X; y1 = pt1.Y; x2 = pt1.X; y2 = pt2.Y; break; } } else { XPoint pt1 = renderer.WorldToView(brush._point1); XPoint pt2 = renderer.WorldToView(brush._point2); x1 = pt1.X; y1 = pt1.Y; x2 = pt2.X; y2 = pt2.Y; } const string format = Config.SignificantFigures3; Elements[Keys.Coords] = new PdfLiteral("[{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}]", x1, y1, x2, y2); //Elements[Keys.Background] = new PdfRawItem("[0 1 1]"); //Elements[Keys.Domain] = Elements[Keys.Function] = function; //Elements[Keys.Extend] = new PdfRawItem("[true true]"); string clr1 = "[" + PdfEncoders.ToString(color1, colorMode) + "]"; string clr2 = "[" + PdfEncoders.ToString(color2, colorMode) + "]"; function.Elements["/FunctionType"] = new PdfInteger(2); function.Elements["/C0"] = new PdfLiteral(clr1); function.Elements["/C1"] = new PdfLiteral(clr2); function.Elements["/Domain"] = new PdfLiteral("[0 1]"); function.Elements["/N"] = new PdfInteger(1); }
/// <summary> /// Converts a GDI-Brush to a PDF-XBrush. /// </summary> /// <remarks> /// Only Solid- and LinearGradientBrushes are supported. /// </remarks> /// <param name="brush">The GDI-Brush to convert.</param> /// <returns>The converted PDF-XBrush.</returns> private static XBrush BrushToXBrush(Brush brush) { XBrush xbrush; SolidBrush solidBrush; LinearGradientBrush lgBrush; if((solidBrush = brush as SolidBrush) != null) { xbrush = new XSolidBrush(solidBrush.Color); } else if((lgBrush = brush as LinearGradientBrush) != null) { //There is no way to extract the angle of the gradient out of the GDI-brush. //It only has a transformation matrix. To create a new gradient for pdfsharp, //we use this matrix as follows: //Create a "line" (start and end point) through the rectangle at the half of //the heigth. The two points are p1 and p2. Transform these points with the //matrix. The transformed points are located on the border of the rectangle. PointF p1 = new PointF(lgBrush.Rectangle.Left, lgBrush.Rectangle.Top + lgBrush.Rectangle.Height/2.0f); PointF p2 = new PointF(lgBrush.Rectangle.Right, lgBrush.Rectangle.Top + lgBrush.Rectangle.Height/2.0f); PointF[] points = new[] {p1, p2}; lgBrush.Transform.TransformPoints(points); p1 = points[0]; p2 = points[1]; //The direction is ok now. But the line might be to short. That is the case if //the line is neither horizontal, nor vertical, nor diagonal. To fill the whole //rectangle with the gradient, the start and end point of the line must be located //outside the rectangle. To determine this gap we have to use some trigonometry. //This will happily never the case in NClass. So we don't have to do this here. xbrush = new XLinearGradientBrush(p1, p2, lgBrush.LinearColors[0], lgBrush.LinearColors[1]); } else { throw new NotImplementedException("Brush type not supported by PDFsharp."); } return xbrush; }
public static void BeginBox(XGraphics gfx, int number, string title, double borderWidth, double borderHeight, XColor shadowColor, XColor backColor, XColor backColor2, XPen borderPen) { const int dEllipse = 15; XRect rect = new XRect(0, 20, 300, 200); if (number % 2 == 0) rect.X = 300 - 5; rect.Y = 40 + ((number - 1) / 2) * (200 - 5); rect.Inflate(-10, -10); XRect rect2 = rect; rect2.Offset(borderWidth, borderHeight); gfx.DrawRoundedRectangle(new XSolidBrush(shadowColor), rect2, new XSize(dEllipse + 8, dEllipse + 8)); XLinearGradientBrush brush = new XLinearGradientBrush(rect, backColor, backColor2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(borderPen, brush, rect, new XSize(dEllipse, dEllipse)); rect.Inflate(-5, -5); XFont font = new XFont("Verdana", 12, XFontStyle.Regular); gfx.DrawString(title, font, XBrushes.Navy, rect, XStringFormats.TopCenter); rect.Inflate(-10, -5); rect.Y += 20; rect.Height -= 20; state = gfx.Save(); gfx.TranslateTransform(rect.X, rect.Y); }