本文整理汇总了Java中org.apache.poi.xwpf.usermodel.XWPFDocument类的典型用法代码示例。如果您正苦于以下问题:Java XWPFDocument类的具体用法?Java XWPFDocument怎么用?Java XWPFDocument使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XWPFDocument类属于org.apache.poi.xwpf.usermodel包,在下文中一共展示了XWPFDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
public static void main( String[] args )
{
long startTime = System.currentTimeMillis();
try
{
// 1) Load docx with POI XWPFDocument
XWPFDocument document = new XWPFDocument( Data.class.getResourceAsStream( "DocxBig.docx" ) );
// 2) Convert POI XWPFDocument 2 PDF with iText
File outFile = new File( "target/DocxBig.pdf" );
outFile.getParentFile().mkdirs();
OutputStream out = new FileOutputStream( outFile );
PdfOptions options = PdfOptions.create().fontEncoding( "windows-1250" );
PdfConverter.getInstance().convert( document, out, options );
}
catch ( Throwable e )
{
e.printStackTrace();
}
System.out.println( "Generate DocxBig.pdf with " + ( System.currentTimeMillis() - startTime ) + " ms." );
}
示例2: readDoc
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
private static String readDoc (String filePath, InputStream is) throws Exception {
String text= "";
is = FileMagic.prepareToCheckMagic(is);
try {
if (FileMagic.valueOf(is) == FileMagic.OLE2) {
WordExtractor ex = new WordExtractor(is);
text = ex.getText();
ex.close();
} else if(FileMagic.valueOf(is) == FileMagic.OOXML) {
XWPFDocument doc = new XWPFDocument(is);
XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
text = extractor.getText();
extractor.close();
}
} catch (OfficeXmlFileException e) {
logger.error(filePath, e);
} finally {
if (is != null) {
is.close();
}
}
return text;
}
示例3: main
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
public static void main( String[] args )
{
long startTime = System.currentTimeMillis();
try
{
// 1) Load docx with POI XWPFDocument
XWPFDocument document = new XWPFDocument( Data.class.getResourceAsStream( "DocxStructures.docx" ) );
// 2) Convert POI XWPFDocument 2 PDF with iText
File outFile = new File( "target/DocxStructures.pdf" );
outFile.getParentFile().mkdirs();
OutputStream out = new FileOutputStream( outFile );
PdfOptions options = null;// PDFViaITextOptions.create().fontEncoding( "windows-1250" );
PdfConverter.getInstance().convert( document, out, options );
}
catch ( Throwable e )
{
e.printStackTrace();
}
System.out.println( "Generate DocxStructures.pdf with " + ( System.currentTimeMillis() - startTime ) + " ms." );
}
示例4: testAccessEmptyIterator
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test(expected = NoSuchElementException.class)
public void testAccessEmptyIterator() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
}
}
示例5: getMissingVariables
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void getMissingVariables() throws IOException, InvalidFormatException {
try (XWPFDocument document = POIServices.getInstance().getXWPFDocument(URIConverter.INSTANCE,
URI.createFileURI("resources/document/properties/missingVariables.docx"));) {
final TemplateCustomProperties properties = new TemplateCustomProperties(document);
final List<String> missingVariables = properties.getMissingVariables();
assertEquals(16, missingVariables.size());
assertEquals("linkNamelinkText", missingVariables.get(0));
assertEquals("bookmarkName", missingVariables.get(1));
assertEquals("queryInBookmark", missingVariables.get(2));
assertEquals("ifCondition", missingVariables.get(3));
assertEquals("queryInIf", missingVariables.get(4));
assertEquals("elseIfCondition", missingVariables.get(5));
assertEquals("queryInElseIf", missingVariables.get(6));
assertEquals("queryInElse", missingVariables.get(7));
assertEquals("letExpression", missingVariables.get(8));
assertEquals("queryInLet", missingVariables.get(9));
assertEquals("forExpression", missingVariables.get(10));
assertEquals("queryInFor", missingVariables.get(11));
assertEquals("queryExpression", missingVariables.get(12));
assertEquals("aqlInSelect", missingVariables.get(13));
assertEquals("aqlLetExpression", missingVariables.get(14));
assertEquals("aqlLetBody", missingVariables.get(15));
}
}
示例6: createDOCXDocument
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
void createDOCXDocument(String pdfText, String outputFileName){
XWPFDocument pdfTextDocument = new XWPFDocument();
XWPFParagraph pdfTextParagraph = pdfTextDocument.createParagraph();;
XWPFRun pdfTextParagraphCharacterRun = pdfTextParagraph.createRun();
StringTokenizer pdfTextReader = new StringTokenizer(pdfText);
String pdfTextLine = null;
while(pdfTextReader.hasMoreTokens()){
pdfTextLine = pdfTextReader.nextToken("\n");
pdfTextParagraphCharacterRun.setText(pdfTextLine);
pdfTextParagraphCharacterRun.addCarriageReturn();
}
try{
pdfTextDocument.write(new FileOutputStream(new File(outputFileName)));
}
catch(Exception e){
System.err.println("An exception occured in creating the DOCX document."+ e.getMessage());
}
}
示例7: readContent
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Override
protected String readContent(final VFSLeaf leaf) throws IOException, DocumentException {
BufferedInputStream bis = null;
final StringBuilder buffy = new StringBuilder();
try {
bis = new BufferedInputStream(leaf.getInputStream());
final POIXMLTextExtractor extractor = (POIXMLTextExtractor) ExtractorFactory.createExtractor(bis);
final POIXMLDocument document = extractor.getDocument();
if (document instanceof XWPFDocument) {
final XWPFDocument xDocument = (XWPFDocument) document;
final XWPFHeaderFooterPolicy hfPolicy = xDocument.getHeaderFooterPolicy();
extractHeaders(buffy, hfPolicy);
extractContent(buffy, xDocument);
extractFooters(buffy, hfPolicy);
}
return buffy.toString();
} catch (final Exception e) {
throw new DocumentException(e.getMessage());
} finally {
if (bis != null) {
bis.close();
}
}
}
示例8: main
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
public static void main(String[] args) throws Exception
{
// Create a new document from scratch
XWPFDocument doc = new XWPFDocument();
// create paragraph
XWPFParagraph para = doc.createParagraph();
// create a run to contain the content
XWPFRun rh = para.createRun();
// Format as desired
rh.setFontSize(15);
rh.setFontFamily("Verdana");
rh.setText("This is the formatted Text");
rh.setColor("fff000");
para.setAlignment(ParagraphAlignment.RIGHT);
// write the file
FileOutputStream out = new FileOutputStream("data/Apache_FormattedText.docx");
doc.write(out);
out.close();
System.out.println("Process Completed Successfully");
}
示例9: readDocx
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
private String readDocx(String path) {
String content = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph para : paragraphs) {
content += para.getText();
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
示例10: microsoftWordDocumentToString
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
private static String microsoftWordDocumentToString(InputStream inputStream) throws IOException {
String strRet;
try (InputStream wordStream = new BufferedInputStream(inputStream)) {
if (POIFSFileSystem.hasPOIFSHeader(wordStream)) {
WordExtractor wordExtractor = new WordExtractor(wordStream);
strRet = wordExtractor.getText();
wordExtractor.close();
} else {
XWPFWordExtractor wordXExtractor = new XWPFWordExtractor(new XWPFDocument(wordStream));
strRet = wordXExtractor.getText();
wordXExtractor.close();
}
}
return strRet;
}
示例11: getNewDefinitionsExisting
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void getNewDefinitionsExisting() throws IOException {
final Generation generation = GenconfPackage.eINSTANCE.getGenconfFactory().createGeneration();
final StringDefinition stringDefinition = GenconfPackage.eINSTANCE.getGenconfFactory().createStringDefinition();
stringDefinition.setKey("variable");
generation.getDefinitions().add(stringDefinition);
try (XWPFDocument document = new XWPFDocument()) {
final TemplateCustomProperties properties = new TemplateCustomProperties(document);
properties.getVariables().put("variable", TemplateCustomProperties.STRING_TYPE);
final List<Definition> definitions = GenconfUtils.getNewDefinitions(generation, properties);
assertEquals(0, definitions.size());
}
}
示例12: getNewDefinitionsExistingInvalidType
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void getNewDefinitionsExistingInvalidType() throws IOException {
final Generation generation = GenconfPackage.eINSTANCE.getGenconfFactory().createGeneration();
final StringDefinition stringDefinition = GenconfPackage.eINSTANCE.getGenconfFactory().createStringDefinition();
stringDefinition.setKey("variable");
generation.getDefinitions().add(stringDefinition);
try (XWPFDocument document = new XWPFDocument()) {
final TemplateCustomProperties properties = new TemplateCustomProperties(document);
properties.getVariables().put("variable", "ecore::EClass");
final List<Definition> definitions = GenconfUtils.getNewDefinitions(generation, properties);
assertEquals(1, definitions.size());
assertTrue(definitions.get(0) instanceof ModelDefinition);
assertEquals("variable", definitions.get(0).getKey());
}
}
示例13: testNonEmptyDoc
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void testNonEmptyDoc() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
XWPFRun run = iterator.next().getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("", run.getText(run.getTextPosition()));
assertTrue(!iterator.hasNext());
}
}
示例14: testLookaheadEmptyIterator
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void testLookaheadEmptyIterator() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
assertNull(iterator.lookAhead(1));
}
}
示例15: testNextWitLookAhead
import org.apache.poi.xwpf.usermodel.XWPFDocument; //导入依赖的package包/类
@Test
public void testNextWitLookAhead() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
// CHECKSTYLE:OFF
assertTrue(iterator.hasElements(7));
// CHECKSTYLE:ON
XWPFRun run;
run = iterator.lookAhead(1).getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.lookAhead(1).getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.lookAhead(2).getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
assertTrue(iterator.hasElements(4));
}
}