} // proc Render /// <summary>Render the whole page to an bitmap.</summary> /// <param name="width">Width of the result bitmap.</param> /// <param name="height">Height of the result bitmap</param> /// <param name="keepAspect">Should the page keep is aspect.</param> /// <param name="flags">Render flags</param> /// <returns>Bitmap of the page.</returns> public ImageSource Render(int width, int height, bool keepAspect = true, PdfPageRenderFlag flags = PdfPageRenderFlag.None) { if (keepAspect) { var aspect = Width / Height; if (width > height) // calc height { height = (int)(width / aspect); } else { width = (int)(height * aspect); } } if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), width, "Must be positiv."); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), height, "Must be positiv."); } // create target bitmap lock (FPDF_LibraryLock) { var hBitmap = CreatePdfBitmap(width, height); try { // render page lock (FPDF_LibraryLock) FPDF_RenderPageBitmap(hBitmap, pageHandle.DangerousGetHandle(), 0, 0, width, height, 0, (int)flags); // copy bitmap return(CreateImageSource(hBitmap, width, height)); } finally { FPDFBitmap_Destroy(hBitmap); } } } // proc Render
} // proc Render /// <summary>Render the page to an device context (e.g. for printing)</summary> /// <param name="hdc">GDI+ device context.</param> /// <param name="x">x-offset in the device context.</param> /// <param name="y">y-offset in the device context.</param> /// <param name="w">width in the device context.</param> /// <param name="h">height in the device context.</param> /// <param name="r">rotation of the page (0,90,180,270 are possible).</param> /// <param name="flags">Render flags</param> public void Render(IntPtr hdc, int x, int y, int w, int h, int r = 0, PdfPageRenderFlag flags = PdfPageRenderFlag.None) { lock (FPDF_LibraryLock) FPDF_RenderPage(hdc, pageHandle.DangerousGetHandle(), x, y, w, h, (r / 90) % 4, (int)flags); } // proc Render
} // func CreateImageSource /// <summary>Renders a Part of a Page</summary> /// <param name="pageClipping">Clipping area of the page in pixels.</param> /// <param name="scaleX">Scale factor from page units to pixels.</param> /// <param name="scaleY">Scale factor from page units to pixels.</param> /// <param name="rotation">Rotation of the page (only 0,90,180,270 are usefull)</param> /// <param name="flags">Render flags</param> /// <returns></returns> public ImageSource Render(Rect pageClipping, double scaleX, double scaleY, double rotation = 0, PdfPageRenderFlag flags = PdfPageRenderFlag.None) { lock (FPDF_LibraryLock) { // create target bitmap var width = (int)pageClipping.Width; var height = (int)pageClipping.Height; var hBitmap = CreatePdfBitmap(width, height); try { var clipping = default(FS_RECTF); var matrix = default(FS_MATRIX); clipping.left = 0.0f; clipping.top = 0.0f; clipping.right = (float)pageClipping.Width; clipping.bottom = (float)pageClipping.Height; var m = new Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); if (rotation != 0) { m.RotateAt(rotation, Width / 2, Height / 2); } m.Scale(scaleX, scaleY); m.Translate(-(float)pageClipping.X, -(float)pageClipping.Y); matrix.a = (float)m.M11; matrix.b = (float)m.M12; matrix.c = (float)m.M21; matrix.d = (float)m.M22; matrix.e = (float)m.OffsetX; matrix.f = (float)m.OffsetY; FPDF_RenderPageBitmapWithMatrix(hBitmap, pageHandle.DangerousGetHandle(), ref matrix, ref clipping, (int)flags); return(CreateImageSource(hBitmap, width, height)); } finally { FPDFBitmap_Destroy(hBitmap); } } } // proc Render