is there an API to get all text with similar formatting in a word document?
@vgm,
Thanks for your inquiry. You can use Aspose.Words to get the text with similar formatting. Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting
- to Run nodes by using Character Styles e.g. a Glyph Style.
- to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles).
- you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.
You can use Document.GetChildNodes(NodeType.Paragraph, true) method to get the Paragraph nodes of document. To get the style of paragraph, please use ParagraphFormat.StyleName. Hope this helps you.
Thanks Tahir for the information. What I’m looking for is, given a document, if there is a text with some formatting, then I need to get all text in the document that have similar formatting. Is there a way to do this in java?
This is the functionality I’m talking about: Select some text in the doc, Goto Home->Editing->Select->Select All Text With Similar Formatting.
@vgm,
Thanks for your inquiry. Please use the following code example to get the desired output.
Document doc = new Document(MyDir + "in.docx");
//Get the fifth paragraph from the document. E.g. its style name is "Heading 1"
Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
String styleName = para.getParagraphFormat().getStyleName();
System.out.println(para.getParagraphFormat().getStyleName());
for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.getParagraphFormat().getStyleName().equals(styleName))
System.out.println(paragraph.toString(SaveFormat.TEXT));
}
Thanks Tahir for your reply.
comparing style names didn’t help. It doesn’t perform the functionality of “Select All Text With Similar Formatting”. Is there any way to achieve this?
@vgm,
Thanks for your inquiry. Please ZIP and attach your input Word document and your expected result here for testing. We will investigate the issue on our side and provide you more information.
Hi Tahir,
Attached is the sample document.Sample Doc .zip (11.9 KB)
Expected result:
for e.g., if you select “1.Georgia” and say “Select All Text with Formatting”, then “2.Resolution” and “3. Hence” will be selected. In case “(1) de-facto” is selected and “Select All Text with Formatting” is performed, then “(2) powerful” and “Georgia” under 3rd item shall get selected.
@vgm,
Thanks for your inquiry. We have logged this feature request as WORDSNET-16651 in our issue tracking system. You will be notified via this forum thread once this feature is available.
We apologize for your inconvenience.
Hi Tahir,
Thanks for logging a feature request. When may we expect this feature to be available?
@vgm,
Thanks for your inquiry. We try our best to deal with every customer request in a timely fashion, we unfortunately cannot guarantee a delivery date to every customer issue. We work on issues on a first come, first served basis. We feel this is the fairest and most appropriate way to satisfy the needs of the majority of our customers.
Currently, this feature is pending for analysis and is in the queue. Once we complete the analysis, we will then be able to provide you an estimate.
You asked for this feature in free support forum and it will be treated with normal priority. To speed up the progress of issue’s resolution, we suggest you please check our paid support policies from following link.
Paid Support Policies