PdfiumViewer组件扩展(Pdfium.Net.Free)--PDF预览器框选
项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net.Free
PdfiumViewer:https://github.com/1000374/PdfiumViewer
Pdfium.Net.Free 支持
-
.NETFramework 4.0
-
.NETFramework 4.5
-
.NETStandard 2.0
- .Net8.0
可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~
框选PDF坐标及区域内文字:
关键代码:
//选中区域的坐标转成相对于pdf的坐标
var point1 = this.PointToPdf(new Point(_currRect.X, _currRect.Y)); var point2 = this.PointToPdf(e.Location); int x = (int)point1.Location.X; int y = (int)point2.Location.Y; int x1 = (int)point2.Location.X; int y1 = (int)point1.Location.Y; var txt = this.Document.Pages[Page].GetBoundedText(x, y, x1, y1); _currRect = Rectangle.Empty; if (Math.Abs(x1 - x) * Math.Abs(y1 - y) == 0)//相当与未框选有效区域 return; BoundedTextHandler?.Invoke(Page, x, y, x1, y1, txt, true);
截取pdf指定区域的图片:
关键代码:
//选中区域的坐标转成相对于pdf的坐标
var point1 = this.PointToPdf(new Point(_currRect.X, _currRect.Y)); var point2 = this.PointToPdf(e.Location); var bound = Document.Pages[Page].PageSize; var rect = new Rectangle(); var x = (int)point1.Location.X; var y = (int)Math.Abs(bound.Height - point1.Location.Y); var x1 = (int)point2.Location.X; var y1 = (int)Math.Abs(bound.Height - point2.Location.Y); rect.X = Math.Min(x, x1); rect.Y = Math.Min(y, y1); rect.Width = Math.Abs(x1 - x); rect.Height = Math.Abs(y1 - y); _currRect = Rectangle.Empty; if (Math.Abs(x1 - x) * Math.Abs(y1 - y) == 0)//相当与未框选有效区域 return; var image = Document.Render( Page, (int)Document.Pages[Page].Width, (int)Document.Pages[Page].Height, rect.X, // x of the top/left of clipping rectangle rect.Y, // y of the top/left point of clipping rectangle rect.Width, // width of clipping reactangle rect.Height, // height of clipping reactangle _cutDpiX, _cutDpiY, FpdfRotation.Rotate0, // no rotation RenderFlags.None // no render flags ); BoundedCutHandler?.Invoke(Page, rect, image);
转换出来的效果: