通过 Python 向 PDF 添加标题

使用 Python 向 PDF 文件添加标题。

使用 Python 庫將頁眉添加到 PDF 文件

為了在PDF中添加頁眉,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Console

pip install aspose-pdf

通過Python將頁眉添加到 PDF 的步驟


您需要 Aspose.PDF for .NET 在您的環境中嘗試代碼。

  1. 使用 Document 物件開啟 PDF 文件。
  2. 建立圖章並定義其屬性。
  3. 使用 AddStamp 方法將圖章新增至頁面。
  4. 儲存 PDF 檔案。

向 PDF 文档添加标题-Python

此示例代码显示了如何将标题添加到 PDF

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

headerText = apdf.Text.TextFragment("This is a footer")
header = apdf.HeaderFooter()
header.Paragraphs.Add(headerText)
marginInfo = apdf.MarginInfo()
marginInfo.Left = 50
marginInfo.Top = 20

for page in document.pages:
    page.header = header

document.save(path_outfile )