Add Sound Annotation

Contents
[ ]

Sound annotations in PDFs enable you to add audio content such as voice notes, music, or sound effects to your documents. Using PdfContentEditor, you can define a small clickable rectangle on a page that plays a specified audio file when activated.

  1. Create a PdfContentEditor instance.
  2. Bind the input PDF document.
  3. Define a rectangle for the sound annotation.
  4. Specifie the audio file, annotation name, page number, and sampling rate.
  5. Save the updated PDF document.
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def add_sound_annotation(infile, sound_file, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Add sound annotation to page 1
    content_editor.create_sound(
        apd.Rectangle(80, 450, 30, 30), sound_file, "Speaker", 1, "8000"
    )
    # Save updated document
    content_editor.save(outfile)