我正在使用 docx4j 库创建 PPT 文件。我已经能够创建带有文本和图像的幻灯片,但无法向其中添加注释。
我正在创建这样的幻灯片:
MainPresentationPart pp = (MainPresentationPart)presentationParts.get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart)presentationParts.get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
SlidePart slidePart = PresentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide" + ++slideNumber + ".xml"));
这样我就可以向正文添加文本或图像,但是当我尝试访问字段 SlidePart.notes 时,它为空。我尝试过初始化它
slidePart.setPartShortcut(new NotesSlidePart());
但是笔记中的所有内容都是空的,我没有取得任何成就。
那么,有没有人有一个如何向 PPT 文件添加注释的有效示例?
非常感谢
最佳答案
这还不够:
slidePart.setPartShortcut(new NotesSlidePart());
您需要通过调用 addTargetPart 将注释幻灯片部分显式添加到您的幻灯片部分(以便正确设置关系)。
但是考虑到 pptx 格式的工作方式,您还需要做更多的事情。要查看需要哪些部分,请将 pptx 上传到 docx4j Web 应用程序。这是我刚刚基于此编写的代码:
// Now add notes slide.
// 1. Notes master
NotesMasterPart nmp = new NotesMasterPart();
NotesMaster notesmaster = (NotesMaster)XmlUtils.unmarshalString(notesMasterXml, Context.jcPML);
nmp.setJaxbElement(notesmaster);
// .. connect it to /ppt/presentation.xml
Relationship ppRelNmp = pp.addTargetPart(nmp);
/*
* <p:notesMasterIdLst>
<p:notesMasterId r:id="rId3"/>
</p:notesMasterIdLst>
*/
pp.getJaxbElement().setNotesMasterIdLst(createNotesMasterIdListPlusEntry(ppRelNmp.getId()));
// .. NotesMasterPart typically has a rel to a theme
// .. can we get away without it?
// Nope .. read this in from a file
ThemePart themePart = new ThemePart(new PartName("/ppt/theme/theme2.xml"));
// TODO: read it from a string instead
themePart.unmarshal(
FileUtils.openInputStream(new File(System.getProperty("user.dir") + "/theme2.xml"))
);
nmp.addTargetPart(themePart);
// 2. Notes slide
NotesSlidePart nsp = new NotesSlidePart();
Notes notes = (Notes)XmlUtils.unmarshalString(notesXML, Context.jcPML);
nsp.setJaxbElement(notes);
// .. connect it to the slide
slidePart.addTargetPart(nsp);
// .. it also has a rel to the slide
nsp.addTargetPart(slidePart);
// .. and the slide master
nsp.addTargetPart(nmp);
您可以在 https://github.com/plutext/docx4j/blob/master/src/samples/pptx4j/org/pptx4j/samples/SlideNotes.java 找到完整的示例
关于java - 如何使用docx4j添加PPT注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20428080/