最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET使用C#添加動(dòng)作到PDF文檔

 更新時(shí)間:2024年11月25日 10:43:56   作者:Eiceblue  
使用C#語言在.NET框架下向PDF文檔中添加動(dòng)作,不僅能夠提升文檔的交互性和用戶體驗(yàn),還能夠在自動(dòng)化工作流中發(fā)揮關(guān)鍵作用,下面我們就來看看如何在.NET平臺使用C#在PDF文檔中添加動(dòng)作吧

使用C#語言在.NET框架下向PDF文檔中添加動(dòng)作,不僅能夠提升文檔的交互性和用戶體驗(yàn),還能夠在自動(dòng)化工作流中發(fā)揮關(guān)鍵作用,例如自動(dòng)跳轉(zhuǎn)至特定頁面、鏈接外部資源或播放音頻資源等操作。這種能力使得開發(fā)者能夠根據(jù)具體需求定制PDF文檔的互動(dòng)操作,進(jìn)而提高文檔的實(shí)用性。本文將介紹如何在.NET平臺使用C#在PDF文檔中添加動(dòng)作。

本文所使用的方法需要用到免費(fèi)Free Spire.PDF for .NET,可通過NuGet安裝:PM> Install-Package Spire.PDF

用C#在PDF中添加動(dòng)作的一般步驟

利用C#以及該庫可以向PDF文檔中嵌入多種互動(dòng)組件動(dòng)作,如瀏覽控制按鈕、外部文件和網(wǎng)頁連接以及聲音播放功能,以此來提升用戶的閱讀體驗(yàn)。下面簡要介紹實(shí)現(xiàn)PDF內(nèi)的動(dòng)作添加的主要步驟:

創(chuàng)建PdfDocument類的實(shí)例。

通過PdfDocument.LoadFromFile()方法加載 PDF 文檔。

使用PdfDocument.Pages[]屬性獲取頁面。

創(chuàng)建表示動(dòng)作的類的實(shí)例,并設(shè)置其屬性。

將動(dòng)作添加到PDF文檔:

  • 可以使用動(dòng)作在頁面的矩形區(qū)域內(nèi)創(chuàng)建PdfActionAnnotation類的實(shí)例,并為動(dòng)作添加提示文字(可選)。然后使用PdfPageBase.Annotations.Add()方法將動(dòng)作注釋添加到頁面上,從而創(chuàng)建可點(diǎn)擊觸發(fā)的動(dòng)作。
  • 也可以通過PdfDocument.AfterOpenAction、PdfDocument.BeforeCloseAction等屬性直接將動(dòng)作設(shè)置為在進(jìn)行其他特定操作時(shí)執(zhí)行的動(dòng)作。

使用PdfDocument.SaveToFile()方法保存生成的文檔。

釋放資源。

在PDF中創(chuàng)建文檔內(nèi)跳轉(zhuǎn)動(dòng)作

文檔內(nèi)跳轉(zhuǎn)動(dòng)作的創(chuàng)建通過PdfGoToAction類實(shí)現(xiàn)。代碼示例:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddNavigationButtonPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建 PdfDocument 的實(shí)例
            PdfDocument pdf = new PdfDocument();

            // 加載 PDF 文件
            pdf.LoadFromFile("示例.pdf");

            // 創(chuàng)建 PdfDestination 實(shí)例并設(shè)置目標(biāo)位置
            PdfDestination destination = new PdfDestination(pdf.Pages[1]);
            destination.Location = new PointF(0, 0);
            destination.Mode = PdfDestinationMode.Location;
            destination.Zoom = 0.6f;

            // 基于目標(biāo)位置創(chuàng)建 PdfGoToAction 實(shí)例
            PdfGoToAction action = new PdfGoToAction(destination);

            // 創(chuàng)建矩形并繪制到第一頁
            RectangleF rect = new RectangleF(70, pdf.PageSettings.Size.Height - 120, 140, 20);
            pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // 在矩形中繪制文本
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            pdf.Pages[0].Canvas.DrawString("跳轉(zhuǎn)到第2頁", font, PdfBrushes.Green, rect, stringFormat);

            // 基于矩形和動(dòng)作創(chuàng)建 PdfActionAnnotation 實(shí)例
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // 將動(dòng)作注釋添加到第一頁
            pdf.Pages[0].Annotations.Add(actionAnnotation);

            // 保存文檔
            pdf.SaveToFile("output/PDF導(dǎo)航動(dòng)作.pdf");
            pdf.Close();
        }
    }
}

結(jié)果

在PDF中創(chuàng)建網(wǎng)頁鏈接打開動(dòng)作

網(wǎng)頁鏈接打開動(dòng)作的創(chuàng)建通過PdfUriAction類實(shí)現(xiàn)。代碼示例:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddSoundActionPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建 PdfDocument 的實(shí)例
            PdfDocument pdf = new PdfDocument();

            // 加載 PDF 文件
            pdf.LoadFromFile("示例.pdf");

            // 獲取第一頁
            PdfPageBase page = pdf.Pages[0];

            // 在頁面上繪制矩形
            RectangleF rect = new RectangleF(30, 30, 120, 20);
            page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // 在矩形內(nèi)繪制文本
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("點(diǎn)擊跳轉(zhuǎn)示例網(wǎng)頁", font, PdfBrushes.LightSkyBlue, rect);

            // 創(chuàng)建 PdfUriAction 實(shí)例并設(shè)置其屬性
            PdfUriAction action = new PdfUriAction();
            action.Uri = "https://www.example.com/";

            // 使用網(wǎng)頁鏈接動(dòng)作和矩形創(chuàng)建 PdfActionAnnotation 實(shí)例
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // 將動(dòng)作注釋添加到第一頁
            page.Annotations.Add(actionAnnotation);

            // 保存文檔
            pdf.SaveToFile("output/PDF網(wǎng)頁鏈接打開動(dòng)作.pdf");
            pdf.Close();
        }
    }
}

結(jié)果

在PDF中創(chuàng)建音頻播放動(dòng)作

音頻播放動(dòng)作的創(chuàng)建通過PdfSoundAction類實(shí)現(xiàn)。代碼示例:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using Spire.Pdf.General;
using System.Drawing;

namespace AddSoundActionPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建 PdfDocument 的實(shí)例
            PdfDocument pdf = new PdfDocument();

            // 加載 PDF 文件
            pdf.LoadFromFile("示例.pdf");

            // 獲取第一頁
            PdfPageBase page = pdf.Pages[0];

            // 在頁面上繪制提示圖像
            PdfImage image = PdfImage.FromFile("音頻.png");
            page.Canvas.DrawImage(image, new PointF(30, 30));

            // 創(chuàng)建 PdfSoundAction 實(shí)例并設(shè)置其屬性
            PdfSoundAction action = new PdfSoundAction("背景.wav");
            // 設(shè)置聲音參數(shù)
            action.Sound.Bits = 16;
            action.Sound.Channels = PdfSoundChannels.Stereo;
            action.Sound.Encoding = PdfSoundEncoding.Signed;
            action.Sound.Rate = 44100;
            // 設(shè)置播放選項(xiàng)
            action.Volume = 0;
            action.Repeat = true;
            action.Mix = true;
            action.Synchronous = true;

            // 基于提示圖像的位置創(chuàng)建 PdfActionAnnotation 實(shí)例,用于聲音動(dòng)作
            RectangleF rect = new RectangleF(30, 30, image.Width, image.Height);
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // 將動(dòng)作注釋添加到第一頁
            page.Annotations.Add(actionAnnotation);

            // 設(shè)置在文檔打開后播放聲音動(dòng)作
            pdf.AfterOpenAction = action;

            // 保存文檔
            pdf.SaveToFile("output/PDF音頻播放動(dòng)作.pdf");
            pdf.Close();
        }
    }
}

結(jié)果

在PDF中創(chuàng)建文件打開動(dòng)作

文件打開動(dòng)作的創(chuàng)建通過PdfLaunchAction類實(shí)現(xiàn)。代碼示例:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddFileLaunchActionPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建 PdfDocument 的實(shí)例
            PdfDocument pdf = new PdfDocument();

            // 加載 PDF 文件
            pdf.LoadFromFile("示例.pdf");

            // 獲取第一頁
            PdfPageBase page = pdf.Pages[0];

            // 在頁面上繪制矩形
            RectangleF rect = new RectangleF(50, 50, 180, 20);
            page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // 在矩形內(nèi)繪制文本
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            pdf.Pages[0].Canvas.DrawString("點(diǎn)擊打開示例2", font, PdfBrushes.Green, rect, stringFormat);

            // 創(chuàng)建 PdfLaunchAction 實(shí)例
            PdfLaunchAction action = new PdfLaunchAction("D:/示例2.pdf", PdfFilePathType.Absolute);
            // 設(shè)置啟動(dòng)模式為在新窗口中打開
            action.IsNewWindow = true;

            // 基于矩形和啟動(dòng)動(dòng)作創(chuàng)建 PdfActionAnnotation 實(shí)例
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // 將動(dòng)作注釋添加到第一頁
            page.Annotations.Add(actionAnnotation);

            // 保存文檔
            pdf.SaveToFile("output/PDF文件打開動(dòng)作.pdf");
            pdf.Close();
        }
    }
}

結(jié)果

在PDF中創(chuàng)建JavaScript動(dòng)作

JavaScript動(dòng)作的創(chuàng)建通過PdfJavaScriptAction類實(shí)現(xiàn)。代碼示例:

using Spire.Pdf;
using Spire.Pdf.Actions;

namespace AddJavaScriptActionPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建 PdfDocument 的實(shí)例
            PdfDocument pdf = new PdfDocument();

            // 加載 PDF 文件
            pdf.LoadFromFile("示例.pdf");

            // 定義JavaScript代碼
            string jsCode =
            "app.alert({" +
            "    cMsg: '歡迎閱讀《水星:太陽系中最小的行星之一,卻擁有無盡的科學(xué)奧秘》。\\n\\n本文將詳細(xì)探討水星的各個(gè)方面,包括概述、形成和歷史、表面特征、氣候和環(huán)境,以及未來的探索。', " +
            "    nIcon: 3, " +
            "    cTitle: '文檔介紹'" +
            "});";

            // 使用代碼創(chuàng)建 PdfJavaScriptAction 實(shí)例
            PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode);

            // 將動(dòng)作設(shè)置為PDF文檔打開時(shí)執(zhí)行
            pdf.AfterOpenAction = action;

            // 保存文檔
            pdf.SaveToFile("output/PDF JavaScript動(dòng)作.pdf");
            pdf.Close();
        }
    }
}

結(jié)果

到此這篇關(guān)于.NET使用C#添加動(dòng)作到PDF文檔的文章就介紹到這了,更多相關(guān)C#添加動(dòng)作到PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果

    WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果

    這篇文章主要為大家詳細(xì)介紹了WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Unity常用命令模式詳解

    Unity常用命令模式詳解

    這篇文章主要為大家詳細(xì)介紹了Unity常用命令模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 基于C#實(shí)現(xiàn)亂碼視頻效果

    基于C#實(shí)現(xiàn)亂碼視頻效果

    亂碼視頻效果可能很多人都在抖音看到過,即把一個(gè)短視頻,轉(zhuǎn)成數(shù)字、字母等亂碼組成的形式進(jìn)行播放。本文將用C#實(shí)現(xiàn)一下這一效果,感興趣的可以了解一下
    2023-01-01
  • 在C#中對TCP客戶端的狀態(tài)封裝詳解

    在C#中對TCP客戶端的狀態(tài)封裝詳解

    本篇文章小編為大家介紹,在C#中對TCP客戶端的狀態(tài)封裝詳解,需要的朋友參考下
    2013-04-04
  • 詳解如何在C#中接受或拒絕Excel中的修訂

    詳解如何在C#中接受或拒絕Excel中的修訂

    修訂功能可以跟蹤文檔所有的修改,了解修改的過程,這對于團(tuán)隊(duì)協(xié)同文檔編輯、審閱是非常有用的一個(gè)功能。本文將詳細(xì)為您介紹如何接受或拒絕 Excel 中的修訂,感興趣的可以收藏一下
    2022-12-12
  • C#模板方法模式(Template Method Pattern)實(shí)例教程

    C#模板方法模式(Template Method Pattern)實(shí)例教程

    這篇文章主要介紹了C#模板方法模式(Template Method Pattern),以實(shí)例形式講述了C#抽象類模板方法的用法,具有很高的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • c# 解決IIS寫Excel的權(quán)限問題

    c# 解決IIS寫Excel的權(quán)限問題

    使用以上方法必須對dcom進(jìn)行配置,給用戶使用office的權(quán)限
    2012-10-10
  • C#使用selenium實(shí)現(xiàn)爬蟲

    C#使用selenium實(shí)現(xiàn)爬蟲

    這篇文章介紹了C#使用selenium實(shí)現(xiàn)爬蟲的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#編程自學(xué)之開篇介紹

    C#編程自學(xué)之開篇介紹

    這篇是一篇關(guān)于C#編程自學(xué)的入門文章,學(xué)習(xí)C#的開始,開啟了C#的奇妙之旅,感興趣的小伙伴們可以持續(xù)關(guān)注系列文章。
    2015-10-10
  • Unity接入百度AI實(shí)現(xiàn)貨幣識別

    Unity接入百度AI實(shí)現(xiàn)貨幣識別

    本文主要介紹了在Unity中接入百度AI,從而實(shí)現(xiàn)貨幣識別,可以返回貨幣的名稱、代碼、面值、年份信息等,感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-01-01

最新評論

山东省| 洪雅县| 阜平县| 莎车县| 遵义市| 宾阳县| 垫江县| 龙海市| 天门市| 拉孜县| 泌阳县| 阆中市| 家居| 锦州市| 大同市| 霸州市| 新乡县| 南岸区| 盖州市| 乌恰县| 唐山市| 类乌齐县| 黄大仙区| 蓬安县| 离岛区| 沭阳县| 侯马市| 巢湖市| 平原县| 东光县| 蓬莱市| 荥阳市| 敖汉旗| 吉安县| 乐平市| 深圳市| 伊宁市| 诸城市| 天等县| 新余市| 大庆市|