PDFBox创建声音对象,链接/引用外部mp3或wav文件

时间:2016-04-20 16:32:04

标签: java pdf pdfbox javasound

我正在编写一个实用程序,使用基于开源java的PDFBox来转换包含'Hyperlink to open a mp3 file'的PDF文件,将其替换为声音对象。

我使用了PDFBox API,因为它看起来已经足够成熟,可以使用Sound对象。我可以阅读PDF文件,并参考mp3找到超链接。但我无法用声音对象替换它。我创建了声音对象并与动作关联,但它不起作用。我想我错过了一些重要的部分,如何使用PDActionSound对象创建Sound对象。是否可以使用PDFBox API引用外部wav文件?

for (PDPage pdPage : pages) {
   List<PDAnnotation> annotations = pdPage.getAnnotations();
   for (PDAnnotation pdAnnotation : annotations) { 
      if (pdAnnotation instanceof PDAnnotationLink) {
          PDAnnotationLink link = ((PDAnnotationLink) pdAnnotation);
          PDAction action = link.getAction();
          if (action instanceof PDActionLaunch) {
              PDActionLaunch launch = ((PDActionLaunch) action);
              String fileInfo = launch.getFile().getFile();
              if (fileInfo.contains(".mp3")) {
                /* create Sound object referring to external mp3*/
                //something like
                PDActionSound actionSound = new PDActionSound(
                                        soundStream);
                //set the ActionSound to the link. 
                link.setAction(actionSound);  
              }
          }
      }
   }
}

如何创建声音对象(PDActionSound)并成功添加到链接?

1 个答案:

答案 0 :(得分:1)

说到成熟,那部分从未被使用过,现在我已经仔细研究了代码,我认为还有一些工作要做...请试试这个,我用PDFBox 2.0创建了这个PDF规范:

PDSimpleFileSpecification fileSpec = new PDSimpleFileSpecification(new COSString("/C/dir1/dir2/blah.mp3")); // see "File Specification Strings" in PDF spec
COSStream soundStream = new COSStream();
soundStream.createOutputStream().close();
soundStream.setItem(COSName.F, fileSpec);
soundStream.setInt(COSName.R, 44100); // put actual sample rate here
PDActionSound actionSound = new PDActionSound(); 
actionSound.getCOSObject().setItem(COSName.getPDFName("Sound"), soundStream)); 
link.setAction(actionSound); // reassign the new action to the link annotation

编辑:因为上面没有用,所以这里是评论中要求的替代解决方案。该文件是嵌入的。它仅适用于.WAV文件,您必须知道它们的详细信息。一开始大概丢失了1/2秒。你应该听到的声音是“我是Al Bundy”。我尝试使用MP3并没有成功。谷歌搜索时,我发现一些文字说只支持“旧”格式(wav,aif等)。我确实找到了另一种播放声音的方式(“Renditions”),它甚至可以在another product中使用嵌入式mp3,但PDF中生成的结构更加复杂。

COSStream soundStream = new COSStream();
OutputStream os = soundStream.createOutputStream(COSName.FLATE_DECODE);
URL url = new URL("http://cd.textfiles.com/hackchronii/WAV/ALBUNDY1.WAV");
InputStream is = url.openStream();
// FileInputStream is = new FileInputStream(".....WAV");
IOUtils.copy(is, os);
is.close();
os.close();
// See p. 506 in PDF spec, Table 294
soundStream.setInt(COSName.C, 1); // channels
soundStream.setInt(COSName.R, 22050); // sampling rate
//soundStream.setString(COSName.E, "Signed"); // The encoding format for the sample data
soundStream.setInt(COSName.B, 8); // The number of bits per sample value per channel. Default value: 8
// soundStream.setName(COSName.CO, "MP3"); // doesn't work
PDActionSound actionSound = new PDActionSound();
actionSound.getCOSObject().setItem(COSName.getPDFName("Sound"), soundStream);
link.setAction(actionSound);

2016年7月7日更新:

我们在PDFBox邮件列表上讨论了这个问题,感谢Gilad Denneboom我们还知道了两件事: 1)在Adobe Acrobat中,它只允许您选择WAV或AIF文件 2)由Gilad Denneboom编写的代码MP3SPI将MP3转换为raw:

private static InputStream getAudioStream(String filename) throws Exception {
    File file = new File(filename);
    AudioInputStream in = AudioSystem.getAudioInputStream(file);
    AudioFormat baseFormat = in.getFormat();
    AudioFormat decodedFormat = new AudioFormat(
        AudioFormat.Encoding.PCM_UNSIGNED,
        baseFormat.getSampleRate(),
        baseFormat.getSampleSizeInBits(),
        baseFormat.getChannels(),
        baseFormat.getChannels(),
        baseFormat.getSampleRate(),
        false);
    return AudioSystem.getAudioInputStream(decodedFormat, in);
}