PDF 中的媒体注释
Contents
[
Hide
]
本文展示了如何在 PDF 文档中使用 Aspose.PDF for Python via .NET 处理媒体注释。
示例脚本演示了多个多媒体工作流:
- 添加声音注释
- 从 U3D 模型创建 3D 注释
- 从媒体文件添加屏幕注释
- 添加和删除富媒体注释
- 检查现有的多媒体注释
添加声音注释
此示例向现有 PDF 的第一页添加声音注释,并将其链接到存储在输入目录中的 WAV 媒体文件。
打开 PDF 并定义媒体文件
media_dir = path.dirname(infile)
document = ap.Document(infile)
page = document.pages[1]
media_file = path.join(media_dir, "file_example_WAV_1MG.wav")
创建并配置声音注释
设置注释矩形、颜色、标题和主题。然后添加弹出注释,以在选中注释时提供额外的详细信息。
sound_annotation = ann.SoundAnnotation(
page,
ap.Rectangle(20, 700, 60, 740, True),
media_file,
)
sound_annotation.color = ap.Color.blue
sound_annotation.title = "John Smith"
sound_annotation.subject = "Sound Annotation demo"
sound_annotation.popup = ann.PopupAnnotation(
page,
ap.Rectangle(20, 700, 60, 740, True),
)
添加注释并保存 PDF
page.annotations.append(sound_annotation)
document.save(outfile)
完整示例
def sound_annotation_add(infile, outfile):
media_dir = path.dirname(infile)
document = ap.Document(infile)
page = document.pages[1]
media_file = path.join(media_dir, "file_example_WAV_1MG.wav")
sound_annotation = ann.SoundAnnotation(
page,
ap.Rectangle(20, 700, 60, 740, True),
media_file,
)
sound_annotation.color = ap.Color.blue
sound_annotation.title = "John Smith"
sound_annotation.subject = "Sound Annotation demo"
sound_annotation.popup = ann.PopupAnnotation(
page,
ap.Rectangle(20, 700, 60, 740, True),
)
page.annotations.append(sound_annotation)
document.save(outfile)
添加 3D 注释
此工作流创建一个新的 PDF 并嵌入来自 U3D 文件的 3D 模型。它还为 3D 内容定义了预设视图和渲染设置。
创建 PDF 文档和 3D 内容
model_file = infile
document = ap.Document()
pdf3d_content = ann.PDF3DContent(model_file)
pdf3d_artwork = ann.PDF3DArtwork(document, pdf3d_content)
pdf3d_artwork.lighting_scheme = ann.PDF3DLightingScheme(type_name="CAD")
pdf3d_artwork.render_mode = ann.PDF3DRenderMode(type_name="Solid")
定义 3D 视图矩阵
这些矩阵描述了如何从不同视点显示 3D 模型。
top_matrix = ap.Matrix3D(
1,
0,
0,
0,
-1,
0,
0,
0,
-1,
0.10271,
0.08184,
0.273836,
)
front_matrix = ap.Matrix3D(
0,
-1,
0,
0,
0,
1,
-1,
0,
0,
0.332652,
0.08184,
0.085273,
)
向艺术作品添加命名视图
pdf3d_artwork.view_array.add(ann.PDF3DView(document, top_matrix, 0.188563, "Top"))
pdf3d_artwork.view_array.add(ann.PDF3DView(document, front_matrix, 0.188563, "Left"))
创建注释并保存文档
page = document.pages.add()
pdf3d_annotation = ann.PDF3DAnnotation(
page,
ap.Rectangle(100, 500, 300, 700, True),
pdf3d_artwork,
)
pdf3d_annotation.border = ann.Border(pdf3d_annotation)
pdf3d_annotation.set_default_view_index(1)
pdf3d_annotation.flags = ann.AnnotationFlags.NO_ZOOM
pdf3d_annotation.name = path.basename(model_file)
page.annotations.append(pdf3d_annotation)
document.save(outfile)
完整示例
def annotation_3d_add(infile, outfile):
model_file = infile
document = ap.Document()
pdf3d_content = ann.PDF3DContent(model_file)
pdf3d_artwork = ann.PDF3DArtwork(document, pdf3d_content)
pdf3d_artwork.lighting_scheme = ann.PDF3DLightingScheme(type_name="CAD")
pdf3d_artwork.render_mode = ann.PDF3DRenderMode(type_name="Solid")
top_matrix = ap.Matrix3D(
1,
0,
0,
0,
-1,
0,
0,
0,
-1,
0.10271,
0.08184,
0.273836,
)
front_matrix = ap.Matrix3D(
0,
-1,
0,
0,
0,
1,
-1,
0,
0,
0.332652,
0.08184,
0.085273,
)
pdf3d_artwork.view_array.add(ann.PDF3DView(document, top_matrix, 0.188563, "Top"))
pdf3d_artwork.view_array.add(
ann.PDF3DView(document, front_matrix, 0.188563, "Left")
)
page = document.pages.add()
pdf3d_annotation = ann.PDF3DAnnotation(
page,
ap.Rectangle(100, 500, 300, 700, True),
pdf3d_artwork,
)
pdf3d_annotation.border = ann.Border(pdf3d_annotation)
pdf3d_annotation.set_default_view_index(1)
pdf3d_annotation.flags = ann.AnnotationFlags.NO_ZOOM
pdf3d_annotation.name = path.basename(model_file)
page.annotations.append(pdf3d_annotation)
document.save(outfile)
添加屏幕注释
屏幕批注允许您将可播放的媒体附加到 PDF 页面。本例创建一个新 PDF,并基于 SWF 文件添加屏幕批注。
创建 PDF 和页面
media_file = infile
document = ap.Document()
page = document.pages.add()
创建屏幕注释
screen_annotation = ann.ScreenAnnotation(
page,
ap.Rectangle(170, 190, 470, 380, True),
media_file,
)
添加注释并保存 PDF
page.annotations.append(screen_annotation)
document.save(outfile)
完整示例
def screen_annotation_with_media_add(infile, outfile):
media_file = infile
document = ap.Document()
page = document.pages.add()
screen_annotation = ann.ScreenAnnotation(
page,
ap.Rectangle(170, 190, 470, 380, True),
media_file,
)
page.annotations.append(screen_annotation)
document.save(outfile)
富媒体注释
添加富媒体注释
富媒体注释可以嵌入高级交互内容,例如带有海报、皮肤和自定义播放设置的视频播放器。
准备媒体和播放器资源
示例从预定义的位置加载视频、海报图像和播放器皮肤文件。
media_dir = path.dirname(infile)
path_to_adobe_app = (
r"C:\\Program Files (x86)\\Adobe\\Acrobat 2017\\Acrobat\\Multimedia Skins"
)
document = ap.Document()
page = document.pages.add()
video_name = "file_example_MP4_480_1_5MG.mp4"
poster_name = "file_example_MP4_480_1_5MG_poster.jpg"
skin_name = "SkinOverAllNoFullNoCaption.swf"
创建富媒体注释
rich_media_annotation = ann.RichMediaAnnotation(
page,
ap.Rectangle(100, 500, 300, 600, True),
)
附加自定义播放器、皮肤、海报和视频
player_path = os.path.join(path_to_adobe_app, "Players", "Videoplayer.swf")
rich_media_annotation.custom_player = open(player_path, "rb")
rich_media_annotation.custom_flash_variables = f"source={video_name}&skin={skin_name}"
skin_path = os.path.join(path_to_adobe_app, skin_name)
rich_media_annotation.add_custom_data(skin_name, open(skin_path, "rb"))
poster_path = os.path.join(media_dir, poster_name)
rich_media_annotation.set_poster(open(poster_path, "rb"))
video_path = os.path.join(media_dir, video_name)
with open(video_path, "rb") as video_file:
rich_media_annotation.set_content(video_name, video_file)
设置播放行为并保存 PDF
该注释被配置为视频内容,并在用户点击时激活。
rich_media_annotation.type = ann.RichMediaAnnotation.ContentType.VIDEO
rich_media_annotation.activate_on = ann.RichMediaAnnotation.ActivationEvent.CLICK
rich_media_annotation.update()
page.annotations.append(rich_media_annotation)
document.save(outfile)
完整示例
def rich_media_annotations_add(infile, outfile):
media_dir = path.dirname(infile)
path_to_adobe_app = (
r"C:\\Program Files (x86)\\Adobe\\Acrobat 2017\\Acrobat\\Multimedia Skins"
)
document = ap.Document()
page = document.pages.add()
video_name = "file_example_MP4_480_1_5MG.mp4"
poster_name = "file_example_MP4_480_1_5MG_poster.jpg"
skin_name = "SkinOverAllNoFullNoCaption.swf"
rich_media_annotation = ann.RichMediaAnnotation(
page,
ap.Rectangle(100, 500, 300, 600, True),
)
player_path = os.path.join(path_to_adobe_app, "Players", "Videoplayer.swf")
rich_media_annotation.custom_player = open(player_path, "rb")
rich_media_annotation.custom_flash_variables = (
f"source={video_name}&skin={skin_name}"
)
skin_path = os.path.join(path_to_adobe_app, skin_name)
rich_media_annotation.add_custom_data(skin_name, open(skin_path, "rb"))
poster_path = os.path.join(media_dir, poster_name)
rich_media_annotation.set_poster(open(poster_path, "rb"))
video_path = os.path.join(media_dir, video_name)
with open(video_path, "rb") as video_file:
rich_media_annotation.set_content(video_name, video_file)
rich_media_annotation.type = ann.RichMediaAnnotation.ContentType.VIDEO
rich_media_annotation.activate_on = ann.RichMediaAnnotation.ActivationEvent.CLICK
rich_media_annotation.update()
page.annotations.append(rich_media_annotation)
document.save(outfile)
删除富媒体注释
此工作流会从现有 PDF 的首页中移除所有富媒体注释。
打开 PDF 并收集富媒体注释
document = ap.Document(infile)
page = document.pages[1]
to_delete = [
annotation
for annotation in page.annotations
if annotation.annotation_type == ann.AnnotationType.RICH_MEDIA
]
删除注释并保存文档
for annotation in to_delete:
page.annotations.delete(annotation)
document.save(outfile)
完整示例
def rich_media_annotations_delete(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
to_delete = [
annotation
for annotation in page.annotations
if annotation.annotation_type == ann.AnnotationType.RICH_MEDIA
]
for annotation in to_delete:
page.annotations.delete(annotation)
document.save(outfile)
获取 Multimedia_annotations
要检查已存储在 PDF 中的多媒体内容,请筛选注释集合中的 screen、sound 和 rich media 注释类型。
打开文档并定义目标注释类型
document = ap.Document(infile)
target_types = {
ann.AnnotationType.SCREEN,
ann.AnnotationType.SOUND,
ann.AnnotationType.RICH_MEDIA,
}
打印多媒体注释矩形
for annotation in document.pages[1].annotations:
if annotation.annotation_type in target_types:
print(f"{annotation.annotation_type} [{annotation.rect}]")
完整示例
def multimedia_annotations_get(infile, outfile):
document = ap.Document(infile)
target_types = {
ann.AnnotationType.SCREEN,
ann.AnnotationType.SOUND,
ann.AnnotationType.RICH_MEDIA,
}
for annotation in document.pages[1].annotations:
if annotation.annotation_type in target_types:
print(f"{annotation.annotation_type} [{annotation.rect}]")