Exemple #1
0
        /// <summary>
        /// Renders a page of the PDF document to the provided graphics instance.
        /// </summary>
        /// <param name="page">Number of the page to render.</param>
        /// <param name="graphics">Graphics instance to render the page on.</param>
        /// <param name="dpiX">Horizontal DPI.</param>
        /// <param name="dpiY">Vertical DPI.</param>
        /// <param name="bounds">Bounds to render the page in.</param>
        /// <param name="fitToBounds">true to fit the rendered page into the specified bounds; otherwise false.</param>
        /// <param name="stretchToBounds">true to stretch the rendered page into the specified bounds; otherwise false.</param>
        /// <param name="keepAspectRatio">true to keep the original aspect ratio in tact; otherwise false.</param>
        /// <param name="centerInBounds">true to center the page within the specified bounds; otherwise false.</param>
        /// <param name="autoRotate">true to rotate the page depending on the specified bounds; otherwise false.</param>
        public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, bool fitToBounds, bool stretchToBounds, bool keepAspectRatio, bool centerInBounds, bool autoRotate)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            float graphicsDpiX = graphics.DpiX;
            float graphicsDpiY = graphics.DpiY;

            var dc = graphics.GetHdc();

            try
            {
                if ((int)graphicsDpiX != (int)dpiX || (int)graphicsDpiY != (int)dpiY)
                {
                    var transform = new NativeMethods.XFORM
                    {
                        eM11 = graphicsDpiX / dpiX,
                        eM22 = graphicsDpiY / dpiY
                    };

                    NativeMethods.SetGraphicsMode(dc, NativeMethods.GM_ADVANCED);
                    NativeMethods.ModifyWorldTransform(dc, ref transform, NativeMethods.MWT_LEFTMULTIPLY);
                }

                bool success = _file.RenderPDFPageToDC(
                    page,
                    dc,
                    (int)dpiX, (int)dpiY,
                    bounds.X, bounds.Y, bounds.Width, bounds.Height,
                    fitToBounds,
                    stretchToBounds,
                    keepAspectRatio,
                    centerInBounds,
                    autoRotate
                    );

                if (!success)
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                graphics.ReleaseHdc(dc);
            }
        }
        /// <summary>
        /// Renders a page of the PDF document to the provided graphics instance.
        /// </summary>
        /// <param name="page">Number of the page to render.</param>
        /// <param name="graphics">Graphics instance to render the page on.</param>
        /// <param name="dpiX">Horizontal DPI.</param>
        /// <param name="dpiY">Vertical DPI.</param>
        /// <param name="bounds">Bounds to render the page in.</param>
        /// <param name="fitToBounds">true to fit the rendered page into the specified bounds; otherwise false.</param>
        /// <param name="stretchToBounds">true to stretch the rendered page into the specified bounds; otherwise false.</param>
        /// <param name="keepAspectRatio">true to keep the original aspect ratio in tact; otherwise false.</param>
        /// <param name="centerInBounds">true to center the page within the specified bounds; otherwise false.</param>
        /// <param name="autoRotate">true to rotate the page depending on the specified bounds; otherwise false.</param>
        public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, bool fitToBounds, bool stretchToBounds, bool keepAspectRatio, bool centerInBounds, bool autoRotate)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");
            if (_disposed)
                throw new ObjectDisposedException(GetType().Name);

            float graphicsDpiX = graphics.DpiX;
            float graphicsDpiY = graphics.DpiY;

            var dc = graphics.GetHdc();

            try
            {
                if ((int)graphicsDpiX != (int)dpiX || (int)graphicsDpiY != (int)dpiY)
                {
                    var transform = new NativeMethods.XFORM
                    {
                        eM11 = graphicsDpiX / dpiX,
                        eM22 = graphicsDpiY / dpiY
                    };

                    NativeMethods.SetGraphicsMode(dc, NativeMethods.GM_ADVANCED);
                    NativeMethods.ModifyWorldTransform(dc, ref transform, NativeMethods.MWT_LEFTMULTIPLY);
                }

                bool success = _file.RenderPDFPageToDC(
                    page,
                    dc,
                    (int)dpiX, (int)dpiY,
                    bounds.X, bounds.Y, bounds.Width, bounds.Height,
                    fitToBounds,
                    stretchToBounds,
                    keepAspectRatio,
                    centerInBounds,
                    autoRotate
                );

                if (!success)
                    throw new Win32Exception();
            }
            finally
            {
                graphics.ReleaseHdc(dc);
            }
        }