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> document = new Document();
// 从指定文件加载文档
document->LoadFromFile(inputFile.c_str(), FileFormat::Docx);
intrusive_ptr<FixedLayoutDocument> layoutDoc = new FixedLayoutDocument(document);
wstring result;
// 使用加载的文档创建一个FixedLayoutDocument对象
intrusive_ptr<FixedLayoutLine> 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<Paragraph> 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<FixedLayoutPage> page = layoutDoc->GetPages()->GetItem(i);
intrusive_ptr<LayoutCollection> 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<Paragraph> para2 = (Object::Dynamic_cast<Section>(document->GetFirstChild()))->GetBody()->GetParagraphs()->GetItemInParagraphCollection(0);
intrusive_ptr<LayoutCollection> paragraphLines = layoutDoc->GetLayoutEntitiesOfNode(para2);
for (int i = 0; i < paragraphLines->GetCount(); i++)
{
intrusive_ptr<FixedLayoutLine> paragraphLine = Object::Dynamic_cast<FixedLayoutLine>(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<wchar_t>);
write.imbue(LocUtf8);
write << result;
write.close();
// 处理文档资源
document->Dispose();
获取Spire.Doc for C++ 12.2.1请点击: