使用iText 7.2.0嵌入PDF的视频不可见问题求助
问题:使用iText创建的PDF文件中视频不可见
问题描述
我正在使用iText 7.2.0将视频嵌入PDF文件。尽管PDF生成成功,但打开文件时嵌入的视频既不可见也无法访问。
代码细节
我通过iText创建PDF文档并嵌入视频文件(路径为videos/video.mp4)。确认视频存在后,使用PdfFileSpec.createEmbeddedFileSpec()创建文件规范,接着将PdfLinkAnnotation与PdfAction.createRendition()生成的PdfAction关联,指定视频路径、文件规范、MIME类型和注释,最后把注释添加到PDF第一页。
以下是代码片段:
public class VideoEmbedPdf{ public static void main(String[] args){ String videoPath = "videos/video.mp4"; // 视频文件路径 String pdfOutputPath = "output/outputwithvideo.pdf"; // PDF输出路径 try { PdfWriter writer = new PdfWriter(pdfOutputPath); PdfDocument pdfDocument = new PdfDocument(writer); Document document = new Document(pdfDocument); // 创建说明段落(可选) Paragraph videoInfo = new Paragraph("点击下方链接访问嵌入的视频:") .setFontColor(ColorConstant.BLACK) .setMarginLeft(5); document.add(videoInfo); // 检查视频文件是否存在 File videoFile = new File(videoPath); if (!videoFile.exists()){ System.err.println("未找到视频文件: " + videoPath); return; } PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDocument, videoFile.getAbsolutePath(), null, "My Video", null); Rectangle annotationRect = new Rectangle(100, 700, 100, 20); // 可调整坐标 PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(annotationRect); PdfAction renditionAction = PdfAction.createRendition(videoPath, fileSpec, "video/mp4", linkAnnotation); linkAnnotation.setAction(renditionAction); pdfDocument.getFirstPage().addAnnotation(linkAnnotation); document.close(); System.out.println("含视频的PDF创建成功。"); } catch (IOException e){ e.printStackTrace(); } } }
预期结果
PDF应包含可见且可点击的链接或嵌入视频,能在PDF查看器中无缝播放。
实际结果
打开PDF时,嵌入的视频既不可见也无法访问,执行过程无错误提示。
排查情况
已确认视频文件存在,查阅了iText文档,验证了注释坐标。
解决方法
你的代码存在几个关键问题,导致视频不可见且无法访问,以下是修正方案:
1. 给注释添加可见外观
PdfLinkAnnotation默认没有视觉标识,用户看不到点击区域。需要为注释添加自定义外观(如背景色、提示文本):
// 创建注释外观 PdfAppearance appearance = PdfAppearance.createAppearance(pdfDocument, annotationRect.getWidth(), annotationRect.getHeight()); // 绘制灰色背景 appearance.setFillColor(Color.LIGHT_GRAY); appearance.rectangle(0, 0, annotationRect.getWidth(), annotationRect.getHeight()); appearance.fill(); // 添加提示文本 appearance.beginText(); PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA); appearance.setFontAndSize(font, 12); appearance.setFillColor(Color.BLUE); appearance.setTextMatrix(10, 5); appearance.showText("播放视频"); appearance.endText(); // 将外观绑定到注释 linkAnnotation.setAppearance(PdfName.N, appearance);
2. 修正RenditionAction参数
PdfAction.createRendition()的第一个参数不应传本地视频路径(分发PDF后路径会失效),应传null让iText自动关联嵌入的文件:
PdfAction renditionAction = PdfAction.createRendition(null, fileSpec, "video/mp4", linkAnnotation);
3. 改用更适合的注释类型
推荐使用PdfScreenAnnotation(专门用于多媒体内容)替代PdfLinkAnnotation,兼容性更好:
PdfScreenAnnotation screenAnnotation = new PdfScreenAnnotation(annotationRect); screenAnnotation.setAction(renditionAction); screenAnnotation.setAppearance(PdfName.N, appearance); pdfDocument.getFirstPage().addAnnotation(screenAnnotation);
完整修正代码
public class VideoEmbedPdf{ public static void main(String[] args){ String videoPath = "videos/video.mp4"; String pdfOutputPath = "output/outputwithvideo.pdf"; try { PdfWriter writer = new PdfWriter(pdfOutputPath); PdfDocument pdfDocument = new PdfDocument(writer); Document document = new Document(pdfDocument); Paragraph videoInfo = new Paragraph("点击下方区域播放视频:") .setFontColor(ColorConstant.BLACK) .setMarginLeft(5); document.add(videoInfo); File videoFile = new File(videoPath); if (!videoFile.exists()){ System.err.println("未找到视频文件: " + videoPath); return; } PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDocument, videoFile.getAbsolutePath(), null, "My Video", null); // 扩大注释区域,提升可点击性 Rectangle annotationRect = new Rectangle(100, 700, 200, 150); // 创建注释外观 PdfAppearance appearance = PdfAppearance.createAppearance(pdfDocument, annotationRect.getWidth(), annotationRect.getHeight()); appearance.setFillColor(Color.LIGHT_GRAY); appearance.rectangle(0, 0, annotationRect.getWidth(), annotationRect.getHeight()); appearance.fill(); // 添加居中的提示文本 appearance.beginText(); PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA); appearance.setFontAndSize(font, 14); appearance.setFillColor(Color.BLUE); float textWidth = font.getWidth("点击播放视频", 14); appearance.setTextMatrix((annotationRect.getWidth() - textWidth)/2, (annotationRect.getHeight() - 14)/2); appearance.showText("点击播放视频"); appearance.endText(); // 创建屏幕注释和播放动作 PdfScreenAnnotation screenAnnotation = new PdfScreenAnnotation(annotationRect); PdfAction renditionAction = PdfAction.createRendition(null, fileSpec, "video/mp4", screenAnnotation); screenAnnotation.setAction(renditionAction); screenAnnotation.setAppearance(PdfName.N, appearance); pdfDocument.getFirstPage().addAnnotation(screenAnnotation); document.close(); System.out.println("含视频的PDF创建成功。"); } catch (IOException | PdfException e){ e.printStackTrace(); } } }
额外注意事项
- 确保使用支持嵌入式视频的PDF查看器(如Adobe Acrobat,轻量查看器可能不支持)。
- 视频编码优先选择H.264编码的MP4格式,兼容性最佳。
- 嵌入后的视频已包含在PDF文件内,分发时无需附带外部视频文件。
内容的提问来源于stack exchange,提问作者Yash Pardhi
相关产品推荐
相关产品推荐

