/// <summary> /// Initializes a new instance of the XGraphics class. /// </summary> /// <param name="gfx">The GFX.</param> /// <param name="size">The size.</param> /// <param name="pageUnit">The page unit.</param> /// <param name="pageDirection">The page direction.</param> XGraphics(Graphics gfx, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection) { if (gfx == null) throw new ArgumentNullException("gfx"); this.gfx = gfx; this.drawGraphics = true; this.pageSize = new XSize(size.width, size.height); this.pageUnit = pageUnit; switch (pageUnit) { case XGraphicsUnit.Point: this.pageSizePoints = new XSize(size.width, size.height); break; case XGraphicsUnit.Inch: this.pageSizePoints = new XSize(XUnit.FromInch(size.width), XUnit.FromInch(size.height)); break; case XGraphicsUnit.Millimeter: this.pageSizePoints = new XSize(XUnit.FromMillimeter(size.width), XUnit.FromMillimeter(size.height)); break; case XGraphicsUnit.Centimeter: this.pageSizePoints = new XSize(XUnit.FromCentimeter(size.width), XUnit.FromCentimeter(size.height)); break; default: throw new NotImplementedException("unit"); } this.pageDirection = pageDirection; Initialize(); }
/// <summary> /// Initializes a new instance of the XUnit class. /// </summary> public UnitConverter(float value, XGraphicsUnit type) { if (!Enum.IsDefined(typeof(XGraphicsUnit), type)) throw new System.ComponentModel.InvalidEnumArgumentException("type"); this.value = value; this.type = type; }
/// <summary> /// Initializes a new instance of the XUnit class. /// </summary> public XUnit(double value, XGraphicsUnit type) { if (!Enum.IsDefined(typeof(XGraphicsUnit), type)) #if !SILVERLIGHT && !NETFX_CORE && !UWP throw new System.ComponentModel.InvalidEnumArgumentException("type"); #else throw new ArgumentException("type"); #endif _value = value; _type = type; }
/// <summary> /// Initializes a new instance of the XGraphics class. /// </summary> /// <param name="gfx">The gfx.</param> /// <param name="size">The size.</param> /// <param name="pageUnit">The page unit.</param> /// <param name="pageDirection">The page direction.</param> XGraphics(Graphics gfx, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection) { if (gfx == null) { //throw new ArgumentNullException("gfx"); gfx = Graphics.FromHwnd(IntPtr.Zero); } this.gsStack = new GraphicsStateStack(this); this.targetContext = XGraphicTargetContext.GDI; this.gfx = gfx; this.drawGraphics = true; this.pageSize = new XSize(size.width, size.height); this.pageUnit = pageUnit; switch (pageUnit) { case XGraphicsUnit.Point: this.pageSizePoints = new XSize(size.width, size.height); break; case XGraphicsUnit.Inch: this.pageSizePoints = new XSize(XUnit.FromInch(size.width), XUnit.FromInch(size.height)); break; case XGraphicsUnit.Millimeter: this.pageSizePoints = new XSize(XUnit.FromMillimeter(size.width), XUnit.FromMillimeter(size.height)); break; case XGraphicsUnit.Centimeter: this.pageSizePoints = new XSize(XUnit.FromCentimeter(size.width), XUnit.FromCentimeter(size.height)); break; case XGraphicsUnit.Presentation: this.pageSizePoints = new XSize(XUnit.FromPresentation(size.width), XUnit.FromPresentation(size.height)); break; default: throw new NotImplementedException("unit"); } this.pageDirection = pageDirection; Initialize(); }
/// <summary> /// Creates the measure context. This is a graphics context created only for querying measures of text. /// Drawing on a measure context has no effect. /// </summary> public static XGraphics CreateMeasureContext(XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection) { XGraphics gfx = null; #if GDI gfx = new XGraphics((System.Drawing.Graphics)null, size, pageUnit, pageDirection); #endif #if WPF && !SILVERLIGHT gfx = new XGraphics((System.Windows.Media.DrawingContext)null, size, pageUnit, pageDirection); #endif #if SILVERLIGHT gfx = new XGraphics(new Canvas(), size, pageUnit, pageDirection); #endif return gfx; }
/// <summary> /// Saves a graphics container with the current state of this XGraphics and /// opens and uses a new graphics container. /// </summary> public XGraphicsContainer BeginContainer(Rect dstrect, Rect srcrect, XGraphicsUnit unit) { return BeginContainer(new XRect(dstrect), new XRect(dstrect), unit); }
/// <summary> /// Saves a graphics container with the current state of this XGraphics and /// opens and uses a new graphics container. /// </summary> public XGraphicsContainer BeginContainer(XRect dstrect, XRect srcrect, XGraphicsUnit unit) { // TODO: unit if (unit != XGraphicsUnit.Point) throw new ArgumentException("The current implementation supports XGraphicsUnit.Point only.", "unit"); XGraphicsContainer xContainer = null; #if GDI if (this.targetContext == XGraphicTargetContext.GDI) xContainer = new XGraphicsContainer(this.gfx.Save()); #endif #if WPF if (this.targetContext == XGraphicTargetContext.WPF) xContainer = new XGraphicsContainer(); #endif InternalGraphicsState iState = new InternalGraphicsState(this, xContainer); iState.Transform = this.transform; this.gsStack.Push(iState); if (this.renderer != null) this.renderer.BeginContainer(xContainer, dstrect, srcrect, unit); XMatrix matrix = new XMatrix(); //XMatrix.Identity; #if true double scaleX = dstrect.Width / srcrect.Width; double scaleY = dstrect.Height / srcrect.Height; matrix.TranslatePrepend(-srcrect.X, -srcrect.Y); matrix.ScalePrepend(scaleX, scaleY); matrix.TranslatePrepend(dstrect.X / scaleX, dstrect.Y / scaleY); #else matrix.Translate(-dstrect.X, -dstrect.Y); matrix.Scale(dstrect.Width / srcrect.Width, dstrect.Height / srcrect.Height); matrix.Translate(srcrect.X, srcrect.Y); #endif AddTransform(matrix, XMatrixOrder.Prepend); return xContainer; }
/// <summary> /// Converts an existing object from one unit into another unit type. /// </summary> public void ConvertType(XGraphicsUnit type) { if (_type == type) return; switch (type) { case XGraphicsUnit.Point: _value = Point; _type = XGraphicsUnit.Point; break; case XGraphicsUnit.Inch: _value = Inch; _type = XGraphicsUnit.Inch; break; case XGraphicsUnit.Centimeter: _value = Centimeter; _type = XGraphicsUnit.Centimeter; break; case XGraphicsUnit.Millimeter: _value = Millimeter; _type = XGraphicsUnit.Millimeter; break; case XGraphicsUnit.Presentation: _value = Presentation; _type = XGraphicsUnit.Presentation; break; default: throw new ArgumentException("Unknown unit type: '" + type + "'"); } }
/// <summary> /// Draws the specified image. /// </summary> public void DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit) { if (image == null) throw new ArgumentNullException("image"); CheckXPdfFormConsistence(image); if (this.drawGraphics) { #if GDI if (this.targetContext == XGraphicTargetContext.GDI) { if (image.gdiImage != null) { InterpolationMode interpolationMode = InterpolationMode.Invalid; if (!image.Interpolate) { interpolationMode = gfx.InterpolationMode; gfx.InterpolationMode = InterpolationMode.NearestNeighbor; } RectangleF destRectF = new RectangleF((float)destRect.X, (float)destRect.Y, (float)destRect.Width, (float)destRect.Height); RectangleF srcRectF = new RectangleF((float)srcRect.X, (float)srcRect.Y, (float)srcRect.Width, (float)srcRect.Height); this.gfx.DrawImage(image.gdiImage, destRectF, srcRectF, GraphicsUnit.Pixel); if (!image.Interpolate) gfx.InterpolationMode = interpolationMode; } else { DrawMissingImageRect(new XRect(destRect.x, destRect.y, destRect.width, destRect.height)); //this.gfx.DrawRectangle(Pens.Red, (float)destRect.X, (float)destRect.Y, // (float)destRect.Width, (float)destRect.Height); //this.gfx.DrawLine(Pens.Red, (float)destRect.X, (float)destRect.Y, (float)(destRect.X + destRect.Width), (float)(destRect.Y + destRect.Height)); //this.gfx.DrawLine(Pens.Red, (float)(destRect.X + destRect.Width), (float)destRect.Y, (float)destRect.X, (float)(destRect.Y + destRect.Height)); } } #endif #if WPF if (this.targetContext == XGraphicTargetContext.WPF) { if (image.wpfImage != null) { //InterpolationMode interpolationMode = InterpolationMode.Invalid; //if (!image.Interpolate) //{ // interpolationMode = gfx.InterpolationMode; // //gfx.InterpolationMode = InterpolationMode.NearestNeighbor; //} // HACK: srcRect is ignored double x = destRect.x; double y = destRect.y; this.dc.DrawImage(image.wpfImage, new System.Windows.Rect(destRect.x, destRect.y, destRect.width, destRect.height)); //if (!image.Interpolate) // gfx.InterpolationMode = interpolationMode; } else { DrawMissingImageRect(destRect); } } #endif } if (this.renderer != null) this.renderer.DrawImage(image, destRect, srcRect, srcUnit); }
/// <summary> /// Creates a new instance of the XGraphics class from a System.Windows.Media.DrawingContext object. /// </summary> public static XGraphics FromCanvas(Canvas canvas, XSize size, XGraphicsUnit unit) { return new XGraphics(canvas, size, unit, XPageDirection.Downwards); }
/// <summary> /// Creates a new instance of the XGraphics class from a PdfSharp.Pdf.PdfPage object. /// </summary> public static XGraphics FromPdfPage(PdfPage page, XGraphicsPdfPageOptions options, XGraphicsUnit unit, XPageDirection pageDirection) { return new XGraphics(page, options, unit, pageDirection); }
public void BeginContainer(XGraphicsContainer container, XRect dstrect, XRect srcrect, XGraphicsUnit unit) { // Before saving, the current transformation matrix must be completely realized. BeginGraphic(); RealizeTransform(); this.gfxState.InternalState = container.InternalState; SaveState(); //throw new NotImplementedException("BeginContainer"); // PdfGraphicsState pdfstate = (PdfGraphicsState)this.gfxState.Clone(); // this.gfxStateStack.Push(pdfstate); // container.Handle = pdfstate; // container.Handle = this.gfxState.Clone(); // Matrix matrix = new Matrix(); // matrix.Translate(srcrect.X, srcrect.Y); // matrix.Scale(dstrect.Width / srcrect.Width, dstrect.Height / srcrect.Height); // matrix.Translate(dstrect.X, dstrect.Y); // Transform = matrix; }
// -------------------------------------------------------------------------------------------- #region Transformation public void SetPageTransform(XPageDirection direction, XPoint origion, XGraphicsUnit unit) { if (this.gfxStateStack.Count > 0) throw new InvalidOperationException("PageTransformation can be modified only when the graphics stack is empty."); throw new NotImplementedException("SetPageTransform"); }
/// <summary> /// Initializes a new instance of the XGraphics class. /// </summary> /// <param name="dc">The drawing context.</param> /// <param name="size">The size.</param> /// <param name="pageUnit">The page unit.</param> /// <param name="pageDirection">The page direction.</param> XGraphics(DrawingContext dc, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection) { if (dc == null) { //throw new ArgumentNullException("dc"); this.dv = new DrawingVisual(); dc = this.dv.RenderOpen(); } this.gsStack = new GraphicsStateStack(this); this.targetContext = XGraphicTargetContext.WPF; this.dc = dc; this.drawGraphics = true; this.pageSize = new XSize(size.width, size.height); this.pageUnit = pageUnit; switch (pageUnit) { case XGraphicsUnit.Point: this.pageSizePoints = new XSize(size.width, size.height); break; case XGraphicsUnit.Inch: this.pageSizePoints = new XSize(XUnit.FromInch(size.width), XUnit.FromInch(size.height)); break; case XGraphicsUnit.Millimeter: this.pageSizePoints = new XSize(XUnit.FromMillimeter(size.width), XUnit.FromMillimeter(size.height)); break; case XGraphicsUnit.Centimeter: this.pageSizePoints = new XSize(XUnit.FromCentimeter(size.width), XUnit.FromCentimeter(size.height)); break; default: throw new NotImplementedException("unit"); } this.pageDirection = pageDirection; Initialize(); }
// TODO: incomplete - srcRect not used public void DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit) { double x = destRect.X; double y = destRect.Y; double width = destRect.Width; double height = destRect.Height; string name = Realize(image); if (!(image is XForm)) { if (this.gfx.PageDirection == XPageDirection.Downwards) { AppendFormat("q {2:0.####} 0 0 -{3:0.####} {0:0.####} {4:0.####} cm {5} Do\nQ\n", x, y, width, height, y + height, name); } else { AppendFormat("q {2:0.####} 0 0 {3:0.####} {0:0.####} {1:0.####} cm {4} Do Q\n", x, y, width, height, name); } } else { BeginPage(); XForm xForm = (XForm)image; xForm.Finish(); PdfFormXObject pdfForm = Owner.FormTable.GetForm(xForm); double cx = width / image.PointWidth; double cy = height / image.PointHeight; if (cx != 0 && cy != 0) { if (this.gfx.PageDirection == XPageDirection.Downwards) { AppendFormat("q {2:0.####} 0 0 -{3:0.####} {0:0.####} {4:0.####} cm 100 Tz {5} Do Q\n", x, y, cx, cy, y + height, name); } else { AppendFormat("q {2:0.####} 0 0 {3:0.####} {0:0.####} {1:0.####} cm {4} Do Q\n", x, y, cx, cy, name); } } } }
public void BeginContainer(XGraphicsContainer container, XRect dstrect, XRect srcrect, XGraphicsUnit unit) { // Before saving, the current transformation matrix must be completely realized. BeginGraphicMode(); RealizeTransform(); _gfxState.InternalState = container.InternalState; SaveState(); }
// TODO: incomplete - srcRect not used public void DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit) { const string format = Config.SignificantFigures4; double x = destRect.X; double y = destRect.Y; double width = destRect.Width; double height = destRect.Height; string name = Realize(image); if (!(image is XForm)) { if (_gfx.PageDirection == XPageDirection.Downwards) { AppendFormatImage("q {2:" + format + "} 0 0 {3:" + format + "} {0:" + format + "} {1:" + format + "} cm {4} Do\nQ\n", x, y + height, width, height, name); } else { AppendFormatImage("q {2:" + format + "} 0 0 {3:" + format + "} {0:" + format + "} {1:" + format + "} cm {4} Do Q\n", x, y, width, height, name); } } else { BeginPage(); XForm form = (XForm)image; form.Finish(); PdfFormXObject pdfForm = Owner.FormTable.GetForm(form); double cx = width / image.PointWidth; double cy = height / image.PointHeight; if (cx != 0 && cy != 0) { XPdfForm xForm = image as XPdfForm; if (_gfx.PageDirection == XPageDirection.Downwards) { double xDraw = x; double yDraw = y; if (xForm != null) { // Yes, it is an XPdfForm - adjust the position where the page will be drawn. xDraw -= xForm.Page.MediaBox.X1; yDraw += xForm.Page.MediaBox.Y1; } AppendFormatImage("q {2:" + format + "} 0 0 {3:" + format + "} {0:" + format + "} {1:" + format + "} cm {4} Do Q\n", xDraw, yDraw + height, cx, cy, name); } else { // TODO Translation for MediaBox. AppendFormatImage("q {2:" + format + "} 0 0 {3:" + format + "} {0:" + format + "} {1:" + format + "} cm {4} Do Q\n", x, y, cx, cy, name); } } } }
/// <summary> /// Draws the specified image. /// </summary> public void DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit) { if (image == null) throw new ArgumentNullException("image"); CheckXPdfFormConsistence(image); if (this.drawGraphics) { if (image.gdiImage != null) { InterpolationMode interpolationMode = InterpolationMode.Invalid; if (!image.Interpolate) { interpolationMode = gfx.InterpolationMode; gfx.InterpolationMode = InterpolationMode.NearestNeighbor; } RectangleF destRectF = new RectangleF((float)destRect.X, (float)destRect.Y, (float)destRect.Width, (float)destRect.Height); RectangleF srcRectF = new RectangleF((float)srcRect.X, (float)srcRect.Y, (float)srcRect.Width, (float)srcRect.Height); this.gfx.DrawImage(image.gdiImage, destRectF, srcRectF, GraphicsUnit.Pixel); if (!image.Interpolate) gfx.InterpolationMode = interpolationMode; } else { this.gfx.DrawRectangle(Pens.Red, (float)destRect.X, (float)destRect.Y, (float)destRect.Width, (float)destRect.Height); this.gfx.DrawLine(Pens.Red, (float)destRect.X, (float)destRect.Y, (float)(destRect.X + destRect.Width), (float)(destRect.Y + destRect.Height)); this.gfx.DrawLine(Pens.Red, (float)(destRect.X + destRect.Width), (float)destRect.Y, (float)destRect.X, (float)(destRect.Y + destRect.Height)); } } if (this.renderer != null) this.renderer.DrawImage(image, destRect, srcRect, srcUnit); }
/// <summary> /// Creates a new instance of the XGraphics class from a System.Drawing.Graphics object. /// </summary> public static XGraphics FromGraphics(Graphics graphics, XSize size, XGraphicsUnit unit) { // TODO: Get object from cache... return new XGraphics(graphics, size, unit, XPageDirection.Downwards); }
/// <summary> /// Converts an existing object from one unit into another unit type. /// </summary> public void ConvertType(XGraphicsUnit type) { if (this.type == type) return; switch (type) { case XGraphicsUnit.Point: this.value = this.Point; this.type = XGraphicsUnit.Point; break; case XGraphicsUnit.Centimeter: this.value = this.Centimeter; this.type = XGraphicsUnit.Centimeter; break; case XGraphicsUnit.Inch: this.value = this.Inch; this.type = XGraphicsUnit.Inch; break; case XGraphicsUnit.Millimeter: this.value = this.Millimeter; this.type = XGraphicsUnit.Millimeter; break; case XGraphicsUnit.Pixel: this.value = this.Pixel; this.type = XGraphicsUnit.Pixel; break; // case XGraphicsUnit.Pica: // this.value = this.Pc; // this.type = XGraphicsUnit.Pica; // break; // // case XGraphicsUnit.Line: // this.value = this.Li; // this.type = XGraphicsUnit.Line; // break; default: throw new ArgumentException("Unknown unit type: '" + type.ToString() + "'"); } }
///// <summary> ///// Creates a new instance of the XGraphics class from a System.Drawing.Graphics object. ///// </summary> //public static XGraphics FromGraphics(Graphics graphics, XSize size, XPageDirection pageDirection) //{ // // TODO: Get object from cache... // return new XGraphics(graphics, size, XGraphicsUnit.Point, pageDirection); //} ///// <summary> ///// Creates a new instance of the XGraphics class from a System.Drawing.Graphics object. ///// </summary> //public static XGraphics FromGraphics(Graphics graphics, XSize size, XGraphicsUnit unit, XPageDirection pageDirection) //{ // // TODO: Get object from cache... // return new XGraphics(graphics, size, XGraphicsUnit.Point, pageDirection); //} #endif #if WPF && !SILVERLIGHT /// <summary> /// Creates a new instance of the XGraphics class from a System.Windows.Media.DrawingContext object. /// </summary> public static XGraphics FromDrawingContext(DrawingContext drawingContext, XSize size, XGraphicsUnit unit) { return new XGraphics(drawingContext, size, unit, XPageDirection.Downwards); }
/// <summary> /// Initializes a new instance of the XGraphics class for drawing on a PDF page. /// </summary> XGraphics(PdfPage page, XGraphicsPdfPageOptions options, XGraphicsUnit pageUnit, XPageDirection pageDirection) { if (page == null) throw new ArgumentNullException("page"); if (page.Owner == null) throw new ArgumentException("You cannot draw on a page that is not owned by a PdfDocument object.", "page"); if (page.RenderContent != null) throw new InvalidOperationException("An XGraphics object already exists for this page and must be disposed before a new one can be created."); if (page.Owner.IsReadOnly) throw new InvalidOperationException("Cannot create XGraphics for a page of a document that cannot be modiefied"); PdfContent content = null; switch (options) { case XGraphicsPdfPageOptions.Replace: page.Contents.Elements.Clear(); goto case XGraphicsPdfPageOptions.Append; case XGraphicsPdfPageOptions.Prepend: content = page.Contents.PrependContent(); break; case XGraphicsPdfPageOptions.Append: content = page.Contents.AppendContent(); break; } page.RenderContent = content; this.gfx = Graphics.FromHwnd(IntPtr.Zero); this.renderer = new PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer(page, this, options); this.pageSizePoints = new XSize(page.Width, page.Height); switch (pageUnit) { case XGraphicsUnit.Point: this.pageSize = new XSize(page.Width, page.Height); break; case XGraphicsUnit.Inch: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Inch, XUnit.FromPoint(page.Height).Inch); break; case XGraphicsUnit.Millimeter: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Millimeter, XUnit.FromPoint(page.Height).Millimeter); break; case XGraphicsUnit.Centimeter: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Centimeter, XUnit.FromPoint(page.Height).Centimeter); break; default: throw new NotImplementedException("unit"); } this.pageUnit = pageUnit; this.pageDirection = pageDirection; Initialize(); }
/// <summary> /// Creates a new instance of the XGraphics class from a PdfSharp.Pdf.PdfPage object. /// </summary> public static XGraphics FromPdfPage(PdfPage page, XGraphicsUnit unit) { return new XGraphics(page, XGraphicsPdfPageOptions.Append, unit, XPageDirection.Downwards); }
/// <summary> /// Draws the specified image. /// </summary> public void DrawImage(XImage image, RectangleF destRect, RectangleF srcRect, XGraphicsUnit srcUnit) { XRect destRectX = new XRect(destRect.X, destRect.Y, destRect.Width, destRect.Height); XRect srcRectX = new XRect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height); DrawImage(image, destRectX, srcRectX, srcUnit); }
/// <summary> /// Initializes a new instance of the XGraphics class. /// </summary> /// <param name="canvas">The canvas.</param> /// <param name="size">The size.</param> /// <param name="pageUnit">The page unit.</param> /// <param name="pageDirection">The page direction.</param> XGraphics(Canvas canvas, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection) { //throw new ArgumentNullException("canvas"); if (canvas == null) canvas = new Canvas(); this.dc = new DrawingContext(canvas); this.gsStack = new GraphicsStateStack(this); this.targetContext = XGraphicTargetContext.WPF; this.drawGraphics = true; this.pageSize = new XSize(size.width, size.height); this.pageUnit = pageUnit; switch (pageUnit) { case XGraphicsUnit.Point: this.pageSizePoints = new XSize(size.width, size.height); break; case XGraphicsUnit.Inch: this.pageSizePoints = new XSize(XUnit.FromInch(size.width), XUnit.FromInch(size.height)); break; case XGraphicsUnit.Millimeter: this.pageSizePoints = new XSize(XUnit.FromMillimeter(size.width), XUnit.FromMillimeter(size.height)); break; case XGraphicsUnit.Centimeter: this.pageSizePoints = new XSize(XUnit.FromCentimeter(size.width), XUnit.FromCentimeter(size.height)); break; case XGraphicsUnit.Presentation: this.pageSizePoints = new XSize(XUnit.FromPresentation(size.width), XUnit.FromPresentation(size.height)); break; default: throw new NotImplementedException("unit"); } this.pageDirection = pageDirection; Initialize(); }
/// <summary> /// Saves a graphics container with the current state of this XGraphics and /// opens and uses a new graphics container. /// </summary> public XGraphicsContainer BeginContainer(XRect dstrect, XRect srcrect, XGraphicsUnit unit) { // TODO: unit if (unit != XGraphicsUnit.Point) throw new ArgumentException("The current implementation supports XGraphicsUnit.Point only.", "unit"); #if true XGraphicsContainer xContainer = new XGraphicsContainer(this.gfx.Save()); InternalGraphicsState iState = new InternalGraphicsState(xContainer); iState.Transform = this.transform; this.gsStack.Push(iState); if (this.renderer != null) this.renderer.BeginContainer(xContainer, dstrect, srcrect, unit); XMatrix matrix = XMatrix.Identity; #if true double scaleX = dstrect.Width / srcrect.Width; double scaleY = dstrect.Height / srcrect.Height; matrix.Translate(-srcrect.X, -srcrect.Y); matrix.Scale(scaleX, scaleY); matrix.Translate(dstrect.X / scaleX, dstrect.Y / scaleY); #else matrix.Translate(-dstrect.X, -dstrect.Y); matrix.Scale(dstrect.Width / srcrect.Width, dstrect.Height / srcrect.Height); matrix.Translate(srcrect.X, srcrect.Y); #endif Transform = matrix; return xContainer; #else XGraphicsContainer container = new XGraphicsContainer(this.gfx.BeginContainer(dstrect, srcrect, this.gfx.PageUnit)); container.Transform = this.transform; if (this.renderer != null) this.renderer.BeginContainer(container, dstrect, srcrect, unit); return container; #endif }
/// <summary> /// Initializes a new instance of the XGraphics class for drawing on a PDF page. /// </summary> XGraphics(PdfPage page, XGraphicsPdfPageOptions options, XGraphicsUnit pageUnit, XPageDirection pageDirection) { if (page == null) throw new ArgumentNullException("page"); if (page.Owner == null) throw new ArgumentException("You cannot draw on a page that is not owned by a PdfDocument object.", "page"); if (page.RenderContent != null) throw new InvalidOperationException("An XGraphics object already exists for this page and must be disposed before a new one can be created."); if (page.Owner.IsReadOnly) throw new InvalidOperationException("Cannot create XGraphics for a page of a document that cannot be modified. Use PdfDocumentOpenMode.Modify."); this.gsStack = new GraphicsStateStack(this); PdfContent content = null; switch (options) { case XGraphicsPdfPageOptions.Replace: page.Contents.Elements.Clear(); goto case XGraphicsPdfPageOptions.Append; case XGraphicsPdfPageOptions.Prepend: content = page.Contents.PrependContent(); break; case XGraphicsPdfPageOptions.Append: content = page.Contents.AppendContent(); break; } page.RenderContent = content; #if GDI // HACK: This does not work with MediumTrust //this.gfx = Graphics.FromHwnd(IntPtr.Zero); this.targetContext = XGraphicTargetContext.GDI; Bitmap bm = new Bitmap(10, 10); this.gfx = Graphics.FromImage(bm); #endif #if WPF && !SILVERLIGHT this.dv = new DrawingVisual(); this.dc = this.dv.RenderOpen(); this.targetContext = XGraphicTargetContext.WPF; #endif #if SILVERLIGHT this.dc = new DrawingContext(new Canvas()); this.targetContext = XGraphicTargetContext.WPF; #endif #if GDI && WPF this.targetContext = PdfSharp.Internal.TargetContextHelper.TargetContext; #endif this.renderer = new PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer(page, this, options); this.pageSizePoints = new XSize(page.Width, page.Height); switch (pageUnit) { case XGraphicsUnit.Point: this.pageSize = new XSize(page.Width, page.Height); break; case XGraphicsUnit.Inch: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Inch, XUnit.FromPoint(page.Height).Inch); break; case XGraphicsUnit.Millimeter: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Millimeter, XUnit.FromPoint(page.Height).Millimeter); break; case XGraphicsUnit.Centimeter: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Centimeter, XUnit.FromPoint(page.Height).Centimeter); break; case XGraphicsUnit.Presentation: this.pageSize = new XSize(XUnit.FromPoint(page.Width).Presentation, XUnit.FromPoint(page.Height).Presentation); break; default: throw new NotImplementedException("unit"); } this.pageUnit = pageUnit; this.pageDirection = pageDirection; Initialize(); }
/// <summary> /// Converts a XGraphicsUnit to a readable String. /// </summary> /// <param name="unit">The XGraphicsUnit to convert.</param> /// <returns>A string representing the XGraphicsUnit.</returns> private static String GetUnitString(XGraphicsUnit unit) { switch(unit) { case XGraphicsUnit.Point: return Strings.Unit_Dots; case XGraphicsUnit.Inch: return Strings.Unit_Inch; case XGraphicsUnit.Millimeter: return Strings.Unit_MM; case XGraphicsUnit.Centimeter: return Strings.Unit_CM; case XGraphicsUnit.Presentation: return Strings.Unit_Pixel; default: return Strings.Unit_Dots; } }