I am using Aspose.Word to extract text from a word document. So far I am getting all the paragraphs in the document using par_col = ((Body)doc.getSections().get(2).getBody()).getParagraphs(); It works excellent, however I want to be able to retrieve all the paragraphs in the document EXCLUDING paragraphs that are inside of any tables. Any help is appreciated.
Thanks for your inquiry. I think you can use DocuemntVisitor to do that.
In this case, you can determine whether paragraph is inside table of not.
Also, you can get all paragraphs form the document and exclude paragraphs, which are children of tables during further processing. For example, see the following code:
// Open document
Document doc = new Document("C:\\Temp\\in");
// Get all paragraphs
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
// Loop through all paragraphs
for (int parIdx = 0; parIdx < paragraphs.getCount(); parIdx++)
{
// Get paragraph
Paragraph par = (Paragraph)paragraphs.get(parIdx);
// Check if paragraph is child of table.
// And exclude such paragraphs from further processing.
if (par.getAncestor(NodeType.TABLE) != null)
continue;
// Do something useful..............
// .................................
}
That was my first thought, however since the shapes are grouped it combines the text from each text box into a signle paragraph, and that paragraph returns null for this test
if (par.getAncestor(NodeType.SHAPE) != null)
I think you can reproduce this behavior with my sample file posted in the other thread.
I think, I understand what is going wrong on your side. You use par.toTxt() method to get text of your paragraphs, but shapes also are children of paragraphs in Word documents, so par.toTxt() also returns text of Shapes (text of all paragraphs inside Shape). To remove this text, you should remove shapes from the document.
// Remove Group shapes
NodeCollection groupShapes = doc.getChildNodes(NodeType.GROUP_SHAPE, true);
while (groupShapes.getCount() != 0)
groupShapes.removeAt(0);
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.