use of org.docx4j.openpackaging.parts.Part 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.Part 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.Part in project com.revolsys.open by revolsys.
the class XlsxRecordReader method initDo.
@Override
protected void initDo() {
super.initDo();
try (InputStream in = this.resource.newBufferedInputStream()) {
final SpreadsheetMLPackage spreadsheetPackage = (SpreadsheetMLPackage) OpcPackage.load(in);
final DocPropsCustomPart customProperties = spreadsheetPackage.getDocPropsCustomPart();
if (customProperties != null) {
int srid = 0;
try {
srid = Integer.parseInt(customProperties.getProperty("srid").getLpwstr());
} catch (final Throwable e) {
}
int axisCount = 2;
try {
axisCount = Integer.parseInt(customProperties.getProperty("axisCount").getLpwstr());
if (axisCount > 4) {
axisCount = 2;
}
} catch (final Throwable e) {
}
double scaleXy = 0;
try {
scaleXy = Double.parseDouble(customProperties.getProperty("scaleXy").getLpwstr());
} catch (final Throwable e) {
}
double scaleZ = 0;
try {
scaleZ = Double.parseDouble(customProperties.getProperty("scaleZ").getLpwstr());
} catch (final Throwable e) {
}
final GeometryFactory geometryFactory = GeometryFactory.fixed(srid, axisCount, scaleXy, scaleXy, scaleZ);
setGeometryFactory(geometryFactory);
}
WorksheetPart worksheetPart = null;
for (final Part part : spreadsheetPackage.getParts().getParts().values()) {
if (part instanceof WorksheetPart) {
if (worksheetPart == null) {
worksheetPart = (WorksheetPart) part;
}
} else if (part instanceof SharedStrings) {
final SharedStrings sharedStrings = (SharedStrings) part;
final CTSst contents = sharedStrings.getContents();
this.sharedStringList = contents.getSi();
}
}
if (worksheetPart != null) {
final Worksheet worksheet = worksheetPart.getContents();
final SheetData sheetData = worksheet.getSheetData();
this.rows = sheetData.getRow();
final List<String> line = readNextRow();
final String baseName = this.resource.getBaseName();
newRecordDefinition(baseName, line);
}
} catch (final IOException | Docx4JException e) {
Logs.error(this, "Unable to open " + this.resource, e);
} catch (final NoSuchElementException e) {
}
}
use of org.docx4j.openpackaging.parts.Part in project docx4j-template by vindell.
the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.
/**
* @Description: 提取word图片
*/
public void saveDocxImg(String filePath, String savePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
if (entry.getValue() instanceof BinaryPartAbstractImage) {
BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
// 图片minetype
String imgContentType = binImg.getContentType();
PartName pt = binImg.getPartName();
String fileName = null;
if (pt.getName().indexOf("word/media/") != -1) {
fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
}
System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
FileOutputStream fos = new FileOutputStream(savePath + fileName);
((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
fos.close();
}
}
}
use of org.docx4j.openpackaging.parts.Part in project docx4j-template by vindell.
the class Docx4j_删除所有批注_S3_Test method removeAllComment.
// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
// 清空comments.xml内容
Parts parts = wordMLPackage.getParts();
HashMap<PartName, Part> partMap = parts.getParts();
CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
Comments comments = commentPart.getContents();
List<Comment> commentList = comments.getComment();
for (int i = 0, len = commentList.size(); i < len; i++) {
commentList.remove(0);
}
// 清空document.xml文件中批注
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
Body body = wmlDocumentEl.getBody();
CommentFinder cf = new CommentFinder();
new TraversalUtil(body, cf);
for (Child commentElement : cf.commentElements) {
System.out.println(commentElement.getClass().getName());
Object parent = commentElement.getParent();
List<Object> theList = ((ContentAccessor) parent).getContent();
boolean removeResult = remove(theList, commentElement);
System.out.println(removeResult);
}
wordMLPackage.save(new FileOutputStream(savePath));
}
Aggregations