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
相关推荐
Mr.Jessy15 小时前
JavaScript高级:构造函数与原型
开发语言·前端·javascript·学习·ecmascript
白兰地空瓶17 小时前
🚀你以为你在写 React?其实你在“搭一套前端操作系统”
前端·react.js
爱上妖精的尾巴18 小时前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
似水流年QC18 小时前
深入探索 WebHID:Web 标准下的硬件交互实现
前端·交互·webhid
陪我去看海18 小时前
测试 mcp
前端
speedoooo19 小时前
在现有App里嵌入一个AI协作者
前端·ui·小程序·前端框架·web app
全栈胖叔叔-瓜州19 小时前
关于llamasharp 大模型多轮对话,模型对话无法终止,或者输出角色标识User:,或者System等角色标识问题。
前端·人工智能
三七吃山漆19 小时前
攻防世界——wife_wife
前端·javascript·web安全·网络安全·ctf
用户479492835691519 小时前
面试官问"try-catch影响性能吗",我用数据打脸
前端·javascript·面试
GISer_Jing20 小时前
前端营销技术实战:数据+AI实战指南
前端·javascript·人工智能