PythonでPDFヘッダーとフッターを追加する

使用する HeaderArtifact そして FooterArtifact PDFページにランニングテキストを追加します。これらの例は最初のページにアーティファクトを追加します。

スタイル付きテキストアーティファクトを作成する

追加例を実行する前に、このヘルパー関数を定義してください。この関数は、ヘッダーとフッターに同じフォント、サイズ、色、配置を適用します。

import aspose.pdf as ap

def _create_text_artifact(artifact_class, text):
    """Create a text artifact (header/footer) with common styling."""
    artifact = artifact_class()
    artifact.text = text
    artifact.text_state.font_size = 14
    artifact.text_state.font = ap.text.FontRepository.find_font("Arial")
    artifact.text_state.foreground_color = ap.Color.navy
    artifact.artifact_horizontal_alignment = ap.HorizontalAlignment.CENTER
    return artifact

PDF にヘッダーを追加する

ヘルパー関数に HeaderArtifact を渡し、返されたアーティファクトをページに追加してください。

import aspose.pdf as ap

def add_header_artifact(infile, outfile):
    """Add a header artifact to the first page of a PDF document."""
    with ap.Document(infile) as document:
        header = _create_text_artifact(ap.HeaderArtifact, "Sample Header")
        document.pages[1].artifacts.append(header)
        document.save(outfile)

PDF にフッターを追加する

ヘルパー関数に FooterArtifact を渡し、返されたアーティファクトをページに追加してください。

import aspose.pdf as ap

def add_footer_artifact(infile, outfile):
    """Add a footer artifact to the first page of a PDF document."""
    with ap.Document(infile) as document:
        footer = _create_text_artifact(ap.FooterArtifact, "Sample Footer")
        document.pages[1].artifacts.append(footer)
        document.save(outfile)

関連トピック