本文整理汇总了Java中org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart类的典型用法代码示例。如果您正苦于以下问题:Java MainDocumentPart类的具体用法?Java MainDocumentPart怎么用?Java MainDocumentPart使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MainDocumentPart类属于org.docx4j.openpackaging.parts.WordprocessingML包,在下文中一共展示了MainDocumentPart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Docx4J_简单例子 t = new Docx4J_简单例子();
WordprocessingMLPackage wordMLPackage = t
.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
//页眉
Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);
t.createHeaderReference(wordMLPackage, mp, factory, relationship);
t.addParagraphTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory);
//页脚
t.createNormalTableTest(wordMLPackage, mp, factory);
relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory);
t.createFooterReference(wordMLPackage, mp, factory, relationship);
t.saveWordPackage(wordMLPackage, new File(
"f:/saveFile/temp/s_simple.docx"));
}
示例2: insertDocx
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的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();
}
}
示例3: writeToStream
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public static void writeToStream(WordprocessingMLPackage wmlPackage,OutputStream output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage);
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//输出模板
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(input);
}
}
示例4: writeToWriter
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public void writeToWriter(WordprocessingMLPackage wmlPackage,Writer output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage.getPackage());
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//获取模板输出编码格式
String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_WMLTEMPLATE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
//输出模板
IOUtils.copy(input, output, Charset.forName(charsetName));
} finally {
IOUtils.closeQuietly(input);
}
}
示例5: getAllTbl
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
/**
* @Description:得到所有表格
*/
public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {
MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();
List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);
if (objList == null) {
return null;
}
List<Tbl> tblList = new ArrayList<Tbl>();
for (Object obj : objList) {
if (obj instanceof Tbl) {
Tbl tbl = (Tbl) obj;
tblList.add(tbl);
}
}
return tblList;
}
示例6: createHeaderReference
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
示例7: createFooterReference
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public void createFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
示例8: main
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Docx4J_简单例子 t = new Docx4J_简单例子();
WordprocessingMLPackage wordMLPackage = t
.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
//图片页眉
//Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);
//文字页眉
Relationship relationship =t.createTextHeaderPart(wordMLPackage, mp, factory, "我是页眉,多创造,少抄袭", JcEnumeration.CENTER);
t.createHeaderReference(wordMLPackage, mp, factory, relationship);
t.addParagraphTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory);
t.createNormalTableTest(wordMLPackage, mp, factory);
//页脚
relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory);
t.createFooterReference(wordMLPackage, mp, factory, relationship);
t.saveWordPackage(wordMLPackage, new File(
"f:/saveFile/temp/s5_simple.docx"));
}
示例9: createHeaderReference
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
示例10: createFooterReference
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
public void createFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
示例11: addParagraph
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
private static void addParagraph(String mdName, String mdValue, MainDocumentPart mdp, String style) {
if (mdValue != null && !mdValue.equals("")){
org.docx4j.wml.P p = mdp.createStyledParagraphOfText(style, mdName + mdValue);
mdp.addObject(p);
// org.docx4j.wml.Text t = factory.createText();
// t.setValue(mdName + mdValue);
// t.setSpace("preserve");
//
// org.docx4j.wml.R run = factory.createR();
// p.getContent().add(run);
// run.getContent().add(t);
//
// org.docx4j.wml.PPr pPr = factory.createPPr();
// p.setPPr(pPr);
//
// org.docx4j.wml.PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
// pPr.setPStyle(pStyle);
// pStyle.setVal(style);
}
}
示例12: getFormattedTextForLineElement
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
private static void getFormattedTextForLineElement(List<WordType> lines, P p, MainDocumentPart mdp) throws Exception{
int wordCount = 0;
int nrWords = lines.size();
for (WordType word : lines){
getFormattedTextForShapeElement((ITrpShapeType) word, p, mdp);
//add empty space after each word
if (wordCount < nrWords-1){
org.docx4j.wml.Text t = factory.createText();
t.setValue(" ");
t.setSpace("preserve");
org.docx4j.wml.R run = factory.createR();
p.getContent().add(run);
run.getContent().add(t);
}
wordCount++;
}
}
示例13: CreateWordprocessingMLPackageFromTemplate
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
/**
* 创建Docx的主方法
*
* @param templatePath 模板docx路径
* @param parameters 参数和值
* @param paragraphParameters 段落参数
* @param imageParameters 书签和图片
* @return
*/
private static WordprocessingMLPackage CreateWordprocessingMLPackageFromTemplate(String templatePath,
HashMap<String, String> parameters,
HashMap<String, String> paragraphParameters,
HashMap<String, String> imageParameters)
throws Exception {
@Cleanup InputStream docxStream = DocxProducer.class.getResourceAsStream(templatePath);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(docxStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
//第一步 替换字符参数
if (parameters != null) {
replaceParameters(documentPart, parameters);
}
//第二步 替换段落
if (paragraphParameters != null) {
replaceParagraph(documentPart, paragraphParameters);
}
//第三步 插入图片
if (imageParameters != null) {
replaceBookMarkWithImage(wordMLPackage, documentPart, imageParameters);
}
return wordMLPackage;
}
示例14: replaceParagraph
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
/**
* 根据字符串参数替换段落
*
* @param documentPart
* @param paragraphParameters
*/
private static void replaceParagraph(MainDocumentPart documentPart, HashMap<String, String> paragraphParameters) throws JAXBException, Docx4JException {
//List<Object> tables = getAllElementFromObject(documentPart, Tbl.class);
/*for (Map.Entry<String, String> entries : paragraphParameters.entrySet()) {
final Tbl table = getTemplateTable(tables, entries.getKey());
final List<Object> allElementFromObject = getAllElementFromObject(table, P.class);
final P p = (P) allElementFromObject.get(1);
appendParaRContent(p, entries.getValue());
}*/
final List<Object> allElementFromObject = getAllElementFromObject(documentPart, P.class);
//final P p = (P) allElementFromObject.get(22);
for (Object paragraph : allElementFromObject) {
final P para = (P) paragraph;
if (!isNullOrEmpty(para.getContent())) {
final List<Object> content = para.getContent();
final String stringFromContent = getStringFromContent(content);
final String s = paragraphParameters.get(stringFromContent);
if (s != null) {
appendParaRContent(para, s);
}
}
}
}
示例15: extractTextFromParagraph
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; //导入依赖的package包/类
/**
* Extract text from the paragraph and append it to the provided {@link StringBuilder}.
*
* @param stringBuilder
* The {@link StringBuilder} to append text to.
* @param formattingData
* Formatting data map to update with ranges of paragraph that are formatted.
* @param paragraph
* The docx4j paragraph object
* @param paragraphProperties
* The docx4j paragraph properties
* @param styleMap
* The extracted styles from the main document
* @param mainDoc
* The main document object
*/
private void extractTextFromParagraph(final StringBuilder stringBuilder, final Map<FormattingType, Set<TextRange>> formattingData,
final P paragraph, final PPr paragraphProperties, final Map<String, Style> styleMap, final MainDocumentPart mainDoc) {
for (final Object paragraphChild : paragraph.getContent()) {
if (paragraphChild instanceof R) {
final R run = (R) paragraphChild;
for (final Object runChild : run.getContent()) {
if (runChild instanceof JAXBElement && ((JAXBElement<?>) runChild).getDeclaredType() == Text.class) {
final String childText = ((Text) ((JAXBElement<?>) runChild).getValue()).getValue();
final TextRange childRange = new TextRange(stringBuilder.length(), stringBuilder.length() + childText.length());
stringBuilder.append(childText);
extractFormattingData(run, childRange, formattingData, paragraphProperties, styleMap, mainDoc);
}
}
}
}
}