詳解如何使用Python在PDF文檔中創(chuàng)建動作
引言
PDF格式因其跨平臺兼容性和豐富的功能集而成為許多行業(yè)中的首選文件格式。其中,PDF中的動作(Action) 功能尤為突出,它允許開發(fā)者嵌入交互式元素,如鏈接、按鈕或是更復雜的腳本,從而顯著提升文檔的互動性和功P能性。通過使用Python這樣的強大編程語言來創(chuàng)建這些動作,不僅可以自動化文檔處理流程,還能實現(xiàn)各種復雜的文檔操作,極大地豐富了PDF文檔的應用場景,使得PDF文檔不再僅僅是靜態(tài)的內容展示,而是能夠更好地服務于用戶需求和業(yè)務流程。本文將介紹如何使用Python在PDF文檔中創(chuàng)建動作。
本文所使用的方法需要用到Spire.PDF for Python,Python:pip install Spire.PDF。
用Python在PDF中創(chuàng)建導航跳轉動作
庫中的PdfGoToAction類代表導航跳轉動作,可以跳轉到文檔內指定頁面的指定位置,同時可自定義跳轉后的頁面縮放。一下是利用此類在PDF文檔中創(chuàng)建導航跳轉動作的操作步驟:
- 導入所需模塊。
- 創(chuàng)建
PdfDocument類,并使用PdfDocument.LoadFromFile()方法載入PDF文件。 - 使用
PdfDocument.Pages.get_Item()方法獲取一個頁面。 - 創(chuàng)建一個
PdfDestination實例來設置跳轉目標位置以及跳轉后的頁面縮放。 - 用
PdfDestination實例創(chuàng)建一個PdfGoToAction實例。 - 用
PdfGoToAction實例在指定位置創(chuàng)建一個PdfActionAnnotation實例,并使用該類下的方法設置注釋的顏色。 - 使用
PdfPageBase.Annotations.Add()方法將注釋添加到頁面上。 - 使用
PdfPageBase.Canvas.DrawString()方法在注釋位置繪制導航提示文字。 - 使用
PdfDocument.SaveToFile()方法保存文檔。 - 釋放資源。
代碼示例
from spire.pdf import *
# 創(chuàng)建PdfDocument類的一個實例并加載一個PDF文檔
pdf = PdfDocument()
pdf.LoadFromFile("示例.pdf")
# 獲取第二頁
page = pdf.Pages.get_Item(1)
# 創(chuàng)建一個PdfDestination實例并設置其屬性
destination = PdfDestination(pdf.Pages.get_Item(0))
destination.Location = PointF(0.0, 0.0)
destination.Mode = PdfDestinationMode.Location
destination.Zoom = 0.8
# 創(chuàng)建一個PdfGoToAction實例
action = PdfGoToAction(destination)
# 創(chuàng)建一個PdfActionAnnotation實例
rect = RectangleF.FromLTRB(70, pdf.PageSettings.Size.Height - 120, 140, pdf.PageSettings.Size.Height - 100)
annotation = PdfActionAnnotation(rect, action)
annotation.Color = PdfRGBColor(Color.get_Red())
# 將注釋添加到第二頁
page.Annotations.Add(annotation)
# 繪制按鈕的文字
font = PdfTrueTypeFont("HarmonyOS Sans SC", 14.0, PdfFontStyle.Regular, True)
stringFormat = PdfStringFormat(PdfTextAlignment.Center)
page.Canvas.DrawString("轉到第一頁", font, PdfBrushes.get_Red(), rect, stringFormat)
# 保存文檔
pdf.SaveToFile("output/PDF添加導航動作.pdf")
pdf.Close()
結果

用Python在PDF中創(chuàng)建聲音動作
PdfSoundAction類表示一個音頻動作,可以用于嵌入音頻到PDF文檔中,并在用戶執(zhí)行指定操作時播放音頻,如點擊動作注釋時和打開文件時。以下是在PDF文檔中創(chuàng)建聲音動作的操作步驟:
- 導入所需模塊。
- 創(chuàng)建
PdfDocument類,并使用PdfDocument.LoadFromFile()方法載入PDF文件。 - 使用指定音頻文件創(chuàng)建
PdfSoundAction實例。 - 使用
PdfSoundAction類下的屬性設置音頻播放參數(shù)。 - 使用
PdfSoundAction.Sound下的屬性設置音頻參數(shù)。 - 使用
PdfDocument.Pages.get_Item()方法獲取一個頁面。 - 使用
PdfPageBase.Canvas.DrawImage()方法在頁面指定位置繪制音頻播放圖標。 - 用
PdfSoundAction實例創(chuàng)建一個PdfActionAnnotation實例,并使用PdfPageBase.Annotations.Add()方法將其添加到頁面的音頻播放圖標位置。 - 還可以直接使用
PdfDocument.AfterOpenAction屬性直接將聲音動作設置為文檔開啟時執(zhí)行的動作。 - 使用
PdfDocument.SaveToFile()方法保存文檔。 - 釋放資源。
代碼示例
from spire.pdf import *
# 創(chuàng)建PdfDocument實例并加載PDF文件
pdf = PdfDocument()
pdf.LoadFromFile("示例2.pdf")
# 獲取文檔的第一頁
page = pdf.Pages.get_Item(0)
# 使用聲音文件路徑創(chuàng)建PdfSoundAction實例
soundAction = PdfSoundAction("Wave.wav")
# 設置音頻參數(shù)
soundAction.Sound.Bits = 16
soundAction.Sound.Channels = PdfSoundChannels.Stereo
soundAction.Sound.Encoding = PdfSoundEncoding.Signed
soundAction.Sound.Rate = 44100
# 設置播放參數(shù)
soundAction.Volume = 0.5
soundAction.Repeat = True
soundAction.Mix = True
soundAction.Synchronous = False
# 在頁面上繪制一張圖片
image = PdfImage.FromFile("Sound.png")
page.Canvas.DrawImage(image, PointF(30.0, 30.0))
# 使用聲音動作創(chuàng)建PdfActionAnnotation實例
rect = RectangleF.FromLTRB(30.0, 30.0, image.GetBounds().Width + 30.0, image.GetBounds().Height + 30.0)
annotation = PdfActionAnnotation(rect, soundAction)
# 將注釋添加到頁面
page.Annotations.Add(annotation)
# 設置聲音動作為文檔打開后播放
# pdf.AfterOpenAction = soundAction
# 保存文檔
pdf.SaveToFile("output/PDF創(chuàng)建聲音動作.pdf")
pdf.Close()
結果

用Python在PDF中創(chuàng)建JavaScript動作
PdfJavaScriptAction類表示一個JavaScript動作,可在指定情況下在PDF文檔中執(zhí)行JavaScript代碼,如填充表單、重置表單等。以下是在PDF中創(chuàng)建JavaScript動作的操作步驟示例:
- 導入所需模塊。
- 創(chuàng)建
PdfDocument類,并使用PdfDocument.LoadFromFile()方法載入PDF文件。 - 使用
PdfDocument.Pages.get_Item()方法獲取一個頁面。 - 自定義一段JavaScript代碼。
- 用此代碼創(chuàng)建
PdfJavaScriptAction實例。 - 用
PdfJavaScriptAction實例在頁面指定位置創(chuàng)建一個PdfActionAnnotation實例,并設置其顏色。 - 使用
PdfPageBase.Annotations.Add()方法將注釋添加到頁面。 - 使用
PdfDocument.SaveToFile()方法保存文檔。 - 釋放資源。
代碼示例
from spire.pdf import *
# 創(chuàng)建一個PdfDocument實例
pdf = PdfDocument()
# 加載一個PDF文件
pdf.LoadFromFile("示例3.pdf")
# 獲取第一頁
page = pdf.Pages.get_Item(0)
# 指定JavaScript代碼
js = """
var lastName = this.getField("LastName").value;
var firstName = this.getField("FirstName").value;
this.getField("FullName").value = lastName + " " + firstName;
"""
# 創(chuàng)建一個JavaScript動作
jsAction = PdfJavaScriptAction(js)
# 創(chuàng)建一個帶有JavaScript動作的注釋
rect = RectangleF.FromLTRB(380, 160, 460, 180)
annotation = PdfActionAnnotation(rect, jsAction)
annotation.Color = PdfRGBColor(Color.get_Red())
# 將注釋添加到頁面上
page.Annotations.Add(annotation)
# 繪制動作提示文本
font = PdfTrueTypeFont("宋體", 12.0, PdfFontStyle.Regular, True)
stringFormat = PdfStringFormat(PdfTextAlignment.Center)
text = "點擊生成全名"
page.Canvas.DrawString(text, font, PdfBrushes.get_Red(), rect, stringFormat)
# 保存文檔
pdf.SaveToFile("output/PDF創(chuàng)建JavaScript動作.pdf")
pdf.Close()
結果

本文演示了如何使用Python在PDF文檔中創(chuàng)建導航跳轉、聲音及JavaScript動作。
到此這篇關于詳解如何使用Python在PDF文檔中創(chuàng)建動作的文章就介紹到這了,更多相關Python PDF創(chuàng)建動作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
分析解決Python中sqlalchemy數(shù)據(jù)庫連接池QueuePool異常
這篇文章主要來給大家分析sqlalchemy數(shù)據(jù)庫連接池QueuePool的異常,給大家用詳細的圖文方式做出了解決的方案,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09
Pytorch 定義MyDatasets實現(xiàn)多通道分別輸入不同數(shù)據(jù)方式
今天小編就為大家分享一篇Pytorch 定義MyDatasets實現(xiàn)多通道分別輸入不同數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
利用Python實現(xiàn)sqlite3增刪改查的封裝
在一些小的應用中,難免會用到數(shù)據(jù)庫,Sqlite數(shù)據(jù)庫以其小巧輕便,無需安裝,移植性好著稱,下面這篇文章主要給大家介紹了關于利用Python實現(xiàn)sqlite3增刪改查的封裝,需要的朋友可以參考下2021-12-12

