为有中文需求的客户提供多渠道中文技术支持.

Wed Jul 10, 2024 3:53 am

//循环word中的每一节
for (int j = 0; j < data.newDoc.Sections.Count; j++)
{
Section section = data.newDoc.Sections[j];
//怎样获取当前节(section )第一页和最后一页的页码?
}

jslzhangtao
 
Posts: 5
Joined: Sat Mar 09, 2024 7:07 am

Wed Jul 10, 2024 9:01 am

你好,

感谢留言。
请参考下面的代码获取指定章节的第一页页码和最后一页页码。如还有疑问或者获取有误,请提供文档供我们进一步测试,感谢您的配合!

Code: Select all
        // 加载文档
        Document doc = new Document();
        doc.loadFromFile("TestCircle.docx");
        // 将文档转换为固定版式
        FixedLayoutDocument fixedLayoutDocument = new FixedLayoutDocument(doc);
        // 获取指定章节
        Section sec = doc.getSections().get(2);
        // 获取章节的第一个段落
        Paragraph para_start = sec.getParagraphs().get(0);
        // 获取第一个段落的页码
        LayoutCollection<LayoutElement> layoutElements_start =  fixedLayoutDocument.getLayoutEntitiesOfNode(para_start);
        System.out.println(layoutElements_start.get(0).getPageIndex());
        // 获取最后一个段落
        Paragraph para_end = sec.getParagraphs().get(sec.getParagraphs().getCount()-1);
        // 获取最后一个段落的页码
        LayoutCollection<LayoutElement> layoutElements_end =  fixedLayoutDocument.getLayoutEntitiesOfNode(para_end);
        System.out.println(layoutElements_end.get(0).getPageIndex());


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 330
Joined: Mon Nov 07, 2022 8:10 am

Return to 中文技术支持