spire.doc for c 12.2.1 已发布。该版本支持通过固定布局获取页的内容。详情请阅读以下内容。
新功能:
- 支持通过固定布局获取页的内容。
// 指定文件路径 wstring input_path = datapath; wstring inputfile = input_path l"in.docx"; wstring output_path = outputpath; wstring outputfile = output_path l"out.txt"; // 创建一个新的 document 实例 intrusive_ptr
document = new document(); // 从指定文件加载文档 document->loadfromfile(inputfile.c_str(), fileformat::docx); intrusive_ptr layoutdoc = new fixedlayoutdocument(document); wstring result; // 使用加载的文档创建一个fixedlayoutdocument对象 intrusive_ptr line = layoutdoc->getpages()->getitem(0)->getcolumns()->getitem(0)->getlines()->getitem(0); result.append(l"line: "); result.append(line->gettext()); result.append(l"\n"); // 检索与该行关联的原始段落 intrusive_ptr para = line->getparagraph(); result.append(l"paragraph text: "); result.append(para->gettext()); result.append(l"\n"); // 以纯文本格式检索第一页上出现的所有文本(包括页眉和页脚) wstring pagetext = layoutdoc->getpages()->getitem(0)->gettext(); result.append(pagetext); result.append(l"\n"); // 循环遍历文档中的每一页并打印每页上出现的行数 for (int i = 0; i < layoutdoc->getpages()->getcount(); i ) { intrusive_ptr page = layoutdoc->getpages()->getitem(i); intrusive_ptr lines = page->getchildentities(layoutelementtype::line, true); result.append(l"page "); result.append(std::to_wstring(page->getpageindex())); result.append(l" has "); result.append(std::to_wstring(lines->getcount())); result.append(l" lines."); result.append(l"\n"); } // 对第一段的布局实体执行反向查找 result.append(l"\n"); result.append(l"the lines of the first paragraph:"); result.append(l"\n"); intrusive_ptr para2 = (object::dynamic_cast (document->getfirstchild()))->getbody()->getparagraphs()->getiteminparagraphcollection(0); intrusive_ptr
paragraphlines = layoutdoc->getlayoutentitiesofnode(para2); for (int i = 0; i < paragraphlines->getcount(); i ) { intrusive_ptr paragraphline = object::dynamic_cast (paragraphlines->getitem(i)); result.append(paragraphline->gettext()); result.append(l"\n"); result.append(paragraphline->getrectangle()->tostring()); result.append(l"\n"); result.append(l"\n"); } // 将提取的文本写入文件 std::wofstream write(outputfile); auto locutf8 = locale(locale(""), new std::codecvt_utf8 ); write.imbue(locutf8); write << result; write.close(); // 处理文档资源 document->dispose();
获取spire.doc for c 12.2.1请点击: