本文整理汇总了Java中org.docx4j.openpackaging.parts.PartName类的典型用法代码示例。如果您正苦于以下问题:Java PartName类的具体用法?Java PartName怎么用?Java PartName使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PartName类属于org.docx4j.openpackaging.parts包,在下文中一共展示了PartName类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: insertDocx
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
private 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.setContentType(new ContentType(ContentTypes.APPLICATION_XML));
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();
}
}
示例2: getComments
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
public int getComments(WordprocessingMLPackage wordMLPackage){
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());
}
}
示例3: saveDocxImg
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
/**
* @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();
}
}
}
示例4: collectComments
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
private static void collectComments(final Map<BigInteger, CommentWrapper> comments,
WordprocessingMLPackage document) {
try {
CommentsPart commentsPart = (CommentsPart) document.getParts()
.get(new PartName("/word/comments.xml"));
if (commentsPart != null) {
for (Comments.Comment comment : commentsPart.getContents().getComment()) {
CommentWrapper commentWrapper = comments.get(comment.getId());
if (commentWrapper != null) {
commentWrapper.setComment(comment);
}
}
}
}
catch (Docx4JException e) {
throw new IllegalStateException(e);
}
}
示例5: main
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
String dataPath = "src/featurescomparison/workingwithpresentations/createnewpresentation/data/";
// Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
// Need references to these parts to create a slide
// Please note that these parts *already exist* - they are
// created by createPackage() above. See that method
// for instruction on how to create and add a part.
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 = presentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide1.xml"));
presentationMLPackage.save(new java.io.File(dataPath + "Pptx4j-New Presentation.pptx"));
System.out.println("\n\n done .. \n\n");
}
示例6: insertDocx
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
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();
}
}
示例7: removeAllComment
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
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));
}
示例8: getNumberingPart
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
/**
* Extracts the numbering parts of the docx file
*
* @return
*/
private org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart getNumberingPart() {
org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart numbering = null;
try {
numbering = (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) this.wordMLPackage
.getParts().get(new PartName("/word/numbering.xml"));
if (numbering == null) {
HashMap<org.docx4j.openpackaging.parts.PartName, org.docx4j.openpackaging.parts.Part> mp = this.wordMLPackage
.getParts().getParts();
Iterator<java.util.Map.Entry<org.docx4j.openpackaging.parts.PartName, org.docx4j.openpackaging.parts.Part>> it = mp
.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<org.docx4j.openpackaging.parts.PartName, org.docx4j.openpackaging.parts.Part> pair = (Map.Entry<org.docx4j.openpackaging.parts.PartName, org.docx4j.openpackaging.parts.Part>) it
.next();
if (pair.getValue() instanceof org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) {
numbering = (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) pair
.getValue();
break;
}
it.remove();
}
}
} catch (InvalidFormatException e) {
System.err.println("Couldn't load numbering part.");
}
return numbering;
}
示例9: insertDocx
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
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();
}
}
示例10: main
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
/**
* @param args
* @throws JAXBException
* @throws Docx4JException
*/
public static void main(String[] args) throws JAXBException, Docx4JException {
// TODO Auto-generated method stub
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/sheet1.xml"), "Sheet1", 1);
CTSheetFormatPr format = Context.getsmlObjectFactory().createCTSheetFormatPr();
format.setDefaultRowHeight(5);
format.setCustomHeight(Boolean.TRUE);
sheet.getJaxbElement().setSheetFormatPr(format);
SheetData sheetData = sheet.getJaxbElement().getSheetData();
Row row = Context.getsmlObjectFactory().createRow();
row.setHt(66.0);
row.setCustomHeight(Boolean.TRUE);
row.setR(1L);
Cell cell1 = Context.getsmlObjectFactory().createCell();
cell1.setV("1234");
row.getC().add(cell1);
Cell cell2 = Context.getsmlObjectFactory().createCell();
cell2.setV("56");
row.getC().add(cell2);
sheetData.getRow().add(row);
SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save( System.getProperty("user.dir") + "/data/xlsx4j/RowHeight-Xlsx4j.xlsx");
}
示例11: main
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
String outputfilepath = System.getProperty("user.dir") + "/data/xlsx4j/AddImage-Xlsx4j.xlsx";
String imagefilePath = System.getProperty("user.dir") + "/data/xlsx4j/greentick.png" ;
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
// Create Drawing part and add to sheet
Drawing drawingPart = new Drawing();
Relationship drawingRel = worksheet.addTargetPart(drawingPart);
// Add anchor XML to worksheet
org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing();
worksheet.getJaxbElement().setDrawing(drawing);
drawing.setId( drawingRel.getId() );
// Create image part and add to Drawing part
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(pkg, drawingPart,
FileUtils.readFileToByteArray(new File(imagefilePath) ));
String imageRelID = imagePart.getSourceRelationship().getId();
// Create and set drawing part content
// Take your pick ..
// .. build it using code
// drawingPart.setJaxbElement(
// buildDrawingPartContentUsingCode(imageRelID));
// .. or build it from an XML string
drawingPart.setJaxbElement(
buildDrawingPartContentFromXmlString(imageRelID));
// Save the xlsx
SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save(outputfilepath);
System.out.println("\n\n done .. " + outputfilepath);
}
示例12: createSlidePart
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
/**
* Create a slide and add it to the package
*
* @param pp
* @param layoutPart
* @param i
* @return the slide
* @throws InvalidFormatException
* @throws JAXBException
*/
private static SlidePart createSlidePart(MainPresentationPart pp, SlideLayoutPart layoutPart, int i)
throws InvalidFormatException, JAXBException {
// Slide part
SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide" + i +".xml") );
pp.addSlideIdListEntry(slidePart);
slidePart.setJaxbElement( SlidePart.createSld() );
// Slide layout part
slidePart.addTargetPart(layoutPart);
return slidePart;
}
示例13: main
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
public static void main(String[] args) throws Exception
{
String dataPath = "src/featurescomparison/workingwithpresentations/createtable/data/";
// Where will we save our new .ppxt?
String outputfilepath = dataPath + "Tables-Pptx4j.pptx";
// Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
// Need references to these parts to create a slide
// Please note that these parts *already exist* - they are
// created by createPackage() above. See that method
// for instruction on how to create and add a part.
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 = presentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide1.xml"));
// Method 1 - programmatic
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( getTable() );
// Method 2 - from string - on slide 2
SlidePart slide2 = presentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide2.xml"));
slide2.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( createGraphicFrameFromString() );
// All done: save it
presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("\n\n done .. saved " + outputfilepath);
}
示例14: getCalculationChain
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
protected CTCalcChain getCalculationChain() {
CTCalcChain calculationChain = null;
try {
CalcChain part = (CalcChain) result.getPackage().getParts().get(new PartName("/xl/calcChain.xml"));
if (part != null) {
calculationChain = part.getJaxbElement();
calculationChain.getC().clear();
}
} catch (InvalidFormatException e) {
//do nothing
}
return calculationChain;
}
示例15: mergeUsingCTAltChunk
import org.docx4j.openpackaging.parts.PartName; //导入依赖的package包/类
/**
* Merge two docx files using an approach that is based on CTAltChunk.
*
* @param separatorTexts
* @param topFile
* @param bottomFile
* @param outputFile
* @return
* @throws Exception
*/
protected static File mergeUsingCTAltChunk(
WordprocessingMLPackage topPackage, File bottomFile,
File outputFile) throws Exception {
/*
* Based on
* https://stackoverflow.com/questions/2494549/is-there-any-java-library
* -maybe-poi-which-allows-to-merge-docx-files
*
*/
FileInputStream bottomIs = new FileInputStream(bottomFile);
MainDocumentPart topMainPart = topPackage.getMainDocumentPart();
// Get binary representation of bottom file
byte[] bottomAsBytes = IOUtils.toByteArray(bottomIs);
/*
* Determine a suitable name for the new part, one that is not already
* taken (in case of multiple merges).
*/
Parts docParts = topPackage.getParts();
Set<PartName> docPartsNames = docParts.getParts().keySet();
Set<String> plainPartNames = new HashSet<String>();
for (PartName pn : docPartsNames) {
plainPartNames.add(pn.getName());
}
String partName = null;
int index = 0;
do {
partName = "/part" + index + ".docx";
index++;
} while (plainPartNames.contains(partName));
/*
* Now add the bottom file as another part to the top package, and add a
* CTAltChunk to the main document of the top package that references
* this new part.
*/
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
new PartName(partName));
afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bottomAsBytes);
Relationship altChunkRel = topMainPart.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
topMainPart.addObject(chunk);
topMainPart.convertAltChunks();
/*
* Finally, save the modified top package to the output file and return
* that file.
*/
topPackage.save(outputFile);
return outputFile;
}