Hello!
I am evaluating the product Aspose.PDF for Python via .NET and I cannot make a fundamental example for the project it is related to: adding an audio (an MP3 file) to a PDF document.
My system is a fully updated Linux Debian 12 to which I have installed the packages (and their dependencies):
- python3-full
- python3-pip
- libgdiplus
Then I created a specific environment for Python with the command : python3 -m venv .venv
to let me install the aspose-pdf package using pip [source .venv/bin/activate
]: pip3 install aspose-pdf
.
From the previus Aspose PDF pip package I have taken the example code to create a simple PDF and check that the library works:
import aspose.pdf as ap
# Instantiate an object of HtmlLoadOptions
options = ap.HtmlLoadOptions()
# Convert HTML to PDF
document = ap.Document("input.html", options)
# Save PDF
document.save("output.pdf")
With it I created a PDF document that I used with the following example code (taken from the Aspose knowledge base) that inserts an MP3 audio file into the PDF file:
import aspose.pdf as pdf
# Load the license
license_pdf = pdf.License()
license_pdf.set_license("Aspose.PDFforPythonvia.NET.lic")
# Load the PDF file
pdfDoc = pdf.Document("output.pdf")
# Create SoundAnnotation object
sound = pdf.annotations.SoundAnnotation(pdfDoc.pages[1], pdf.Rectangle(25, 750, 70, 840, True), "beyond-horizons-201654.mp3")
# Set sound annotation properties
sound.color = pdf.Color.red
sound.title = "It is a click"
sound.subject = "Sound Annotation demo for embedding sound"
sound.popup = pdf.annotations.PopupAnnotation(pdfDoc)
# Add annotation to the PDF and save it
pdfDoc.pages[1].annotations.add(sound, True)
pdfDoc.save("OutputMp3.pdf")
print("Audio added successfully")
In order to execute the previous codes I found on the Internet that I had to export the following variable to the system so that it would not cause problems:
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
The program runs without error but when I try to see the result of the PDF the embedded audio [ābeyond-horizons-201654.mp3ā, a musical audio of 1:58 seconds long and free to use downloaded from the Internet] is not heard, only sounds like static, like an untuned TV channel.
I am using a trial license (Aspose.PDFforPythonvia.NET.lic) that I have requested for the package in case it was necessary to use it to obtain all the functionalities (although I understand that it would not be necessary).
I have added to the post the final result (the PDF with audio) so that you can see (listen to it )
Could anyone tell me what the hell Iām doing wrong?
All the best
OutputMp3.pdf (3,6 MB)