Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
Question:Hi, does pdf viewer have the take snapshot functionality as Acrobat? If not, do you have plan to add it? Answer:Hi, There is no such tool but you can easily implement it yourself. The main idea is a render a part of a page with the specified size. Question:But how can I simulate a filled rubber band (clicking e dragging the mouse) into the viewer? Answer: You can create a derived class and override OnMouseDown, OnMouseUp, OnMouseMove and OnPaint methods. Please look at code below. Code:
using Patagames.Pdf.Net.Controls.WinForms;
using System;
using Patagames.Pdf.Net;
using System.Drawing;
using System.Windows.Forms;
using Patagames.Pdf.Enums;
namespace ExamplesWinForms
{
public class MyPdfViewer : PdfViewer
{
PointF _startPt = new PointF(0, 0);
PointF _curPt = new PointF(0, 0);
int _shapshotPage = -1;
bool _isSelectionStarted = false;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
_shapshotPage = PointInPage(e.Location);
if (_shapshotPage >= 0)
{
_startPt = ClientToPage(_shapshotPage, e.Location);
_curPt = _startPt;
_isSelectionStarted = true;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
_isSelectionStarted = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_isSelectionStarted)
{
int pageIdx = PointInPage(e.Location);
if (pageIdx != _shapshotPage)
return;
_curPt = ClientToPage(_shapshotPage, e.Location);
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_shapshotPage >= 0)
{
var pt1 = PageToClient(_shapshotPage, _startPt);
var pt2 = PageToClient(_shapshotPage, _curPt);
int left = Math.Min(pt1.X, pt2.X);
int top = Math.Min(pt1.Y, pt2.Y);
int right = Math.Max(pt1.X, pt2.X);
int bottom = Math.Max(pt1.Y, pt2.Y);
e.Graphics.DrawRectangle(Pens.Red, left, top, right - left, bottom - top);
}
}
private void NormalizeSnapshotPoints(out PointF leftTop, out PointF rightBotton)
{
leftTop = new PointF(
Math.Min(_startPt.X, _curPt.X),
Math.Max(_startPt.Y, _curPt.Y));
rightBotton = new PointF(
Math.Max(_startPt.X, _curPt.X),
Math.Min(_startPt.Y, _curPt.Y));
}
internal Image TakeShapshot(int width)
{
//The rectangle on a page in PDF coordinates which should be snapshoted.
PointF leftTop;
PointF rightBottom;
NormalizeSnapshotPoints(out leftTop, out rightBottom);
//Calculate the scale
double k = width / (rightBottom.X - leftTop.X);
//Calculate image height
int height = (int)((leftTop.Y - rightBottom.Y) * k);
//The current page of loaded document.
var page = CurrentPage;
using (var bmp = new PdfBitmap(width, height, true))
{
//Calculate the offset
int devX, devY;
page.PageToDevice(0, 0, (int)(page.Width * k), (int)(page.Height * k), PageRotate.Normal, leftTop.X, leftTop.Y, out devX, out devY);
//Render part of page into bitmap;
CurrentPage.Render(bmp, -devX, -devY, (int)(page.Width * k), (int)(page.Height * k), PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
return new Bitmap(bmp.Image);
}
}
}
}
Edited by user Saturday, March 23, 2019 5:44:53 AM(UTC)
| Reason: Not specified
|