C#/VB.NET: Set Viewer Preferences and Zoom Factors for PDFs

Optimizing the viewer preferences and zoom factors is crucial for improving the viewing experience of PDF documents. By using the appropriate viewer preferences and zoom factors, you can make your PDF documents more user-friendly, viewable and suitable for different devices and platforms. In this article, we will demonstrate how to set viewer preferences and zoom factors for PDF documents in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Set Viewer Preferences for PDF in C# and VB.NET

Viewer preferences are settings that can be applied to a PDF document to control how it is displayed when it is opened in a PDF viewer. These preferences can affect various aspects of the viewing experience, such as the initial view, page layout, and navigation tabs.

To set the viewer preference for a PDF document using Spire.PDF for .NET, you can follow these steps:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the PdfViewerPreferences object.
  • Set viewer preference for the document using the properties provided by the PdfViewerPreferences class.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace SetViewerPreference
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of PdfDocument class
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile(@"Example.pdf");

            //Get the PdfViewerPreferences object
            PdfViewerPreferences viewerPreferences = pdf.ViewerPreferences;

            //Set viewer preference           
            viewerPreferences.FitWindow = false;
            viewerPreferences.HideMenubar = true;
            viewerPreferences.HideToolbar = true;
            viewerPreferences.CenterWindow= true;
            viewerPreferences.DisplayTitle = false;
            viewerPreferences.PageLayout = PdfPageLayout.SinglePage;
            viewerPreferences.PageMode = PdfPageMode.UseNone;

            //Save the result document
            pdf.SaveToFile("SetViewerPreference.pdf");
            pdf.Close();
        }
    }
}

C#/VB.NET: Set Viewer Preferences and Zoom Factors for PDFs

Set Zoom Factors for PDF in C# and VB.NET

The zoom factor determines the zoom level of the PDF document when it is opened. By default, most PDF viewers set the zoom factor to "Fit Page," which scales the document to fit the width of the viewer window. However, you can also set a specific zoom factor, such as 60%, 150% or 200%, depending on your needs.

To set the zoom factor for a PDF document using Spire.PDF for .NET, you can follow these steps:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a specific page using PdfDocument.Pages[int index] property.
  • Initialize an instance of the PdfDestination class.
  • Set the destination mode, location and zoom factor using PdfDestination.Mode and PdfDestination.Location and PdfDestination.Zoom properties.
  • Initialize an instance of the PdfGoToAction class and pass the PdfDestination instance to the constructor of the class as a parameter.
  • Set the action to be executed when the document is opened using PdfDocument.AfterOpenAction property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using System.Drawing;

namespace SetZoomFactor
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the PdfDocument class
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile(@"Example.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Initialize an instance of the PdfDestination class
            PdfDestination dest = new PdfDestination(page);
            //Set the destination mode
            dest.Mode = PdfDestinationMode.Location;
            //Set the destination location
            dest.Location = new PointF(40f, 40f);
            //Set the zoom factor
            dest.Zoom = 1.5f;

            //Initialize an instance of the PdfGoToAction class
            PdfGoToAction gotoAction = new PdfGoToAction(dest);
            //Set the action to be executed when the document is opened
            pdf.AfterOpenAction = gotoAction;

            //Save the result document
            pdf.SaveToFile("SetZoomFactor.pdf");
            pdf.Close();
        }
    }
}

C#/VB.NET: Set Viewer Preferences and Zoom Factors for PDFs

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.