Spire.PDF for .NET【页面设置】演示:设置 PDF 的查看器首选项和缩放系数

优化查看器首选项和缩放因子对于改善 PDF 文档的查看体验至关重要。通过使用适当的查看器首选项和缩放因子,您可以使您的 PDF 文档更加用户友好、可查看且适合不同的设备和平台。在本文中,我们将演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中为 PDF 文档设置查看器首选项和缩放因子。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。 可以从此链接下载 DLL 文件,也可以通过NuGet安装。

复制代码
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中设置 PDF 查看器首选项

查看器首选项是可应用于 PDF 文档的设置,用于控制在 PDF 查看器中打开时文档的显示方式。这些首选项会影响查看体验的各个方面,例如初始视图、页面布局和导航选项卡。

要使用 Spire.PDF for .NET 设置 PDF 文档的查看器首选项,您可以按照以下步骤操作:

  • 初始化PdfDocument类的实例。
  • **使用PdfDocument.LoadFromFile()**方法加载 PDF 文档。
  • 获取PdfViewerPreferences对象。
  • 使用PdfViewerPreferences类提供的属性设置文档的查看器首选项。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

【C# 】

复制代码
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();
}
}
}

VB.NET

复制代码
Imports Spire.Pdf

Namespace SetViewerPreference
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Initialize an instance of PdfDocument class
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Example.pdf")

'Get the PdfViewerPreferences object
Dim viewerPreferences As PdfViewerPreferences = 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()
End Sub
End Class
End Namespace
在 C# 和 VB.NET 中设置 PDF 的缩放比例

缩放系数决定了 PDF 文档打开时的缩放级别。默认情况下,大多数 PDF 查看器将缩放系数设置为"适合页面",即缩放文档以适合查看器窗口的宽度。但是,您也可以根据需要设置特定的缩放系数,例如 60%、150% 或 200%。

要使用 Spire.PDF for .NET 设置 PDF 文档的缩放比例,您可以按照以下步骤操作:

  • 初始化PdfDocument类的实例。
  • **使用PdfDocument.LoadFromFile()**方法加载 PDF 文档。
  • **使用PdfDocument.Pages[int index]**属性获取特定页面。
  • 初始化PdfDestination类的实例。
  • 使用PdfDestination.ModePdfDestination.LocationPdfDestination.Zoom属性设置目标模式、位置和缩放比例。
  • 初始化PdfGoToAction 类的实例,并将PdfDestination实例作为参数传递给该类的构造函数。
  • 使用PdfDocument.AfterOpenAction属性设置打开文档时要执行的操作。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

【C# 】

复制代码
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();
}
}
}

VB.NET

复制代码
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.General
Imports System.Drawing

Namespace SetZoomFactor
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Initialize an instance of the PdfDocument class
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Example.pdf")

'Get the first page
Dim page As PdfPageBase = pdf.Pages(0)

'Initialize an instance of the PdfDestination class
Dim dest As PdfDestination = 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
Dim gotoAction As PdfGoToAction = 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()
End Sub
End Class
End Namespace
相关推荐
getapi35 分钟前
在宝塔面板中部署 Vue 项目打包后的 dist 文件作为前端
前端·javascript·vue.js
59678515444 分钟前
css浮动
前端·css·html
我想发发发1 小时前
已经安装了ROS环境却还是报错`ModuleNotFoundError: No module named ‘rclpy‘`
前端·人工智能·chrome·机器人
—Qeyser1 小时前
Flutter 组件通信完全指南
前端·javascript·flutter
天天进步20151 小时前
从脚本到服务:5 分钟通过 Botasaurus 将你的爬虫逻辑转化为 Web API
前端·爬虫
沛沛老爹2 小时前
Web转AI架构篇:Agent Skills vs MCP-混合架构设计模式实战指南
java·前端·人工智能·架构·llm·rag
张张努力变强2 小时前
C++类和对象(一):inline函数、nullptr、类的定义深度解析
开发语言·前端·jvm·数据结构·c++·算法
Elcker2 小时前
JAVA-Web 项目研发中如何保持团队研发风格的统一
java·前端·javascript
selectDele3 小时前
Solid.js和React的比较
前端·javascript·react.js·solid.js
小旋风012343 小时前
前端对接豆包AI(vue2版本)
前端·人工智能