为了账号安全,请及时绑定邮箱和手机立即绑定

C# 设置PDF页面跳转按钮

标签:
C# .NET

概述

在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置按钮跳转到指定页面。两种方法适应不同的程序设计需要,可自行选择合适的添加方法。

使用工具

代码示例中使用了类库Spire.PDF for .NET 。在使用该类库时,须在项目程序中引用Spire.Pdf.dll(如下图),dll可在Bin文件中获取,同时注意在程序中添加相应的using指令。

https://img1.sycdn.imooc.com//5d0732510001828c02680590.jpg


代码操作示例(供参考)

【示例 1】跳转至特定页(首页、下一页、上一页、最后一页)

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing; 
namespace ButtonToAppointedPage_PDF
{  
  class Program   
    {       
        static void Main(string[] args)      
          {           
           //实例化PdfDocument类,加载PDF测试F文档           
            PdfDocument doc = new PdfDocument();           
            doc.LoadFromFile("sample.pdf");             
            //允许添加Form            
            doc.AllowCreateForm = true;             
            //获取文档最后一页            
            PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];             
            //在页面指定位置添加指定大小的按钮            
            PdfButtonField button = new PdfButtonField(lastPage, "Click To Back ");            
            button.Bounds = new RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 400, 60, 20);             
            
            //设置按钮边框颜色            
            button.BorderStyle = PdfBorderStyle.Solid;            
            button.BorderColor = new PdfRGBColor(Color.White);             
            //设置按钮背景色            
            button.BackColor = Color.Azure;             
            //设置按钮提示语                  
            button.ToolTip = "To the first page";             
            
            //设置按钮文字字体和颜色                 
            PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Avant Garde", 9f), true);            
            button.Font = trueTypeFont;            
            button.ForeColor = Color.Black;             
            //创建PdfNamedAction实例,在传入的参数中选择上一页、下一页、首页或最后一页            
            PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);             
            
            //应用动作            
            button.Actions.MouseDown = namedAction;             
            //添加按钮到文档            
            doc.Form.Fields.Add(button);             
            
            //保存并打开PDF文档            
            doc.SaveToFile("result.pdf", FileFormat.PDF);            
            System.Diagnostics.Process.Start("result.pdf");         
            }    
      }
 }

PS:这里的PdfNameAction类支持四种按钮跳转动作

https://img1.sycdn.imooc.com//5d0734ff0001a54608280150.jpg

按钮添加效果(截图):

https://img1.sycdn.imooc.com//5d0735a40001496911280563.jpg

点击文中的按钮时,即可跳转至按钮指向的页面。

 

2.跳转至指定页面

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing; 
namespace Buttom2
{  
  class Program  
    {      
      static void Main(string[] args)      
        {            
        //实例化PdfDocument类,加载PDF文档            
        PdfDocument doc = new PdfDocument();            
        doc.LoadFromFile("sample.pdf");             
        
        //允许添加Form            
        doc.AllowCreateForm = true;             
        //获取最后一页            
        PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];             
        //在页面指定位置添加按钮            
        PdfButtonField button = new PdfButtonField(lastPage, "Back");            
        button.Bounds = new RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 700, 50, 20);             
        //设置按钮边框颜色            
        button.BorderStyle = PdfBorderStyle.Solid;            
        button.BorderColor = new PdfRGBColor(Color.Transparent);             
        
        //设置按钮背景色            
        button.BackColor = Color.WhiteSmoke;             
        //设置按钮提示语                  
        button.ToolTip = "Click and back to the third page";             
        
        //设置按钮文字字体和颜色                 
        PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Avant Garde", 9f), true);            
        button.Font = trueTypeFont;            
        button.ForeColor = Color.Black;             
        //实例化PdfDestination对象,传入指定页码到第3页            
        PdfDestination destination = new PdfDestination(doc.Pages[2]);
                     
        //创建go to动作            
        PdfGoToAction goToAction = new PdfGoToAction(destination);
        //应用动作            
        button.Actions.MouseDown = goToAction;             
        
        //添加按钮到文档            
        doc.Form.Fields.Add(button);             
        
        //保存并打开PDF文档            
        doc.SaveToFile("result.pdf", FileFormat.PDF);            
        System.Diagnostics.Process.Start("result.pdf");       
       }   
    }
}

添加效果(截图):

https://img1.sycdn.imooc.com//5d0736eb0001172e11240438.jpg

点击按钮,即可跳转至指定的文档第3页。

(本文完)

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
9
获赞与收藏
48

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消