本文整理汇总了Java中org.docx4j.openpackaging.exceptions.Docx4JException类的典型用法代码示例。如果您正苦于以下问题:Java Docx4JException类的具体用法?Java Docx4JException怎么用?Java Docx4JException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Docx4JException类属于org.docx4j.openpackaging.exceptions包,在下文中一共展示了Docx4JException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: render
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
protected void render(FopFactory fopFactory, FOUserAgent foUserAgent, String outputFormat, Source foDocumentSrc, PlaceholderReplacementHandler.PlaceholderLookup placeholderLookup, OutputStream outputStream) throws Docx4JException {
Fop fop = null;
Result result = null;
try {
if (foUserAgent==null) {
fop = fopFactory.newFop(outputFormat, outputStream);
} else {
fop = fopFactory.newFop(outputFormat, foUserAgent, outputStream);
}
result = (placeholderLookup == null ?
//1 Pass
new SAXResult(fop.getDefaultHandler()) :
//2 Pass
new SAXResult(new PlaceholderReplacementHandler(fop.getDefaultHandler(), placeholderLookup)));
} catch (FOPException e) {
throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
}
XmlSerializerUtil.serialize(foDocumentSrc, result, false, false);
}
示例2: main
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
/**
* 跟前面的做的一样, 我们再一次创建了一个表格, 并添加了三个单元格, 其中有两个
* 单元带有样式. 在新方法中我们传进表格行, 单元格内容, 是否为粗体及字体大小作
* 为参数. 你需要注意, 因为the Office Open specification规范定义这个属性是半个
* 点(half-point)大小, 因此字体大小需要是你想在Word中显示大小的两倍,
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Tbl table = factory.createTbl();
Tr tableRow = factory.createTr();
addRegularTableCell(tableRow, "Normal text");
addStyledTableCell(tableRow, "Bold text", true, null);
addStyledTableCell(tableRow, "Bold large text", true, "40");
table.getContent().add(tableRow);
addBorders(table);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new java.io.File("src/main/files/HelloWord6.docx") );
}
示例3: getClickAction
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
@Override
public void getClickAction(final Dictionary dictionary, final TabFactory tabFactory, final DialogFactory dialogFactory) {
EditorTab currTab = ((EditorTab) tabFactory.getSelectedTab());
try {
if (currTab.getEditorPane().saveDocx()) {
dialogFactory.buildConfirmationDialogBox(
dictionary.DIALOG_EXPORT_SUCCESS_TITLE,
dictionary.DIALOG_EXPORT_SUCCESS_DOCX_CONTENT
).showAndWait();
}
} catch (Docx4JException | JAXBException e1) {
dialogFactory.buildExceptionDialogBox(
dictionary.DIALOG_EXCEPTION_TITLE,
dictionary.DIALOG_EXCEPTION_EXPORT_DOCX_CONTENT,
e1.getMessage(),
e1
).showAndWait();
}
}
示例4: getTable
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
/**
* 该方法找到表格,获取第一行并且遍历提供的map向表格添加新行,在将其返回之前删除模版行。这个方法用到了两个助手方法:addRowToTable 和 getTemplateTable。我们首先看一下后面的那个:
*/
public static Tbl getTable(List<Tbl> tables, String placeholder) throws Docx4JException {
for (Iterator<Tbl> iterator = tables.iterator(); iterator.hasNext();) {
Tbl tbl = iterator.next();
//查找当前table下面的text对象
List<Text> textElements = getTargetElements(tbl, Text.class);
for (Text text : textElements) {
Text textElement = (Text) text;
//
if (textElement.getValue() != null && textElement.getValue().equals(placeholder)) {
return (Tbl) tbl;
}
}
}
return null;
}
示例5: replaceTable
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd,
WordprocessingMLPackage template) throws Docx4JException, JAXBException {
List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class);
// 1. find the table
Tbl tempTable = getTable(tables, placeholders[0]);
List<Tr> rows = getTargetElements(tempTable, Tr.class);
// first row is header, second row is content
if (rows.size() == 2) {
// this is our template row
Tr templateRow = (Tr) rows.get(1);
for (Map<String, String> replacements : textToAdd) {
// 2 and 3 are done in this method
addRowToTable(tempTable, templateRow, replacements);
}
// 4. remove the template row
tempTable.getContent().remove(templateRow);
}
}
示例6: writeToStream
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的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);
}
}
示例7: writeToWriter
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的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);
}
}
示例8: main
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
/**
* First we create the package, then we alter the style sheet and add some
* styled paragraphs. Finally we save the package.
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
alterStyleSheet();
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title",
"Hello World! This title is now in Arial.");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",
"Subtitle, this subtitle is now Arial too");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1",
"As is Heading1");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading2",
"Heading2 is now Arial, no longer bold and has an underline " +
"and fontsize 12");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading3",
"Heading3 is now Arial");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Normal",
"And normal text has changed to Arial and fontsize 10");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord12.docx") );
}
示例9: main
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
/**
* 创建一个带边框的表格并添加四个带内容的行, 然后将表格添加到文档并保存
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Tbl table = factory.createTbl();
addBorders(table);
addTableRowWithMergedCells("Heading 1", "Heading 1.1",
"Field 1", table);
addTableRowWithMergedCells(null, "Heading 1.2", "Field 2", table);
addTableRowWithMergedCells("Heading 2", "Heading 2.1",
"Field 3", table);
addTableRowWithMergedCells(null, "Heading 2.2", "Field 4", table);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new java.io.File(
"src/main/files/HelloWord9.docx") );
}
示例10: main
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
/**
* 创建一个带边框的表格并添加一行. 然后添加两个带内容的单元格并给定宽度.
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Tbl table = factory.createTbl();
addBorders(table);
Tr tr = factory.createTr();
addTableCellWithWidth(tr, "Field 1", 2500);
addTableCellWithWidth(tr, "Field 2", 0);
table.getContent().add(tr);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new java.io.File("src/main/HelloWord133.docx") );
}
示例11: convertDocxFilesToTxtFiles
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
public static void convertDocxFilesToTxtFiles(String inputFolder, String outputFolder, boolean overwrite) throws IOException, Docx4JException, JAXBException {
File inDir = new File(inputFolder);
if (!inDir.exists()) {
throw new IOException("Input folder does not exist: "+inputFolder);
}
File outDir = createDirectory(outputFolder, overwrite);
File[] docxFiles = inDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".docx");
}
});
for (File docx : docxFiles) {
String txt = extractTextFromDocx(docx.getAbsolutePath());
String basename = FilenameUtils.getBaseName(docx.getName());
Files.write(Paths.get(outDir.getAbsolutePath()+File.separator+basename+".txt"), txt.getBytes());
}
}
示例12: replaceParagraph
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的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);
}
}
}
}
示例13: setResults
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
public void setResults(FormattingResults formattingResults) throws Docx4JException {
List<PageSequenceResults> resultList = null;
PageSequenceResults pageSequenceResults = null;
if (formattingResults == null) {
throw new Docx4JException("Apache fop returned no FormattingResults (null)");
}
else {
resultList = formattingResults.getPageSequences();
if (resultList == null) {
throw new Docx4JException("Apache fop returned null pageSequences");
}
else if (resultList.size() != pageNumberInformation.size()) {
throw new Docx4JException("Apache fop returned different count of sections than expected, returned: " + resultList.size() + ", expected: " + pageNumberInformation.size());
}
}
putDocumentPageCount(formattingResults.getPageCount());
for (int i=0; i<formattingResults.getPageSequences().size(); i++) {
pageSequenceResults = resultList.get(i);
putSectionPageCount(i, pageSequenceResults.getPageCount());
}
}
示例14: render
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
public void render(String foDocument, FOSettings settings,
boolean twoPass,
List<SectionPageInformation> pageNumberInformation,
OutputStream outputStream) throws Docx4JException {
Writer writer = null;
if (twoPass) {
log.warn("Using the DummyFORenderer with a two pass conversion, there might be placeholders in the output");
}
try {
writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(foDocument);
writer.flush();
} catch (Exception e) {
throw new Docx4JException("Exception while storing fo document to OutputStream: " + e.getMessage(), e);
}
}
示例15: createSectionRoot
import org.docx4j.openpackaging.exceptions.Docx4JException; //导入依赖的package包/类
@Override
protected Element createSectionRoot(FOConversionContext conversionContext,
Document document,
ConversionSectionWrapper sectionWrapper, Element currentParent) throws Docx4JException {
Element pageSequence = document.createElementNS(XSL_FO, "page-sequence");
int pageNumberInitial = sectionWrapper.getPageNumberInformation().getPageStart();
String pageFormat = sectionWrapper.getPageNumberInformation().getPageFormat();
pageSequence.setAttribute("master-reference", sectionWrapper.getId());
pageSequence.setAttribute("id", "section_" + sectionWrapper.getId());
pageFormat = FormattingSwitchHelper.getFoPageNumberFormat(pageFormat);
if (pageNumberInitial > -1) {
pageSequence.setAttribute("initial-page-number", Integer.toString(pageNumberInitial));
}
if (pageFormat != null) {
pageSequence.setAttribute("format", pageFormat);
}
return pageSequence;
}