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
相关推荐
嘻嘻嘻嘻嘻嘻ys12 分钟前
《Spring Boot 3 + GraalVM原生镜像实战:云原生时代的毫秒启动与性能调优》
前端·后端
嘻嘻嘻嘻嘻嘻ys15 分钟前
《Spring Boot 3.0×GraalVM:云原生时代的毫秒级启动实战革命》
前端·后端
嘻嘻嘻嘻嘻嘻ys17 分钟前
《Vue 3.4响应式内核优化与WebAssembly性能突破实战指南》
前端·后端
一天睡25小时32 分钟前
前端性能优化面试回答技巧(一)
前端·面试
樊小肆32 分钟前
Vue3 在线 PDF 编辑 2.0 缩放拖拽、渲染优化功能解析
前端·vue.js·开源
程序员韩立33 分钟前
精通 React Hooks:从核心技巧到自定义实践
前端
WEI_Gaot34 分钟前
React Hooks useRef useId
前端·react.js
一天睡25小时35 分钟前
视频HLS分片与关键帧优化深度解析
前端·javascript
孤鸿玉35 分钟前
[Flutter小试牛刀] 低配版signals,添加多层监听链
android·前端·响应式设计
漫漫漫丶36 分钟前
基于Node+HeadlessBrowser的浏览器自动化方案
前端