/// <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="flags">Flags used to influence the rendering.</param> public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, PdfRenderFlags flags) { 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); } var point = new NativeMethods.POINT(); NativeMethods.SetViewportOrgEx(dc, bounds.X, bounds.Y, out point); bool success = _file.RenderPDFPageToDC( page, dc, (int)dpiX, (int)dpiY, 0, 0, bounds.Width, bounds.Height, FlagsToFPDFFlags(flags) ); NativeMethods.SetViewportOrgEx(dc, point.X, point.Y, out point); 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="forPrinting">Render the page for printing.</param> public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, bool forPrinting) { 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, true /* fitToBounds */, true /* stretchToBounds */, true /* keepAspectRatio */, true /* centerInBounds */, true /* autoRotate */, forPrinting ); 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="flags">Flags used to influence the rendering.</param> public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, PdfRenderFlags flags) { 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); } var point = new NativeMethods.POINT(); NativeMethods.SetViewportOrgEx(dc, bounds.X, bounds.Y, out point); bool success = _file.RenderPDFPageToDC( page, dc, (int)dpiX, (int)dpiY, 0, 0, bounds.Width, bounds.Height, FlagsToFPDFFlags(flags) ); NativeMethods.SetViewportOrgEx(dc, point.X, point.Y, out point); if (!success) throw new Win32Exception(); } finally { graphics.ReleaseHdc(dc); } }
/// <summary> /// Renders a page of the PDF document to an image. /// </summary> /// <param name="page">Number of the page to render.</param> /// <param name="width">Width of the rendered image.</param> /// <param name="height">Height of the rendered image.</param> /// <param name="dpiX">Horizontal DPI.</param> /// <param name="dpiY">Vertical DPI.</param> /// <param name="forPrinting">Render the page for printing.</param> /// <returns>The rendered image.</returns> public Image Render(int page, int width, int height, float dpiX, float dpiY, bool forPrinting) { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } var dc = NativeMethods.CreateCompatibleDC(IntPtr.Zero); try { int dcDpiX = NativeMethods.GetDeviceCaps(dc, NativeMethods.LOGPIXELSX); int dcDpiY = NativeMethods.GetDeviceCaps(dc, NativeMethods.LOGPIXELSY); if (dcDpiX != (int)dpiX || dcDpiY != (int)dpiY) { var transform = new NativeMethods.XFORM { eM11 = dcDpiX / dpiX, eM22 = dcDpiY / dpiY }; NativeMethods.SetGraphicsMode(dc, NativeMethods.GM_ADVANCED); NativeMethods.ModifyWorldTransform(dc, ref transform, NativeMethods.MWT_LEFTMULTIPLY); } var bitmap = NativeMethods.CreateCompatibleBitmap(NativeMethods.GetDC(IntPtr.Zero), width, height); try { var oldBitmap = NativeMethods.SelectObject(dc, bitmap); try { var brush = NativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(Color.White)); try { var oldBrush = NativeMethods.SelectObject(dc, brush); try { NativeMethods.Rectangle(dc, 0, 0, width, height); } finally { NativeMethods.SelectObject(dc, oldBrush); } } finally { NativeMethods.DeleteObject(brush); } // _file is an instance of PdfFile from PdfViewer port // Now render the page onto the memory DC, which in turn changes the bitmap bool success = _file.RenderPDFPageToDC( page, dc, (int)dpiX, (int)dpiY, 0, 0, width, height, true /* fitToBounds */, true /* stretchToBounds */, true /* keepAspectRatio */, true /* centerInBounds */, true /* autoRotate */, forPrinting ); if (!success) { throw new Win32Exception(); } } finally { NativeMethods.SelectObject(dc, oldBitmap); } return(Bitmap.FromHbitmap(bitmap)); } finally { NativeMethods.DeleteObject(bitmap); } } finally { NativeMethods.DeleteObject(dc); } }