use of org.docx4j.openpackaging.parts.PartName in project Java-Tutorial by gpcodervn.
the class LinkToSlides method startPptx.
private static void startPptx() throws Exception {
// generate Ppt file
String outFileName = "output/LinkToSlides.pptx";
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
MainPresentationPart pp = (MainPresentationPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
// OK, now we can create a slide
SlidePart slidePart = null;
slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide1.xml"));
org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory();
// first Link relation
org.docx4j.relationships.Relationship rel1 = factory.createRelationship();
rel1.setType(Namespaces.PRESENTATIONML_SLIDE);
rel1.setTarget("/ppt/slides/slide" + 5 + ".xml");
slidePart.getRelationshipsPart().addRelationship(rel1);
String linkedTable = table1.replaceFirst("replaceID", rel1.getId());
// second link relation
org.docx4j.relationships.Relationship rel2 = factory.createRelationship();
rel2.setType(Namespaces.PRESENTATIONML_SLIDE);
rel2.setTarget("/ppt/slides/slide" + 10 + ".xml");
slidePart.getRelationshipsPart().addRelationship(rel2);
linkedTable = linkedTable.replaceFirst("replaceID", rel2.getId());
CTGraphicalObjectFrame graphicFrame2 = (CTGraphicalObjectFrame) XmlUtils.unmarshalString(linkedTable, Context.jcPML, CTGraphicalObjectFrame.class);
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(graphicFrame2);
for (int i = 2; i < 15; i++) {
slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide" + i + ".xml"));
Shape sample = ((Shape) XmlUtils.unmarshalString(getTextData(i), Context.jcPML));
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
}
presentationMLPackage.save(new java.io.File(outFileName));
System.out.println("Done");
}
use of org.docx4j.openpackaging.parts.PartName in project Java-Tutorial by gpcodervn.
the class ReportFromTemplate method main.
public static void main(String[] args) {
final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
String fileName = "";
try {
// Populate the Strings that will replace the template text
Map<String, String> map = new HashMap<String, String>();
map.put("Project", "BP Mount");
map.put("Date", "21-Mar-2011");
// C:\\test\\template1.docx is the template file
WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("resources/Template1.docx"));
Parts parts = template.getParts();
HashMap<PartName, Part> partsMap = parts.getParts();
PartName partName = null;
Part part = null;
Set<PartName> set = partsMap.keySet();
for (Iterator<PartName> iterator = set.iterator(); iterator.hasNext(); ) {
PartName name = (PartName) iterator.next();
if (name.getName().equalsIgnoreCase("/word/media/image1.png")) {
part = partsMap.get(name);
partName = name;
}
}
if (part != null && partName != null) {
part = partsMap.get(partName);
BinaryPart binaryPart = (BinaryPart) part;
binaryPart.setBinaryData(fileToBytes(fileToReplace));
}
List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
for (Object key : map.keySet()) {
// textValue = textValue.replaceAll("\\$\\{" + key + "\\}", (String) map.get(key));
textValue = textValue.trim().replace("${" + key + "}", (String) map.get(key));
}
text.setValue(textValue);
}
/*
* Add the other contents here
*/
template.save(new File("output/ReportFromTemplate.docx"));
System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Errors");
}
}
use of org.docx4j.openpackaging.parts.PartName in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method getComments.
public int getComments(WordprocessingMLPackage wordMLPackage) throws Exception {
Parts parts = wordMLPackage.getParts();
HashMap<PartName, Part> partMap = parts.getParts();
CommentsPart commentPart = (CommentsPart) partMap.get(new CommentsPart().getPartName());
Comments comments = commentPart.getContents();
List<Comment> commentList = comments.getComment();
for (Comment comment : commentList) {
StringBuffer sb = new StringBuffer();
sb.append(" ID: ").append(comment.getId());
sb.append(" 作者:").append(comment.getAuthor());
sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime());
sb.append(" 内容:").append(comment.getContent());
// sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString()));
System.out.println(sb.toString());
}
return 0;
}
use of org.docx4j.openpackaging.parts.PartName in project docx4j-template by vindell.
the class WMLPackageUtils method insertDocx.
// 插入文档
private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
try {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bytes);
Relationship altChunkRel = main.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addObject(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.docx4j.openpackaging.parts.PartName in project Aspose.Cells-for-Java by aspose-cells.
the class Xlsx4jAddComments method main.
/**
* @param args
*/
public static void main(String[] args) {
try {
// The path to the documents directory.
String dataDir = Utils.getDataDir(Xlsx4jAddComments.class);
String outputfilepath = dataDir + "AddComments-Xlsx4j.xlsx";
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
addContent(sheet);
pkg.save(new File(outputfilepath));
System.out.println("\n\n done .. " + outputfilepath);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations