/// <summary> /// Start/Continue progressive rendering for specified page /// </summary> /// <param name="page">Pdf page object</param> /// <param name="pageRect">Actual page's rectangle. </param> /// <param name="pageRotate">Page orientation: 0 (normal), 1 (rotated 90 degrees clockwise), 2 (rotated 180 degrees), 3 (rotated 90 degrees counter-clockwise).</param> /// <param name="renderFlags">0 for normal display, or combination of flags defined above.</param> /// <param name="useProgressiveRender">True for use progressive render</param> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> internal bool RenderPage(PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags, bool useProgressiveRender) { if (!this.ContainsKey(page)) { ProcessNew(page); //Add new page into collection if (PaintBackground != null) { PaintBackground(this, new EventArgs <Rectangle>(pageRect)); } } if ((renderFlags & (RenderFlags.FPDF_THUMBNAIL | RenderFlags.FPDF_HQTHUMBNAIL)) != 0) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 4; } else if (!useProgressiveRender) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 3; } PdfBitmap bitmap = this[page].Bitmap; bool ret = ProcessExisting(bitmap ?? CanvasBitmap, page, pageRect, pageRotate, renderFlags); if (bitmap != null) { Pdfium.FPDFBitmap_CompositeBitmap(CanvasBitmap.Handle, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, bitmap.Handle, pageRect.X, pageRect.Y, BlendTypes.FXDIB_BLEND_NORMAL); } return(ret); }
/// <summary> /// Construct new instnace of the PrintPageLoadedEventArgs class /// </summary> /// <param name="page">The page what will be printed.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> public BeforeRenderPageEventArgs(PdfPage page, double width, double height, PageRotate rotation) { Page = page; Width = width; Height = height; Rotation = rotation; }
/// <summary> /// Construct new instnace of the PrintPageLoadedEventArgs class /// </summary> /// <param name="g">Drawing surface.</param> /// <param name="page">The page what will be printed.</param> /// <param name="x">Horizontal position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="y">Vertical position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> public BeforeRenderPageEventArgs(Graphics g, PdfPage page, int x, int y, int width, int height, PageRotate rotation) { Graphics = g; Page = page; X = x; Y = y; Width = width; Height = height; Rotation = rotation; }
/// <summary> /// Start/Continue progressive rendering for specified page /// </summary> /// <param name="page">Pdf page object</param> /// <param name="pageRect">Actual page's rectangle. </param> /// <param name="pageRotate">Page orientation: 0 (normal), 1 (rotated 90 degrees clockwise), 2 (rotated 180 degrees), 3 (rotated 90 degrees counter-clockwise).</param> /// <param name="renderFlags">0 for normal display, or combination of flags defined above.</param> /// <param name="useProgressiveRender">True for use progressive render</param> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> internal bool RenderPage(PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags, bool useProgressiveRender) { if (!this.ContainsKey(page)) { ProcessNew(page); //Add new page into collection } if (!useProgressiveRender) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 3; } return(ProcessExisting(page, pageRect, pageRotate, renderFlags)); }
private void CreateImagesColumn(string imageFile, PageRotate pageRotate, string imageColumnFile1, string imageColumnFile2) { using (Image image = Image.FromFile(imageFile)) { int width = image.Width; int height = image.Height; Rectangle column1; Rectangle column2; RotateFlipType?rotate = null; if (pageRotate == PageRotate.NoRotate || pageRotate == PageRotate.Rotate180) { column1 = new Rectangle(0, 0, width / 2, height); column2 = new Rectangle(width / 2, 0, width - (width / 2), height); if (pageRotate == PageRotate.Rotate180) { rotate = RotateFlipType.Rotate180FlipNone; } } else // if (parameters.PageRotate == PageRotate.Rotate90 || parameters.PageRotate == PageRotate.Rotate270) { column1 = new Rectangle(0, height / 2, width, height - (height / 2)); column2 = new Rectangle(0, 0, width, height / 2); if (pageRotate == PageRotate.Rotate90) { rotate = RotateFlipType.Rotate90FlipNone; } else { rotate = RotateFlipType.Rotate270FlipNone; } } Trace.WriteLine($"create image column \"{imageColumnFile1}\""); using (Bitmap bitmap = zimg.Crop(image, column1)) { if (rotate != null) { bitmap.RotateFlip((RotateFlipType)rotate); } bitmap.Save(imageColumnFile1, image.RawFormat); } Trace.WriteLine($"create image column \"{imageColumnFile2}\""); using (Bitmap bitmap = zimg.Crop(image, column2)) { if (rotate != null) { bitmap.RotateFlip((RotateFlipType)rotate); } bitmap.Save(imageColumnFile2, image.RawFormat); } } }
/// <summary> /// デフォルトコンストラクタ(Serialize用) /// </summary> private Page() { path = null; index = ""; locked = false; rotate = PageRotate.Rotate0; format = PageFormat.Undefined; clipLeft = 0; clipTop = 0; clipRight = 100; clipBottom = 100; bold = 0.0f; contrast = 0.0f; }
/// <summary> /// Start/Continue progressive rendering for specified page /// </summary> /// <param name="page">Pdf page object</param> /// <param name="pageRect">Actual page's rectangle. </param> /// <param name="pageRotate">Page orientation: 0 (normal), 1 (rotated 90 degrees clockwise), 2 (rotated 180 degrees), 3 (rotated 90 degrees counter-clockwise).</param> /// <param name="renderFlags">0 for normal display, or combination of flags defined above.</param> /// <param name="useProgressiveRender">True for use progressive render</param> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> internal bool RenderPage(PdfPage page, Int32Rect pageRect, PageRotate pageRotate, RenderFlags renderFlags, bool useProgressiveRender) { if (!this.ContainsKey(page)) { ProcessNew(page); //Add new page into collection } if ((renderFlags & (RenderFlags.FPDF_THUMBNAIL | RenderFlags.FPDF_HQTHUMBNAIL)) != 0) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 4; } else if (!useProgressiveRender) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 3; } return(ProcessExisting(page, pageRect, pageRotate, renderFlags)); }
private void DrawThumbnail(PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags) { int pw = (int)page.Width; int ph = (int)page.Height; int w = Math.Max(pw, (renderFlags & RenderFlags.FPDF_HQTHUMBNAIL) == RenderFlags.FPDF_HQTHUMBNAIL ? pageRect.Width : pw); int h = Math.Max(ph, (renderFlags & RenderFlags.FPDF_HQTHUMBNAIL) == RenderFlags.FPDF_HQTHUMBNAIL ? pageRect.Height : ph); using (var bmp = new PdfBitmap(w, h, true)) { page.RenderEx(bmp, 0, 0, w, h, pageRotate, renderFlags); using (var g = Graphics.FromImage(CanvasBitmap.Image)) { g.DrawImage(bmp.Image, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height); } } }
/// <summary> /// Process existing pages /// </summary> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> private bool ProcessExisting(PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags) { switch (this[page].status) { case ProgressiveRenderingStatuses.RenderReader: this[page].status = page.StartProgressiveRender(CanvasBitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags, null); if (this[page].status == ProgressiveRenderingStatuses.RenderDone) { return(true); } return(false); //Start rendering. Return nothing. case ProgressiveRenderingStatuses.RenderDone: page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //Stop rendering. Return image. case ProgressiveRenderingStatuses.RenderDone + 2: return(true); //Rendering already stoped. return image case ProgressiveRenderingStatuses.RenderDone + 3: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; page.RenderEx(CanvasBitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags); return(true); //Rendering in non progressive mode case ProgressiveRenderingStatuses.RenderDone + 4: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; DrawThumbnail(page, pageRect, pageRotate, renderFlags); return(true); //Rendering thumbnails case ProgressiveRenderingStatuses.RenderTobeContinued: this[page].status = page.ContinueProgressiveRender(); return(false); //Continue rendering. Return nothing. case ProgressiveRenderingStatuses.RenderFailed: default: CanvasBitmap.FillRect(pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, Color.Red); CanvasBitmap.FillRect(pageRect.X + 5, pageRect.Y + 5, pageRect.Width - 10, pageRect.Height - 10, Color.White); page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //Error occur. Stop rendering. return image with error } }
/// <summary> /// Raises the System.Drawing.Printing.PrintDocument.PrintPage event. It is called /// before a page prints. /// </summary> /// <param name="e"> A System.Drawing.Printing.PrintPageEventArgs that contains the event data.</param> protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); IntPtr hdc = IntPtr.Zero; IntPtr currentPage = IntPtr.Zero; try { if (e.Cancel) { return; } currentPage = Pdfium.FPDF_LoadPage(_docForPrint, _pageForPrint); if (currentPage == IntPtr.Zero) { e.Cancel = true; return; } double dpiX = e.Graphics.DpiX; double dpiY = e.Graphics.DpiY; double width, height; double x, y; CalcSize(currentPage, dpiX, dpiY, e.PageSettings.PrintableArea, e.PageSettings.Landscape, out width, out height, out x, out y); PageRotate rotation = CalcRotation(currentPage, e.PageSettings.Landscape, ref width, ref height, ref x, ref y); using (var page = PdfPage.FromHandle(_pdfDoc, currentPage, _pageForPrint)) { OnBeforeRenderPage(page, width, height, rotation); } hdc = e.Graphics.GetHdc(); Pdfium.SetWorldTransform(hdc, new FS_MATRIX(1, 0, 0, 1, x, y)); Pdfium.FPDF_RenderPage( hdc, currentPage, (int)0, (int)0, (int)(width), (int)(height), rotation, RenderFlags); //Print next page if (_pageForPrint < PrinterSettings.ToPage - (_useDP ? PrinterSettings.FromPage : 1)) { _pageForPrint++; e.HasMorePages = true; } } finally { if (hdc != IntPtr.Zero) { e.Graphics.ReleaseHdc(hdc); } hdc = IntPtr.Zero; if (currentPage != IntPtr.Zero) { Pdfium.FPDF_ClosePage(currentPage); } currentPage = IntPtr.Zero; } }
/// <summary> /// Process existing pages /// </summary> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> private bool ProcessExisting(PdfBitmap bitmap, PdfPage page, Rectangle pageRect, PageRotate pageRotate, RenderFlags renderFlags) { #if PDF_ENABLE_XFA if (page.Document.FormFill != null && page.Document.FormFill.DocumentType == DocumentTypes.DynamicXfa) { this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; } #endif switch (this[page].status) { case ProgressiveRenderingStatuses.RenderReader: this[page].status = page.StartProgressiveRender(bitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags, null); if (this[page].status == ProgressiveRenderingStatuses.RenderDone) { return(true); } return(false); //Start rendering. Return nothing. case ProgressiveRenderingStatuses.RenderDone: page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //Stop rendering. Return image. case ProgressiveRenderingStatuses.RenderDone + 2: return(true); //Rendering already stoped. return image case ProgressiveRenderingStatuses.RenderDone + 3: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; page.RenderEx(bitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags); return(true); //Rendering in non progressive mode case ProgressiveRenderingStatuses.RenderDone + 4: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; DrawThumbnail(bitmap, page, pageRect, pageRotate, renderFlags); return(true); //Rendering thumbnails case ProgressiveRenderingStatuses.RenderTobeContinued: this[page].status = page.ContinueProgressiveRender(); return(false); //Continue rendering. Return nothing. case ProgressiveRenderingStatuses.RenderFailed: default: bitmap.FillRectEx(pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, Color.Red.ToArgb()); bitmap.FillRectEx(pageRect.X + 5, pageRect.Y + 5, pageRect.Width - 10, pageRect.Height - 10, Color.White.ToArgb()); page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //An error has occurred. Stop rendering. return special image } }
/// <summary> /// Raises the BeforeRenderPage event. /// </summary> /// <param name="g">Drawing surface.</param> /// <param name="page">The pagewhat will be printed.</param> /// <param name="x">Horizontal position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="y">Vertical position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> protected virtual void OnBeforeRenderPage(Graphics g, PdfPage page, ref int x, ref int y, ref int width, ref int height, PageRotate rotation) { if (BeforeRenderPage != null) { var args = new BeforeRenderPageEventArgs(g, page, x, y, width, height, rotation); BeforeRenderPage(this, args); x = args.X; y = args.Y; width = args.Width; height = args.Height; } }
/// <summary> /// Rotates the specified page to the specified angle. /// </summary> /// <param name="pageIndex">Zero-based index of a page for rotation.</param> /// <param name="angle">The angle which must be turned page</param> /// <remarks>The PDF page rotates clockwise. See <see cref="PageRotate"/> for details.</remarks> public void RotatePage(int pageIndex, PageRotate angle) { if (Document == null) return; Document.Pages[pageIndex].Rotation = angle; UpdateDocLayout(); }
private void DrawThumbnail(PdfBitmap bitmap, PdfPage page, Int32Rect pageRect, PageRotate pageRotate, RenderFlags renderFlags) { int w = Math.Max((int)page.Width, pageRect.Width); int h = Math.Max((int)page.Height, pageRect.Height); using (var bmp = new PdfBitmap(w, h, true)) { page.RenderEx(bmp, 0, 0, w, h, pageRotate, renderFlags); using (var g = System.Drawing.Graphics.FromImage(bitmap.Image)) { g.DrawImage(bmp.Image, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height); } } }
/// <summary> /// Process existing pages /// </summary> /// <returns>Null if page still painting, PdfBitmap object if page successfully rendered.</returns> private bool ProcessExisting(PdfBitmap bitmap, PdfPage page, Int32Rect pageRect, PageRotate pageRotate, RenderFlags renderFlags) { switch (this[page].status) { case ProgressiveRenderingStatuses.RenderReader: this[page].status = page.StartProgressiveRender(bitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags, null); if (this[page].status == ProgressiveRenderingStatuses.RenderDone) { return(true); } return(false); //Start rendering. Return nothing. case ProgressiveRenderingStatuses.RenderDone: page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //Stop rendering. Return image. case ProgressiveRenderingStatuses.RenderDone + 2: return(true); //Rendering already stoped. return image case ProgressiveRenderingStatuses.RenderDone + 3: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; page.RenderEx(bitmap, pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, pageRotate, renderFlags); return(true); //Rendering in non progressive mode case ProgressiveRenderingStatuses.RenderDone + 4: this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; DrawThumbnail(bitmap, page, pageRect, pageRotate, renderFlags); return(true); //Rendering thumbnails case ProgressiveRenderingStatuses.RenderTobeContinued: this[page].status = page.ContinueProgressiveRender(); return(false); //Continue rendering. Return nothing. case ProgressiveRenderingStatuses.RenderFailed: default: bitmap.FillRectEx(pageRect.X, pageRect.Y, pageRect.Width, pageRect.Height, Helpers.ToArgb(Colors.Red)); bitmap.FillRectEx(pageRect.X + 5, pageRect.Y + 5, pageRect.Width - 10, pageRect.Height - 10, Helpers.ToArgb(Colors.White)); page.CancelProgressiveRender(); this[page].status = ProgressiveRenderingStatuses.RenderDone + 2; return(true); //An error has occurred. Stop rendering. return special image } }
/// <summary> /// Raises the AfterRenderPage event. /// </summary> /// <param name="g">Drawing surface.</param> /// <param name="page">The pagewhat will be printed.</param> /// <param name="x">Horizontal position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="y">Vertical position of the <paramref name="page"/> on the drawing surface.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> protected virtual void OnAfterRenderPage(Graphics g, PdfPage page, int x, int y, int width, int height, PageRotate rotation) { if (AfterRenderPage != null) { AfterRenderPage(this, new BeforeRenderPageEventArgs(g, page, x, y, width, height, rotation)); } }
/// <summary> /// Raises the BeforeRenderPage event. /// </summary> /// <param name="page">The pagewhat will be printed.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> protected virtual void OnBeforeRenderPage(PdfPage page, double width, double height, PageRotate rotation) { if (BeforeRenderPage != null) { BeforeRenderPage(this, new BeforeRenderPageEventArgs(page, width, height, rotation)); } }
/// <summary> /// Raises the BeforeRenderPage event. /// </summary> /// <param name="page">The pagewhat will be printed.</param> /// <param name="width">The page's width calculated to match the sheet size.</param> /// <param name="height">The page's height calculated to match the sheet size.</param> /// <param name="rotation">The page rotation.</param> protected virtual void OnBeforeRenderPage(PdfPage page, double width, double height, PageRotate rotation) { if (BeforeRenderPage != null) BeforeRenderPage(this, new BeforeRenderPageEventArgs(page, width, height, rotation)); }