Python在PDF中添加或刪除超鏈接的操作
引言
PDF文件現(xiàn)已成為文檔存儲和分發(fā)的首選格式。然而,PDF文件的靜態(tài)特性有時會限制其交互性。超鏈接是提高PDF文件互動性和用戶體驗的關鍵元素。Python作為一種強大的編程語言,擁有多種庫和工具來處理PDF文件,包括添加、刪除超鏈接。本文將詳細介紹如何使用第三方庫Spire.PDF for Python來進行這些操作。
所需Python庫 - Spire.PDF for Python??梢酝ㄟ^下面的pip 命令直接安裝:
pip install Spire.Pdf
Python 在PDF文檔中添加超鏈接
Spire.PDF for Python支持在PDF中添加不同類型的超鏈接:
- 簡單文字鏈接:直接使用
PdfPageBase.Canvas.DrawString()方法將其繪制到頁面上。 - 超文本鏈接、郵箱鏈接:通過
PdfTextWebLink.DrawTextWebLink()方法繪制到頁面上。 - 文檔鏈接:通過
PdfPageBase.AnnotationsWidget.Add(PdfFileLinkAnnotation)方法添加。
Python 代碼如下:
from spire.pdf.common import *
from spire.pdf import *
# 創(chuàng)建PDF文檔
pdf = PdfDocument()
# 添加頁面
page = pdf.Pages.Add()
# 設置初始X和Y坐標
y = 30.0
x = 10.0
# 創(chuàng)建PDF字體
font = PdfTrueTypeFont("宋體", 14.0, PdfFontStyle.Regular, True)
font1 = PdfTrueTypeFont("宋體", 14.0, PdfFontStyle.Underline, True)
# 添加簡單文本鏈接
label = "簡單鏈接: "
format = PdfStringFormat()
format.MeasureTrailingSpaces = True
page.Canvas.DrawString(label, font, PdfBrushes.get_Black(), 0.0, y, format)
x = font.MeasureString(label, format).Width
url = "https://www.e-iceblue.cn"
page.Canvas.DrawString(url, font1, PdfBrushes.get_Blue(), x, y)
y = y + 28
# 添加超文本鏈接
label = "超文本鏈接:"
page.Canvas.DrawString(label, font, PdfBrushes.get_Black(), 0.0, y, format)
x = font.MeasureString(label, format).Width
webLink = PdfTextWebLink()
webLink.Text = "主頁"
webLink.Url = url
webLink.Font = font1
webLink.Brush = PdfBrushes.get_Blue()
webLink.DrawTextWebLink(page.Canvas, PointF(x, y))
y = y + 28
# 添加郵件鏈接
label = "郵件鏈接: "
page.Canvas.DrawString(label, font, PdfBrushes.get_Black(), 0.0, y, format)
x = font.MeasureString(label, format).Width
link = PdfTextWebLink()
link.Text = "聯(lián)系我們"
link.Url = "mailto:support @e-iceblue.com"
link.Font = font1
link.Brush = PdfBrushes.get_Blue()
link.DrawTextWebLink(page.Canvas, PointF(x, y))
y = y + 28
# 添加文檔鏈接
label = "文檔鏈接: "
page.Canvas.DrawString(label, font, PdfBrushes.get_Black(), 0.0, y, format)
x = font.MeasureString(label, format).Width
text = "點擊打開文件"
location = PointF(x, y)
size = font1.MeasureString(text)
linkBounds = RectangleF(location, size)
fileLink = PdfFileLinkAnnotation(linkBounds, "C:\\Users\\Administrator\\Desktop\\排名.xlsx")
fileLink.Border = PdfAnnotationBorder(0.0)
page.AnnotationsWidget.Add(fileLink)
page.Canvas.DrawString(text, font1, PdfBrushes.get_Blue(), x, y)
# 保存PDF文檔
pdf.SaveToFile("PDF超鏈接.pdf")
pdf.Close()
生成文件:

Python 刪除PDF 文檔中的超鏈接
如果要將PDF文檔中已有的超鏈接一次性全部刪除,可以參考以下步驟:
- 通過
LoadFromFile()方法加載 PDF 文檔。 - 循環(huán)遍歷文檔中的頁面,并通過
PdfPageBase.AnnotationsWidget屬性獲取每個頁面上的注釋。 - 循環(huán)遍歷所有注釋,檢查每個注釋是否為超鏈接。
- 如果是,則使用
PdfAnnotationCollection.Remove()方法將其刪除。 - 使用
PdfDocument.SaveToFile()方法保存文檔。
Python 代碼:
from spire.pdf import *
from spire.pdf.common import *
# 加載PDF文檔
pdf = PdfDocument()
pdf.LoadFromFile("PDF超鏈接.pdf")
# 遍歷文檔中的所有頁面
for j in range(pdf.Pages.Count):
# 獲取每一頁
page = pdf.Pages.get_Item(j)
# 獲取每一頁上的注釋
annotations = page.AnnotationsWidget
# 檢查注釋是否為空
if annotations.Count > 0:
# 遍歷所有注釋
i = annotations.Count - 1
while i >=0:
# 獲取注釋
annotation = annotations.get_Item(i)
# 檢查注釋是否為超鏈接
if isinstance(annotation, PdfTextWebLinkAnnotationWidget):
# 刪除超鏈接
annotations.Remove(annotation)
i -= 1
# 保存PDF文檔
pdf.SaveToFile("刪除PDF超鏈接.pdf")
pdf.Close()
如果僅需刪除PDF某一頁中的指定超鏈接,可以參考 以下代碼:
# 刪除第一頁中的第一個超鏈接 page = pdf.Pages.get_Item(0) page.AnnotationsWidget.RemoveAt(0)
到此這篇關于Python在PDF中添加或刪除超鏈接的操作的文章就介紹到這了,更多相關Python添加或刪除PDF超鏈接內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
13行python代碼實現(xiàn)對微信進行推送消息的示例代碼
本文主要介紹了13行python代碼實現(xiàn)對微信進行推送消息的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報錯
這篇文章主要為大家介紹了Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報錯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

