Category / Section
How to set a zoom level to view the page in PDF document using C# and VB.NET
5 mins read
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can set the zoom level to view a page in the PDF document.
Steps to set a zoom level to view the page in PDF document programmatically:
- Create a new C# console application project.
- Install the Syncfusion.pdf.Winforms NuGet package as a reference to your .NET Framework application from the NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports System.Drawing
- Use the following code snippet to set a zoom level to view the page in PDF document.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); // Set the page size document.PageSettings.Size = PdfPageSize.A4; //Add a page to the document PdfPage page = document.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 500)); //Create an instance for named destination PdfDestination destination = new PdfDestination(page, new PointF(0, 500)); destination.Zoom = 2f; //Set the goto action PdfGoToAction gotoAction = new PdfGoToAction(destination); document.Actions.AfterOpen = gotoAction; //Save the document document.Save("Output.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Output.pdf");
VB.NET
'Create a New PDF document Dim document As PdfDocument = New PdfDocument() 'Set the page size document.PageSettings.Size = PdfPageSize.A4 'Add a page to the document Dim page As PdfPage = document.Pages.Add() 'Create PDF graphics for the page Dim graphics As PdfGraphics = page.Graphics 'Set the font Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) 'Draw the text graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 500)) 'Create an instance for named destination Dim destination As PdfDestination = New PdfDestination(page, New PointF(0, 500)) destination.Zoom = 2.0F 'Set the goto action Dim gotoAction As PdfGoToAction = New PdfGoToAction(destination) document.Actions.AfterOpen = gotoAction 'Save the document document.Save("Output.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Output.pdf")
A complete working sample can be downloaded from PDFSample.zip.
Take a moment to peruse the documentation, where you can find other options like supported action type, adding action to form field, and adding action to bookmark with code example.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.