java有什么好的方法计算word页数 poi好像不准

java有什么好的方法计算word页数 poi好像不准,第1张

XWPFDocument docx = new XWPFDocument(POIXMLDocumentopenPackage(电月度例会会议纪要docx));

int pages = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getPages();//总页数

int wordCount = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getCharacters();// 忽略空格的总字符数 另外还有getCharactersWithSpaces()方法获取带空格的总字数。

Systemoutprintln (pages= + pages + wordCount= + wordCount);}

public static void parse97() throws Exception {

WordExtractor doc = new WordExtractor(new FileInputStream(电月度例会会议纪要doc));

XWPFDocument docx = new XWPFDocument(POIXMLDocumentopenPackage(电月度例会会议纪要docx));

int pages = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getPages();//总页数

int wordCount = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getCharacters();// 忽略空格的总字符数 另外还有getCharactersWithSpaces()方法获取带空格的总字数。

Systemoutprintln (pages= + pages + wordCount= + wordCount);}

public static void parse97() throws Exception {

WordExtractor doc = new WordExtractor(new FileInputStream(电月度例会会议纪要doc));

XWPFDocument docx = new XWPFDocument(POIXMLDocumentopenPackage(电月度例会会议纪要docx)); int pages = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getPages();//总页数 int wordCount = docxgetProperties()getExtendedProperties()getUnderlyingProperties()getCharacters();// 忽略空格的总字符数 另外还有getCharactersWithSpaces()方法获取带空格的总字数。 Systemoutprintln (pages= + pages + wordCount= + wordCount);} public static void parse97() throws Exception { WordExtractor doc = new WordExtractor(new FileInputStream(电月度例会会议纪要doc));

试试这个代码,需要添加spiredoc jar依赖

import comspiredocDocument;

import comspiredocFileFormat;

import comspiredocSection;

import comspiredocdocuments;

import comspiredocfieldsDocPicture;

public class InsertImage {

    public static void main(String[] args) {

        //实例化Document对象

        Document doc = new Document();

        //加载文档

        docloadFromFile("C:\\Users\\Administrator\\Desktop\\testdocx");

        //获取第一个section

        Section section = docgetSections()get(0);

        //添加一个段落

        Paragraph para = sectionaddParagraph();

        //添加到段落

        DocPicture picture = paraappendPicture("C:\\Users\\Administrator\\Desktop\\Cartoonpng");

        //设置文字环绕方式(居于文字上方)

        picturesetTextWrappingStyle(TextWrappingStyleIn_Front_Of_Text);

        //指定的相对位置

        picturesetHorizontalOrigin(HorizontalOriginPage);

        picturesetHorizontalPosition(250f);

        picturesetVerticalOrigin(VerticalOriginTop_Margin_Area);

        picturesetVerticalPosition(150f);

        //设置大小

        picturesetWidth(80f);

        picturesetHeight(80f);

        //保存到文档

        docsaveToFile("output/InsertImagedocx", FileFormatDocx);

    }

}

生成的Word:

步骤

第一步,使用输入流打开文件,并获得文档的XWPFDocument对象。然后获得文档的所有段落,进而获得要 *** 作的文本框所在的段落,具体使用时候,可以通过判断或者print *** 作得知要 *** 作的文本框到底是哪一段。

FileInputStream fis = newFileInputStream("e:/filedocx");

XWPFDocument doc = new XWPFDocument(fis);

List<XWPFParagraph> paragraphList =docgetParagraphs();

XWPFParagraph paragraph = paragraphListget(10);

文本框在Word中显示

第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象的游标。可以通过打印XmlObject来得知当前XML的内容,也可以使用XmlCursor的getName方法和getTextValue方法来查看当前游标所在位置的Node及Node的值。

XmlObject object =paragraphgetCTP()getRArray(1);

XmlCursor cursor = objectnewCursor();

第四步,通过移动游标,找到要修改的文本所在位置,然后使用游标的setTextValue来设置其值。

//修改第一处文本:

cursortoChild(1); cursortoChild(0);cursortoChild(3); cursortoChild(0); cursortoChild(0); cursortoChild(3);cursortoChild(1); cursorsetTextValue("First");

// 修改第二处文本

cursortoParent(); cursortoParent();cursortoChild(1);

cursortoChild(3); cursortoChild(1);

cursorsetTextValue("Second");

第四步,保存文件、关闭输入输出流。

FileOutputStream fos = newFileOutputStream("e:/exportdocx");

docwrite(fos);

fosflush();

fosclose();

fisclose();

修改后的文本框。

以上就是关于java有什么好的方法计算word页数 poi好像不准全部的内容,包括:java有什么好的方法计算word页数 poi好像不准、java如何获取word文档页数、Java如何调用PPT当前页码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9301877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存