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
相关推荐
nnlss10 分钟前
nvm 安装node 报错
前端
酷盖机车男15 分钟前
封装轮播图 (因为基于微博小程序,语法可能有些出入,如需使用需改标签)
前端·javascript·小程序·uni-app
世界和平�����39 分钟前
openlayers中一些问题的解决方案
前端·javascript·vue.js
小菜yh1 小时前
后端人需知
java·前端·javascript·vue.js·设计模式
周万宁.FoBJ1 小时前
vue3 实现文本内容超过N行折叠并显示“...展开”组件
开发语言·前端·javascript
Jiaberrr1 小时前
uniapp视频禁止用户推拽进度条并保留进度条显示的解决方法——方案二
前端·javascript·uni-app·音视频
森叶2 小时前
webpack 的打包target讲解 & node环境打包下的文件存储造成不易察觉的坑点
前端·webpack·node.js
新智元2 小时前
陶哲轩全网悬赏「最强大脑」!AI + 人类颠覆数学难题?凡尔赛网友已下场
前端·人工智能
亿牛云爬虫专家2 小时前
Puppeteer的高级用法:如何在Node.js中实现复杂的Web Scraping
前端·javascript·爬虫·node.js·爬虫代理·puppeteer·代理ip
uhan252 小时前
2024年9月26日 linux笔记
linux·服务器·前端