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

Notification

Icon
Error

Options
Go to last post Go to first unread
spurgeon  
#1 Posted : Tuesday, April 17, 2018 12:47:48 PM(UTC)
spurgeon

Rank: Member

Groups: Registered
Joined: 2/2/2016(UTC)
Posts: 20

How can I get the zoom value when the PdfViewer.SizeMode is not equal to SizeModes.Zoom? When the SizeMode is not Zoom, I would like the user to be able to click a Zoom In or Zoom Out button and then have the PdfViewer zoom in/out from the current zoom value. However, if I switch from FitToWidth to SizeModes.Zoom, for example, the Zoom value does not correspond to the current zoom value, so I do not have a reference point.
Paul Rayman  
#2 Posted : Tuesday, April 17, 2018 4:22:59 PM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Are you using latest version of PdfViewer?
spurgeon  
#3 Posted : Wednesday, April 18, 2018 6:15:30 AM(UTC)
spurgeon

Rank: Member

Groups: Registered
Joined: 2/2/2016(UTC)
Posts: 20

I'm using 3.24.2704. For example:

private void zoomIn()
{
pdfViewer1.SizeMode = SizeModes.Zoom;
pdfViewer1.Zoom /= .8f;
}
private void zoomOut()
{
pdfViewer1.SizeMode = SizeModes.Zoom;
pdfViewer1.Zoom *= .8f;
}

If SizeMode is FitToWidth and you resize the PdfViewer, then call zoomIn() or zoomOut(), the Zoom value is not the current zoom value of the viewer. So, then my question is how to set the zoom value to the correct viewable zoom value when switching between SizeMode.Zoom and other modes.
Paul Rayman  
#4 Posted : Thursday, April 19, 2018 7:28:20 PM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Hi,

WinForms
Code:

private void ZoomOut()
{
    if (pdfViewer1.SizeMode != SizeModes.Zoom)
        SwitchToZoomMode();
    pdfViewer1.Zoom *= 0.8f;
}

private void SwitchToZoomMode()
{
    //  ActualPageWidthInPixels  is  1.0f (Zoom)
    //  CurentPageWidthInPixels  is  ? (Zoom)
    //  ? = CurrentPageWidthInPixels * 1.0f / ActulaPageWidthInPixels;
    int ActualPageWidthInPixels = (int)(pdfViewer1.CurrentPage.Width / 72 * GetDpi());

    var pt1 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(0, 0));
    var pt2 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(pdfViewer1.CurrentPage.Width, 0));
    int CurrentPageWidthInPixels = pt2.X - pt1.X;

    float zoom = (float)CurrentPageWidthInPixels / ActualPageWidthInPixels;
    pdfViewer1.SizeMode = SizeModes.Zoom;
    pdfViewer1.Zoom = zoom;
}

int _dpi = 0;
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern int GetDeviceCaps(IntPtr hDC, int nIndex);

private int GetDpi()
{
    if (_dpi <= 0)
    {
        //Gets DPI
        using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
        {
            IntPtr desktop = g.GetHdc();
            _dpi = GetDeviceCaps(desktop, 88);
            g.ReleaseHdc(desktop);
        }
    }
    return _dpi;
}


WPF
Code:

private void ZoomOut(object sender, RoutedEventArgs e)
{
    if (pdfViewer1.SizeMode != SizeModes.Zoom)
        SwitchToZoomMode();
    pdfViewer1.Zoom *= 0.8f;
}

private void SwitchToZoomMode()
{
    //  ActualPageWidthInWPFPoints  is  1.0f (Zoom)
    //  CurentPageWidthInWPFPoints  is  ? (Zoom)
    //  ? = CurentPageWidthInWPFPoints * 1.0f / ActulaPageWidthInPixels;
    double ActualPageWidthInWPFPoints = (int)(pdfViewer1.CurrentPage.Width / 72 * 96);

    var pt1 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new Point(0, 0));
    var pt2 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new Point(pdfViewer1.CurrentPage.Width, 0));
    double CurentPageWidthInWPFPoint = pt2.X - pt1.X;

    double zoom = CurentPageWidthInWPFPoint / ActualPageWidthInWPFPoints;
    pdfViewer1.SizeMode = SizeModes.Zoom;
    pdfViewer1.Zoom = (float)zoom;
}

Edited by user Friday, April 20, 2018 5:58:47 AM(UTC)  | Reason: Not specified

spurgeon  
#5 Posted : Friday, April 20, 2018 5:45:34 AM(UTC)
spurgeon

Rank: Member

Groups: Registered
Joined: 2/2/2016(UTC)
Posts: 20

Thank you.
spurgeon  
#6 Posted : Monday, July 16, 2018 5:39:52 PM(UTC)
spurgeon

Rank: Member

Groups: Registered
Joined: 2/2/2016(UTC)
Posts: 20

Sorry to come back around to this after such a long time, but I did not find that your solution works. PdfViewer.CurrentPage.Width never changes when SizeMode.FitToWidth is set, so when changing back to SizeMode.Zoom, you cannot get the current zoom with your SwitchToZoomMode() method. You can see this by setting the Resize() event and printing PdfViewer1.CurrentPage.Width. It's always the same result while resizing the PdfViewer control when SizeMode.FitToWidth is set.

Code:
    private void pdfViewer1_Resize(object sender, EventArgs e)
    {
        Console.WriteLine("pdfViewer1_Resize curPageWidth:" + pdfViewer1.CurrentPage.Width);  // this value never changes when SizeMode.FitToWidth is set
    }


Is there a way to get the current zoom level when SizeMode.FitToWidth is set so that I can appropriately zoom in and out from the current viewable zoom level?

Edited by moderator Monday, July 16, 2018 10:03:41 PM(UTC)  | Reason: Not specified

Paul Rayman  
#7 Posted : Monday, July 16, 2018 10:16:16 PM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
pdfViewer1.CurrentPage.Width should never change, because it is the width of the PDF page in points. The page width in points does not depend on the render size of the page.

The following row calculates the width of the page in the pixels, that depend on current DPI. And this value does not depend on the render size of the page too.
Code:
int ActualPageWidthInPixels = (int)(pdfViewer1.CurrentPage.Width / 72 * GetDpi());


For example we can render page which size is 100х100 points to any other size 200x200, 500x500, 20x20 and even 10x10000.
And the following rows are calculate the width of the page that depends on a render size.
Code:
 var pt1 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(0, 0));
 var pt2 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(pdfViewer1.CurrentPage.Width, 0));
 int CurrentPageWidthInPixels = pt2.X - pt1.X;


so the float zoom = (float)CurrentPageWidthInPixels / ActualPageWidthInPixels give us the scale factor.

Edited by user Tuesday, July 17, 2018 4:27:32 AM(UTC)  | Reason: Not specified

spurgeon  
#8 Posted : Tuesday, July 17, 2018 4:25:10 AM(UTC)
spurgeon

Rank: Member

Groups: Registered
Joined: 2/2/2016(UTC)
Posts: 20


Thanks, I see my problem now:

GOOD:
Code:

float newZoom = getCurrentZoom();
pdfViewer1.SizeMode = SizeModes.Zoom;
pdfViewer1.Zoom = newZoom;


BAD:
Code:

pdfViewer1.SizeMode = SizeModes.Zoom;
pdfViewer1.Zoom = getCurrentZoom();


BAD:
Code:

pdfViewer1.Zoom = getCurrentZoom();
pdfViewer1.SizeMode = SizeModes.Zoom;


Code:

private float getCurrentZoom()
{
    int ActualPageWidthInPixels = (int)(pdfViewer1.CurrentPage.Width / 72 * Globals.Dpi);
    var pt1 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(0, 0));
    var pt2 = pdfViewer1.PageToClient(pdfViewer1.CurrentIndex, new PointF(pdfViewer1.CurrentPage.Width, 0));
    int CurrentPageWidthInPixels = pt2.X - pt1.X;
    return (float)CurrentPageWidthInPixels / ActualPageWidthInPixels;
}
Users browsing this topic
Guest (7)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.