本文整理汇总了Java中org.docx4j.Docx4J.toFO方法的典型用法代码示例。如果您正苦于以下问题:Java Docx4J.toFO方法的具体用法?Java Docx4J.toFO怎么用?Java Docx4J.toFO使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.docx4j.Docx4J
的用法示例。
在下文中一共展示了Docx4J.toFO方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertPDF
import org.docx4j.Docx4J; //导入方法依赖的package包/类
@Override
public File convertPDF(File theFile, String md5UploadedFile) throws PDFConverterException {
try {
WordprocessingMLPackage wordMLPckg = Docx4J.load(theFile);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPckg);
String outFileName = this.getOutputFileName(md5UploadedFile);
File outputFile = new File(Config.getString("application.staticFiles"), outFileName);
FileOutputStream pdfStream = new FileOutputStream(outputFile);
Docx4J.toFO(foSettings, pdfStream, Docx4J.FLAG_EXPORT_PREFER_XSL);
pdfStream.close();
return outputFile;
} catch (Exception e) {
log.error("Fail to create PDF in DOC4J Converter", e);
throw new PDFConverterException("Fail to create PDF in DOC4J Converter", e);
}
}
示例2: convert
import org.docx4j.Docx4J; //导入方法依赖的package包/类
@Override
public InputStream convert(InputStream fromInputSource, String toMimeType) {
try {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
String regex = null;
// Windows:
// String
// regex=".*(calibri|camb|cour|arial|symb|times|Times|zapf).*";
//regex=".*(calibri|camb|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
// Mac
// String
// regex=".*(Courier New|Arial|Times New Roman|Comic Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans Serif|MS Serif).*";
PhysicalFonts.setRegex(regex);
WordprocessingMLPackage pkg = WordprocessingMLPackage.load(fromInputSource);
// Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(pkg);
updater.update(true);
// FO exporter setup (required)
// .. the FOSettings object
FOSettings foSettings = Docx4J.createFOSettings();
// if (false) {
// foSettings.setFoDumpFile(new java.io.File("/tmp/test.fo"));
// }
foSettings.setWmlPackage(pkg);
//ByteArrayOutputStream os = new ByteArrayOutputStream();
String outputfilepath;
outputfilepath = "/tmp/temp.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
// Specify whether PDF export uses XSLT or not to create the FO
// (XSLT takes longer, but is more complete).
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
if (pkg.getMainDocumentPart().getFontTablePart()!=null) {
pkg.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
}
return new FileInputStream(outputfilepath);
} catch (Exception e) {
}
return null;
}
示例3: testTblHeaderOne
import org.docx4j.Docx4J; //导入方法依赖的package包/类
/**
* "fo:table" content model is: (marker*,table-column*,table-header?,table-footer?,table-body+)
*
* so a table with just a table header should have
* that converted to table-body row.
*/
@Test
public void testTblHeaderOne() throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/src/test/resources/tables/tblHeaderTestOne.docx";
WordprocessingMLPackage wordMLPackage= WordprocessingMLPackage.load(new java.io.File(inputfilepath));
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
// want the fo document as the result.
foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// exporter writes to an OutputStream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Don't care what type of exporter you use
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);
byte[] bytes = baos.toByteArray();
// System.out.println(new String(bytes, "UTF-8"));
// Now use XPath to assert it has a table-body
try {
org.w3c.dom.Document domDoc = w3cDomDocumentFromByteArray( bytes);
assertTrue(this.isAbsent(domDoc, "//fo:table-header"));
assertTrue(this.isPresent(domDoc, "//fo:table-body"));
} catch (SAXParseException e) {
Assert.fail(e.getMessage());
Assert.fail(new String(bytes, "UTF-8"));
}
}
示例4: testTblHeaderTwo
import org.docx4j.Docx4J; //导入方法依赖的package包/类
/**
* "fo:table" content model is: (marker*,table-column*,table-header?,table-footer?,table-body+)
*
* so a table with body rows before header rows should have
* those body rows converted to header rows.
*/
@Test
public void testTblHeaderTwo() throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/src/test/resources/tables/tblHeaderTestTwo.docx";
WordprocessingMLPackage wordMLPackage= WordprocessingMLPackage.load(new java.io.File(inputfilepath));
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
// want the fo document as the result.
foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// exporter writes to an OutputStream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Don't care what type of exporter you use
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);
byte[] bytes = baos.toByteArray();
// System.out.println(new String(bytes, "UTF-8"));
// Now use XPath to assert it has a table-body
try {
org.w3c.dom.Document domDoc = w3cDomDocumentFromByteArray( bytes);
assertTrue(this.isAbsent(domDoc, "//fo:table-body[following-sibling::fo:table-header]"));
assertTrue(this.isPresent(domDoc, "//fo:table-header[following-sibling::fo:table-body]"));
} catch (SAXParseException e) {
Assert.fail(e.getMessage());
Assert.fail(new String(bytes, "UTF-8"));
}
}
示例5: writeAsPDF
import org.docx4j.Docx4J; //导入方法依赖的package包/类
/**
* Writes this document as PDF to a OutputStream and closed the stream in
* any case.
*
* @param out
* - the Outputstream to write the document.
* @throws IOException
*/
public void writeAsPDF(OutputStream out) throws IOException {
WordprocessingMLPackage mlPackage = getWordMLPackage();
try {
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(mlPackage);
Docx4J.toFO(foSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL);
} catch (Exception e) {
throw new WteException("Unable to create PDF Document", e);
} finally {
out.close();
}
}
示例6: writeToPDFWhithFo
import org.docx4j.Docx4J; //导入方法依赖的package包/类
/**
* 将 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 存为 pdf
*/
public void writeToPDFWhithFo(WordprocessingMLPackage wmlPackage,OutputStream output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
try {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
//String regex = null;
// Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wmlPackage);
updater.update(true);
// .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
// eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
// eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
// to a font which does
PhysicalFonts.get("Arial Unicode MS");
// FO exporter setup (required)
// .. the FOSettings object
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wmlPackage);
foSettings.setApacheFopMime("application/pdf");
// Document format:
// The default implementation of the FORenderer that uses Apache Fop will output
// a PDF document if nothing is passed via
// foSettings.setApacheFopMime(apacheFopMime)
// apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
// FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
//foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// Specify whether PDF export uses XSLT or not to create the FO
// (XSLT takes longer, but is more complete).
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, output, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that uses a xsl transformation
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
// faster, but not yet at feature parity
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
// Clean up, so any ObfuscatedFontPart temp files can be deleted
// if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
// wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
// }
// This would also do it, via finalize() methods
updater = null;
foSettings = null;
wmlPackage = null;
} finally{
IOUtils.closeQuietly(output);
}
}
示例7: getAreaTreeViaFOP
import org.docx4j.Docx4J; //导入方法依赖的package包/类
static org.w3c.dom.Document getAreaTreeViaFOP(WordprocessingMLPackage hfPkg, boolean useXSLT) throws Docx4JException, ParserConfigurationException, SAXException, IOException {
// Currently FOP dependent! But an Antenna House version ought to be feasible.
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(hfPkg);
foSettings.setApacheFopMime(MimeConstants.MIME_FOP_AREA_TREE);
foSettings.setLayoutMasterSetCalculationInProgress(true); // avoid recursion
// foSettings.getFeatures().add(ConversionFeatures.PP_PDF_APACHEFOP_DISABLE_PAGEBREAK_LIST_ITEM); // in 3.0.1, this is off by default
// Since hfPkg is already a clone, we don't need PP_COMMON_DEEP_COPY
// Plus it invokes setFontMapper, which does processEmbeddings again, and those fonts aren't much use to us here
foSettings.getFeatures().remove(ConversionFeatures.PP_COMMON_DEEP_COPY);
if (log.isDebugEnabled()) {
foSettings.setFoDumpFile(new java.io.File(System.getProperty("user.dir") + "/hf.fo"));
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
if (useXSLT) {
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
} else {
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
}
InputStream is = new ByteArrayInputStream(os.toByteArray());
DocumentBuilder builder = XmlUtils.getNewDocumentBuilder();
return builder.parse(is);
}
示例8: outputXSLFO
import org.docx4j.Docx4J; //导入方法依赖的package包/类
public void outputXSLFO(OutputStream os, PdfSettings settings) throws Docx4JException {
setupSettings(settings, FOSettings.MIME_PDF);
Docx4J.toFO(settings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}
示例9: testTblIndentOnCentredTable
import org.docx4j.Docx4J; //导入方法依赖的package包/类
@Test
@Ignore
public void testTblIndentOnCentredTable() throws Exception {
boolean save = true;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setJaxbElement(
(Document)XmlUtils.unmarshalString(table1XML));
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
OutputStream os = null;
if (save) {
os = new FileOutputStream(new File(System.getProperty("user.dir") + "/OUT_testTblIndentOnCentredTable.pdf"));
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_testTblIndentOnCentredTable.docx"));
} else {
// want the fo document as the result.
foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// exporter writes to an OutputStream.
os = new ByteArrayOutputStream();
}
//Don't care what type of exporter you use
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
if (save) {
} else {
byte[] bytes = ((ByteArrayOutputStream)os).toByteArray();
// System.out.println(new String(bytes, "UTF-8"));
// Now use XPath to assert it has a table-body
org.w3c.dom.Document domDoc = w3cDomDocumentFromByteArray( bytes);
assertTrue(this.isAbsent(domDoc, "//fo:table-header"));
assertTrue(this.isPresent(domDoc, "//fo:table-body"));
}
}
示例10: testTblIndentInheritance
import org.docx4j.Docx4J; //导入方法依赖的package包/类
public void testTblIndentInheritance() throws Exception {
boolean save = true;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().setJaxbElement(
(Document)XmlUtils.unmarshalString(table1XML));
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
OutputStream os = null;
if (save) {
os = new FileOutputStream(new File(System.getProperty("user.dir") + "/OUT_testTblIndentInheritance.pdf"));
wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_testTblIndentInheritance.docx"));
} else {
// want the fo document as the result.
foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// exporter writes to an OutputStream.
os = new ByteArrayOutputStream();
}
//Don't care what type of exporter you use
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
if (save) {
} else {
byte[] bytes = ((ByteArrayOutputStream)os).toByteArray();
System.out.println(new String(bytes, "UTF-8"));
// Now use XPath to assert it has a table-body
org.w3c.dom.Document domDoc = w3cDomDocumentFromByteArray( bytes);
assertTrue(this.isAbsent(domDoc, "//fo:table-header"));
assertTrue(this.isPresent(domDoc, "//fo:table-body"));
}
}