logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
Guest  
#1 Posted : Wednesday, March 29, 2017 7:31:17 AM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 158

Was thanked: 4 time(s) in 4 post(s)
Hi guys !

I would like to zoom with MouseWheel (Delta > 0) or (Delta < 0) at Cursor Mouse position on PdfViewer.
I used Pdfium.NET.SDK 3.7.4.2704 (latest version on February 2017)

How to make this in C# ?

Code:

<Grid Panel.ZIndex="1" Grid.Column="1" Grid.ColumnSpan="2"  Grid.Row="3"  Grid.RowSpan="2" x:Name="ScroolBar" >
    <ScrollViewer x:Name="viewerScroll" Panel.ZIndex="3" CanContentScroll="True"  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Wpf:PdfViewer x:Name="pdfViewer"  MouseWheel="pdfViewer_MouseWheel" MouseMove="pdfViewer_MouseMove"  Background="#AAA095" >
        </Wpf:PdfViewer>
    </ScrollViewer>
</Grid>



Code:

private void pdfViewer_MouseWheel(object sender, MouseWheelEventArgs e)
{
    System.Windows.Point p = e.GetPosition(null);

    //float newzoom = this.pdfViewer.Zoom;
    //this.pdfViewer.PageToClient(this.pdfViewer.CurrentIndex, p);

    if (e.Delta > 0)
    {
        calculZoomPlus();
    }
    else if(e.Delta < 0)
    {
        calculZoomMoins();
    }
}

private void calculZoomPlus()
{
    if (this.pdfViewer.Zoom < 0.125f)
    {
        this.pdfViewer.Zoom = 0.125f;
    }
    else if (this.pdfViewer.Zoom >= 0.125f && this.pdfViewer.Zoom < 4.00f)
    {
        this.pdfViewer.Zoom += 0.125f;
    }
    else if (this.pdfViewer.Zoom == 4.00f)
    {
        this.pdfViewer.Zoom = 4.00f;
    }
}

private void calculZoomMoins()
{
    if (this.pdfViewer.Zoom == 0.25f)
    {
        this.pdfViewer.Zoom = 0.125f;
    }
    else if (this.pdfViewer.Zoom <= 0.125f)
    {
           
    }
    else if (this.pdfViewer.Zoom > 0.125f && this.pdfViewer.Zoom <= 4.00f)
    {
        this.pdfViewer.Zoom -= 0.125f;
    }
}



Sorry for my bad english, I'm french.

Thanks for your help.

Edited by user Wednesday, March 29, 2017 7:42:40 AM(UTC)  | Reason: More explanations

slat  
#2 Posted : Tuesday, April 18, 2017 8:14:27 AM(UTC)
Quote
slat

Rank: Newbie

Groups: Registered
Joined: 12/15/2016(UTC)
Posts: 3
New Zealand

Not sure on the WPF implementation but I have been able to achieve this in Winforms. (Although its a bit sloppy).

Code:

// Cache a copy of the current mouse position on page
var pagePoint = pdfViewer1.ClientToPage(_pdfViewer.CurrentIndex, 
    pdfViewer1.PointToClient(Cursor.Position));

// Set the zoom
pdfViewer1.Zoom = zoom;
pdfViewer1.SizeMode = SizeModes.Zoom;

// Copy page point back to new client relative position
var clientPoint = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, pagePoint);

var size = pdfViewer1.ClientSize;

// Set client point to half the viewer size (To place point in middle of page)
clientPoint.X -= size.Width / 2;
clientPoint.Y -= size.Height / 2;

// Set point back to page again
pagePoint = pdfViewer1.ClientToPage(pdfViewer1.CurrentIndex, clientPoint);

// Scroll the page point into view
pdfViewer1.ScrollToPoint(pdfViewer1.CurrentIndex, pagePoint);


It does jitter a bit, as the layout is recalculated during the zoom and again on applying the scroll to point afterwards.
Seems like some changes may be required here: (Not sure?)
https://github.com/Patagames/Pdf.WinForms/blob/master/PdfViewer.cs#L1439

I also added this code to the top of the OnMouseWheel method: (As the main form uses ctrl key + mouse wheel to initiate scroll)
https://github.com/Patagames/Pdf.WinForms/blob/master/PdfViewer.cs#L1657
Code:

if ((ModifierKeys & Keys.Control) == Keys.Control)
{
    base.OnMouseWheel(e);
    return;
}


Would appreciate any insight into preventing the jittering, ie. delaying layout until the very end.

Edited by user Tuesday, April 18, 2017 8:26:10 AM(UTC)  | Reason: Not specified

Guest  
#3 Posted : Friday, September 29, 2017 7:03:39 PM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 158

Was thanked: 4 time(s) in 4 post(s)
Hello,
I can see that this subject is a bit old (it a ex coworker who started it).

I'm now facing the same problem and there is noting clear about this on the web.
The problem is that I'm not able to make a correct MousseWeel Zoom on my PdfViewer. It goes to the top left side of my window instead of focusing on the cursor.
I glad that someone answered but it doesn't seem to work for me. But I see that you use the method PdfViewer.ScrollToPoint(). Is this the solution of my problem ?

Is there someone here who may have something for me to work with?


Thank you very much.

Edited by user Friday, September 29, 2017 9:50:50 PM(UTC)  | Reason: More precisions

Paul Rayman  
#4 Posted : Monday, October 2, 2017 8:41:32 AM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,018

Thanks: 5 times
Was thanked: 121 time(s) in 118 post(s)
I am assuming that you are looking for a WPF solution and if so, try the following code example

Code:

private void pdfViewer1_MouseWheel(object sender, MouseWheelEventArgs e)
{
	if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
	{
		e.Handled = true;

		int idx = pdfViewer1.CurrentIndex;
		var mousePoint = e.GetPosition(this);
		var leftTopPagePoint = pdfViewer1.ClientToPage(idx, new Point(0, 0));
		var pagePoint = pdfViewer1.ClientToPage(idx, mousePoint);

		if (e.Delta < 0 && pdfViewer1.Zoom > 0.4)
			pdfViewer1.Zoom /= 1.3f;
		else if (e.Delta > 0)
			pdfViewer1.Zoom *= 1.3f;
		pdfViewer1.SizeMode = SizeModes.Zoom;

		var newPagePoint = pdfViewer1.ClientToPage(idx, mousePoint);
		var offsetPoint = newPagePoint - pagePoint;

		var scrollPoint = leftTopPagePoint - offsetPoint;

		// Scroll the page point into view
		pdfViewer1.ScrollToPoint(idx, scrollPoint);
	}

}
thanks 2 users thanked Paul Rayman for this useful post.
fmotsch on 3/6/2018(UTC), azaezaeaze on 7/18/2018(UTC)
Guest  
#5 Posted : Monday, October 9, 2017 3:03:11 AM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 158

Was thanked: 4 time(s) in 4 post(s)
Good morning Paul Rayman,

I've tried your solution and it's way better than the solution we were working on (the zoom doesn't focus on the left side of the screen anymore).
But it's not very accurate. I mean that it doesn't zoom exactly on the cursor position.
Do I have to put the cursor position in the ScrollToPoint function? ( pdfViewer1.ScrollToPoint(idx, "someting that represent the cursor position"); )

Thank you very much !

Edited by user Monday, October 9, 2017 4:16:13 AM(UTC)  | Reason: Not specified

Paul Rayman  
#6 Posted : Sunday, October 15, 2017 7:27:19 AM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,018

Thanks: 5 times
Was thanked: 121 time(s) in 118 post(s)
Hmm... it works fine on my side.
Make sure you are not missing this line.
Code:

e.Handled = true;
Guest  
#7 Posted : Wednesday, November 15, 2017 4:53:23 AM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 158

Was thanked: 4 time(s) in 4 post(s)
Thank you very much Paul, it works perfectly !
Quick Reply Show Quick Reply
Users browsing this topic
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.