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

Python實(shí)現(xiàn)在PDF添加各種類型的注釋和標(biāo)注

 更新時(shí)間:2026年04月03日 08:19:59   作者:用戶835629078051  
在?PDF?文檔處理場(chǎng)景中,添加注釋是一項(xiàng)實(shí)用且常見的需求,本文將詳細(xì)介紹如何使用?Python?在?PDF?文檔中添加各種類型的注釋和標(biāo)注,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下

在 PDF 文檔處理場(chǎng)景中,添加注釋是一項(xiàng)實(shí)用且常見的需求。無論是文檔審閱、協(xié)作編輯還是內(nèi)容標(biāo)記,注釋功能都能幫助我們?cè)诓恍薷脑牡那闆r下添加說明、高亮重點(diǎn)或插入批注。本文將詳細(xì)介紹如何使用 Python 在 PDF 文檔中添加各種類型的注釋和標(biāo)注。

為什么需要為 PDF 添加注釋

手動(dòng)在 PDF 中添加注釋不僅效率低下,而且難以保持一致的格式。通過編程方式實(shí)現(xiàn)這一功能具有以下優(yōu)勢(shì):

  • 批量處理 - 一次性為多個(gè)文檔添加相同格式的注釋
  • 精確控制 - 像素級(jí)定位注釋位置,確保與文檔內(nèi)容完美對(duì)應(yīng)
  • 類型豐富 - 程序化創(chuàng)建高亮、下劃線、自由文本、圖章等多種注釋
  • 樣式一致性 - 統(tǒng)一設(shè)置顏色、透明度、邊框等屬性,保持專業(yè)外觀

環(huán)境準(zhǔn)備

首先需要安裝支持 PDF 操作的 Python 庫。本文使用 Spire.PDF for Python,它提供了完整的 PDF 注釋 API。

pip install Spire.PDF

該庫支持多種注釋類型,包括文本標(biāo)記、自由文本、圖章、線條、附件等,能夠滿足各種文檔標(biāo)注需求。

核心注釋類型

Spire.PDF 支持豐富的注釋類型,每種類型都有其特定的應(yīng)用場(chǎng)景:

1. 自由文本注釋(Free Text Annotation)

自由文本注釋允許在 PDF 頁面上添加可編輯的文本框,適用于添加詳細(xì)說明或批注。

from spire.pdf import *

# 創(chuàng)建 PDF 文檔
doc = PdfDocument()
page = doc.Pages.Add()

# 定義注釋區(qū)域
rect = RectangleF(100.0, 200.0, 200.0, 50.0)

# 創(chuàng)建自由文本注釋
textAnnotation = PdfFreeTextAnnotation(rect)
textAnnotation.Text = "\n這是一條自由文本注釋"

# 設(shè)置字體和邊框
font = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Regular, True)
textAnnotation.Font = font

# 設(shè)置邊框樣式
border = PdfAnnotationBorder(1.5)
textAnnotation.Border = border
textAnnotation.BorderColor = PdfRGBColor(Color.get_Blue())

# 設(shè)置線條末端樣式
textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle

# 設(shè)置背景顏色和透明度
textAnnotation.Color = PdfRGBColor(Color.get_LightBlue())
textAnnotation.Opacity = 0.8

# 添加到頁面
page.AnnotationsWidget.Add(textAnnotation)

# 保存文檔
doc.SaveToFile("FreeTextAnnotation.pdf")
doc.Close()

關(guān)鍵 API 說明:

  • PdfFreeTextAnnotation - 創(chuàng)建可編輯文本注釋,需要指定矩形區(qū)域
  • PdfAnnotationBorder - 定義邊框?qū)挾?,使注釋外觀更加清晰
  • PdfLineEndingStyle - 控制注釋邊框的末端樣式(圓形、箭頭、方形等)
  • Opacity - 設(shè)置透明度(0.0-1.0),0.0 完全透明,1.0 完全不透明

2. 文本標(biāo)記注釋(Text Markup Annotation)

文本標(biāo)記注釋用于高亮、下劃線或刪除線標(biāo)記特定文本,是文檔審閱中最常用的功能。

from spire.pdf import *

# 加載現(xiàn)有 PDF 文檔
doc = PdfDocument()
doc.LoadFromFile("template.pdf")
page = doc.Pages[0]

# 獲取要標(biāo)記的文本位置
font = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Regular, True)
format = PdfStringFormat()

# 繪制提示文本
prompt = "需要高亮的文本:"
page.Canvas.DrawString(prompt, font, PdfBrushes.get_DodgerBlue(), 0.0, 50.0)

# 計(jì)算目標(biāo)文本位置
x = font.MeasureString(prompt, format).Width
label = "這是一個(gè)重要段落"
page.Canvas.DrawString(label, font, PdfBrushes.get_OrangeRed(), x, 50.0)

# 創(chuàng)建高亮注釋
incorrectWordLocation = PointF(x, 50.0)
markupText = "重要內(nèi)容提醒"
annotation = PdfTextMarkupAnnotation(
    markupText, 
    "重要段落",
    RectangleF(x, 50.0, 100.0, 20.0), 
    font
)

# 設(shè)置高亮類型為高亮顯示
annotation.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight

# 設(shè)置高亮顏色
annotation.TextMarkupColor = PdfRGBColor(Color.get_Yellow())

# 添加到頁面
page.AnnotationsWidget.Add(annotation)

# 保存文檔
doc.SaveToFile("TextMarkupAnnotation.pdf")
doc.Close()

支持的標(biāo)記類型:

  • Highlight - 高亮顯示,類似熒光筆效果
  • Underline - 添加下劃線
  • StrikeOut - 添加刪除線
  • Squiggly - 添加波浪線

3. 線條注釋(Line Annotation)

線條注釋用于在文檔中繪制帶箭頭的直線,適用于指示、連接或測(cè)量。

from spire.pdf import *

doc = PdfDocument()
page = doc.Pages.Add()

# 定義線條的起點(diǎn)和終點(diǎn)坐標(biāo) [x1, y1, x2, y2]
linePoints = [100, 400, 300, 400]

# 創(chuàng)建線條注釋
lineAnnotation = PdfLineAnnotation(linePoints, "尺寸標(biāo)注")

# 設(shè)置線條意圖(影響閱讀器的顯示方式)
lineAnnotation.LineIntent = PdfLineIntent.LineDimension

# 設(shè)置起點(diǎn)和終點(diǎn)的樣式
lineAnnotation.BeginLineStyle = PdfLineEndingStyle.ClosedArrow
lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond

# 設(shè)置線條顏色和背景色
lineAnnotation.InnerLineColor = PdfRGBColor(Color.get_Green())
lineAnnotation.BackColor = PdfRGBColor(Color.get_Green())

# 設(shè)置邊框樣式
lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid
lineAnnotation.lineBorder.BorderWidth = 2

# 添加到頁面
page.AnnotationsWidget.Add(lineAnnotation)

# 保存文檔
doc.SaveToFile("LineAnnotation.pdf")
doc.Close()

線條樣式選項(xiàng):

  • PdfLineEndingStyle.None - 無線端裝飾
  • PdfLineEndingStyle.Butt - 平頭端點(diǎn)
  • PdfLineEndingStyle.ClosedArrow - 閉合箭頭
  • PdfLineEndingStyle.OpenArrow - 開放箭頭
  • PdfLineEndingStyle.Diamond - 菱形端點(diǎn)
  • PdfLineEndingStyle.Circle - 圓形端點(diǎn)

4. 圖章注釋(Rubber Stamp Annotation)

圖章注釋用于添加預(yù)定義或自定義的狀態(tài)標(biāo)記,如"已批準(zhǔn)"、"機(jī)密"、"草稿"等。

from spire.pdf import *
from datetime import datetime

# 加載 PDF 文檔
document = PdfDocument()
document.LoadFromFile("document.pdf")
page = document.Pages[0]

# 創(chuàng)建自定義圖章模板
template = PdfTemplate(150.0, 60.0)

# 設(shè)置字體和顏色
font1 = PdfTrueTypeFont("微軟雅黑", 14.0, PdfFontStyle.Bold, True)
brush = PdfSolidBrush(PdfRGBColor(Color.get_Red()))

# 繪制圓角矩形邊框
rectangle = RectangleF(PointF(5.0, 5.0), SizeF(140.0, 50.0))
path = PdfPath()
path.AddArc(rectangle.X, rectangle.Y, 10.0, 10.0, 180.0, 90.0)
path.AddArc(rectangle.X + rectangle.Width - 10.0, rectangle.Y, 10.0, 10.0, 270.0, 90.0)
path.AddArc(rectangle.X + rectangle.Width - 10.0, rectangle.Y + rectangle.Height - 10.0, 
            10.0, 10.0, 0.0, 90.0)
path.AddArc(rectangle.X, rectangle.Y + rectangle.Height - 10.0, 10.0, 10.0, 90.0, 90.0)
path.CloseFigure()
template.Graphics.DrawPath(PdfPen(brush, 2.0), path)

# 繪制圖章文本
stampText = "? 已批準(zhǔn)\n" + datetime.now().strftime("%Y-%m-%d")
template.Graphics.DrawString(stampText, font1, brush, PointF(10.0, 15.0))

# 創(chuàng)建圖章注釋
stamp = PdfRubberStampAnnotation(RectangleF(PointF(400.0, 50.0), SizeF(150.0, 60.0)))
appearance = PdfAppearance(stamp)
appearance.Normal = template
stamp.Appearance = appearance

# 設(shè)置元數(shù)據(jù)
stamp.Author = "審核人"
stamp.Subject = "文檔審批"
stamp.ModifiedDate = datetime.now()

# 添加到頁面
page.AnnotationsWidget.Add(stamp)

# 保存文檔
document.SaveToFile("StampAnnotation.pdf")
document.Close()

預(yù)定義圖章圖標(biāo):

如果不想自定義圖章,可以使用內(nèi)置圖標(biāo):

# 使用預(yù)定義的"草稿"圖章
stamp = PdfRubberStampAnnotation(
    RectangleF(100.0, 100.0, 100.0, 40.0), 
    "草稿狀態(tài)"
)
stamp.Icon = PdfRubberStampAnnotationIcon.Draft
stamp.Color = PdfRGBColor(Color.get_Plum())
page.AnnotationsWidget.Add(stamp)

常用預(yù)定義圖標(biāo)包括:

  • Draft - 草稿
  • Approved - 已批準(zhǔn)
  • Confidential - 機(jī)密
  • Final - 最終版
  • Experimental - 實(shí)驗(yàn)性

5. 彈出注釋(Popup Annotation)

彈出注釋通常與其他注釋配合使用,鼠標(biāo)懸停時(shí)顯示詳細(xì)信息。

from spire.pdf import *

doc = PdfDocument()
page = doc.Pages.Add()

# 先創(chuàng)建一個(gè)文本標(biāo)記
font = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Regular, True)
page.Canvas.DrawString("將鼠標(biāo)懸停在注釋上查看備注", font, 
                       PdfBrushes.get_Black(), 50.0, 100.0)

# 創(chuàng)建彈出注釋
popupRect = RectangleF(PointF(100.0, 150.0), SizeF.Empty())
popupAnnotation = PdfPopupAnnotation(popupRect, "這是詳細(xì)的批注信息\n可以包含多行文本")

# 設(shè)置圖標(biāo)樣式
popupAnnotation.Icon = PdfPopupIcon.Paragraph

# 設(shè)置是否默認(rèn)展開
popupAnnotation.Open = True

# 設(shè)置顏色
popupAnnotation.Color = PdfRGBColor(Color.get_Yellow())

# 添加到頁面
page.AnnotationsWidget.Add(popupAnnotation)

# 保存文檔
doc.SaveToFile("PopupAnnotation.pdf")
doc.Close()

6. 多邊形和多段線注釋(Polygon & Polyline Annotation)

多邊形和多段線注釋用于創(chuàng)建自定義形狀的標(biāo)注區(qū)域。

from spire.pdf import *
from datetime import datetime

doc = PdfDocument()
page = doc.Pages.Add()

# 創(chuàng)建多邊形注釋(封閉形狀)
polygon = PdfPolygonAnnotation(
    page, 
    [PointF(100.0, 200.0), PointF(150.0, 180.0), PointF(200.0, 200.0), 
     PointF(180.0, 250.0), PointF(120.0, 250.0)]
)

# 設(shè)置多邊形屬性
polygon.Color = PdfRGBColor(Color.get_PaleVioletRed())
polygon.Text = "重點(diǎn)關(guān)注區(qū)域"
polygon.Author = "審稿人"
polygon.Subject = "區(qū)域標(biāo)記"
polygon.BorderEffect = PdfBorderEffect.BigCloud  # 云朵邊框效果
polygon.ModifiedDate = datetime.now()

# 添加到頁面
page.AnnotationsWidget.Add(polygon)

# 保存文檔
doc.SaveToFile("PolygonAnnotation.pdf")
doc.Close()

綜合示例:創(chuàng)建帶多種注釋的審閱文檔

在實(shí)際應(yīng)用中,通常會(huì)組合使用多種注釋類型。以下示例演示如何在一個(gè)文檔中添加多種注釋:

from spire.pdf import *
from datetime import datetime

# 創(chuàng)建文檔
doc = PdfDocument()
margin = PdfMargins()
margin.Top = 50
margin.Bottom = 50
margin.Left = 50
margin.Right = 50
page = doc.Pages.Add(PdfPageSize.A4(), margin)

# 添加標(biāo)題
titleFont = PdfTrueTypeFont("微軟雅黑", 16.0, PdfFontStyle.Bold, True)
page.Canvas.DrawString("文檔審閱示例", titleFont, 
                       PdfBrushes.get_Black(), 0.0, 30.0)

# 添加正文
contentFont = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Regular, True)
content = "這是一段需要審閱的文本內(nèi)容。其中某些部分需要高亮顯示,某些部分需要添加批注。"
page.Canvas.DrawString(content, contentFont, 
                       PdfBrushes.get_Black(), 0.0, 80.0)

# 1. 添加自由文本注釋
freeTextRect = RectangleF(50.0, 150.0, 250.0, 60.0)
freeText = PdfFreeTextAnnotation(freeTextRect)
freeText.Text = "\n審閱意見:\n此處表述需要更加清晰明確"
freeText.Font = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Regular, True)
freeText.Color = PdfRGBColor(Color.get_LightYellow())
freeText.Opacity = 0.9
page.AnnotationsWidget.Add(freeText)

# 2. 添加圖章注釋
stampTemplate = PdfTemplate(120.0, 50.0, True)
stampFont = PdfTrueTypeFont("微軟雅黑", 12.0, PdfFontStyle.Bold, True)
stampBrush = PdfSolidBrush(PdfRGBColor(Color.get_Red()))
stampTemplate.Graphics.DrawString("待修改", stampFont, stampBrush, PointF(10.0, 15.0))

stamp = PdfRubberStampAnnotation(RectangleF(PointF(350.0, 150.0), SizeF(120.0, 50.0)))
stampAppearance = PdfAppearance(stamp)
stampAppearance.Normal = stampTemplate
page.AnnotationsWidget.Add(stamp)

# 3. 添加線條注釋指向特定位置
linePoints = [320, 200, 350, 180]
lineAnnot = PdfLineAnnotation(linePoints, "參考此處")
lineAnnot.BeginLineStyle = PdfLineEndingStyle.Butt
lineAnnot.EndLineStyle = PdfLineEndingStyle.ClosedArrow
lineAnnot.InnerLineColor = PdfRGBColor(Color.get_Blue())
page.AnnotationsWidget.Add(lineAnnot)

# 保存文檔
doc.SaveToFile("ComprehensiveReview.pdf")
doc.Close()

生成結(jié)果預(yù)覽:

實(shí)用技巧

設(shè)置注釋的作者和時(shí)間戳

annotation = PdfFreeTextAnnotation(rect)
annotation.Author = "張三"  # 設(shè)置作者
annotation.CreationDate = datetime.now()  # 創(chuàng)建時(shí)間
annotation.ModifiedDate = datetime.now()  # 修改時(shí)間

控制注釋的顯示層級(jí)

# 設(shè)置注釋標(biāo)志
annotation.Flags = PdfAnnotationFlags.Locked  # 鎖定注釋,防止刪除
# 或使用其他標(biāo)志
# PdfAnnotationFlags.Hidden - 隱藏注釋
# PdfAnnotationFlags.NoView - 不顯示在視圖中
# PdfAnnotationFlags.ReadOnly - 只讀模式

提取頁面中的所有注釋

# 加載文檔并獲取注釋集合
doc = PdfDocument()
doc.LoadFromFile("annotated.pdf")
annotations = doc.Pages[0].AnnotationsWidget

# 遍歷所有注釋
if annotations.Count > 0:
    for i in range(annotations.Count):
        annotation = annotations.get_Item(i)
        
        # 根據(jù)類型處理不同注釋
        if isinstance(annotation, PdfFreeTextAnnotationWidget):
            print(f"自由文本:{annotation.Text}")
        elif isinstance(annotation, PdfTextMarkupAnnotationWidget):
            print(f"高亮文本:{annotation.Text}")
        
        print(f"作者:{annotation.Author}")
        print(f"修改日期:{annotation.ModifiedDate}")

總結(jié)

本文介紹了使用 Python 在 PDF 文檔中添加各種類型注釋的方法。通過 Spire.PDF 提供的豐富 API,開發(fā)者可以:

  • 創(chuàng)建自由文本、高亮、線條、圖章等多種注釋
  • 精確控制注釋的位置、顏色、透明度等屬性
  • 批量處理文檔審閱和標(biāo)注任務(wù)
  • 提取和管理現(xiàn)有注釋信息

這些功能使得 PDF 注釋自動(dòng)化成為可能,大大提高了文檔協(xié)作和審閱的效率。在實(shí)際項(xiàng)目中,可以根據(jù)具體需求組合使用不同的注釋類型,創(chuàng)建專業(yè)的交互式 PDF 文檔。

以上就是Python實(shí)現(xiàn)在PDF添加各種類型的注釋和標(biāo)注的詳細(xì)內(nèi)容,更多關(guān)于Python PDF添加注釋的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python yolo混合文件xml和img整理/回顯yolo標(biāo)注文件方式

    python yolo混合文件xml和img整理/回顯yolo標(biāo)注文件方式

    本文介紹了如何根據(jù)XML文件中的標(biāo)簽順序進(jìn)行索引轉(zhuǎn)換,并提供了一種自定義標(biāo)簽轉(zhuǎn)格式的方法,以回顯YOLO標(biāo)注文件
    2026-01-01
  • Python異常處理之常見異常類型絕佳實(shí)踐詳解

    Python異常處理之常見異常類型絕佳實(shí)踐詳解

    這篇文章主要為大家介紹了Python異常處理之常見異常類型絕佳實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Python3安裝Pillow與PIL的方法

    Python3安裝Pillow與PIL的方法

    今天小編就為大家分享一篇關(guān)于Python3安裝Pillow與PIL的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • python采用requests庫模擬登錄和抓取數(shù)據(jù)的簡(jiǎn)單示例

    python采用requests庫模擬登錄和抓取數(shù)據(jù)的簡(jiǎn)單示例

    這篇文章主要介紹了python采用requests庫模擬登錄和抓取數(shù)據(jù)的簡(jiǎn)單示例,代碼簡(jiǎn)單卻功能強(qiáng)大!需要的朋友可以參考下
    2014-07-07
  • 一文詳細(xì)聊一聊Python中的下劃線“_”們

    一文詳細(xì)聊一聊Python中的下劃線“_”們

    Python中的下劃線(_)有多種用途,包括特殊方法、內(nèi)部變量、避免關(guān)鍵字沖突、名稱修飾、臨時(shí)變量和忽略變量等,這篇文章主要介紹了Python中的一些下劃線“_”們,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • Python列表切片操作之截取指定范圍的元素

    Python列表切片操作之截取指定范圍的元素

    在Python編程中,列表(List)是最常用的數(shù)據(jù)結(jié)構(gòu)之一,而切片操作(Slicing)作為列表的核心特性,能夠以簡(jiǎn)潔高效的方式截取、修改或復(fù)制列表中的元素,本文將深入探討列表切片的方方面面,從基礎(chǔ)語法到高級(jí)技巧,需要的朋友可以參考下
    2026-05-05
  • Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)(面向?qū)ο蟀?

    Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)(面向?qū)ο蟀?

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)面向?qū)ο蟀娴膶W(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Python面向?qū)ο笾惡蛯?duì)象屬性的增刪改查操作示例

    Python面向?qū)ο笾惡蛯?duì)象屬性的增刪改查操作示例

    這篇文章主要介紹了Python面向?qū)ο笾惡蛯?duì)象屬性的增刪改查操作,結(jié)合實(shí)例形式分析了Python面向?qū)ο笙嚓P(guān)的類與對(duì)象屬性常見操作技巧,需要的朋友可以參考下
    2018-12-12
  • 一文詳解NumPy數(shù)組迭代與合并

    一文詳解NumPy數(shù)組迭代與合并

    NumPy?數(shù)組迭代是訪問和處理數(shù)組元素的重要方法,它允許您逐個(gè)或成組地遍歷數(shù)組元素,NumPy?提供了多種函數(shù)來合并數(shù)組,用于將多個(gè)數(shù)組的內(nèi)容連接成一個(gè)新數(shù)組,本文給大家詳細(xì)介紹了NumPy數(shù)組迭代與合并,需要的朋友可以參考下
    2024-05-05
  • pyx文件 生成pyd 文件用于 cython調(diào)用的實(shí)現(xiàn)

    pyx文件 生成pyd 文件用于 cython調(diào)用的實(shí)現(xiàn)

    這篇文章主要介紹了pyx文件 生成pyd 文件用于 cython調(diào)用的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評(píng)論

宁夏| 芦溪县| 武清区| 青岛市| 商洛市| 蒲江县| 若尔盖县| 宁南县| 土默特左旗| 池州市| 喀喇沁旗| 陇川县| 新民市| 鄄城县| 大化| 旌德县| 汕尾市| 广安市| 如皋市| 年辖:市辖区| 亳州市| 桐柏县| 麻城市| 台中市| 黄平县| 临沧市| 和平区| 晋州市| 谢通门县| 沽源县| 曲靖市| 寿宁县| 师宗县| 蒲江县| 张家港市| 沂水县| 二连浩特市| 特克斯县| 红河县| 芮城县| 岱山县|