Dear Team,
I need create new style in word document using Aspose.Word in java. Create new style format in two cases.
Case 1: If figure caption exist give the style name as Num_Fig.
Case 2 : If figure caption does not exist mean given the style name as Unum_Fig.
Note:
For the case 1 after removing the images from source document style name should be give it as Num_Fig1 from the removed imaging place.
For the case 2 after removing the images from source document style name should be give it as Unum_Fig1 from the removed imaging place.
Here I have attached sample document for your reference.Sample.zip (274.6 KB)
Thanks for your inquiry. Please note MS Word has four types of styles - character, paragraph, list and table. You can easily create these type of styles using Aspose.Words for Java. Please check following code snippet used to create a sample paragraph style. Hopefully it will help you to accomplish the task.
.....
//Create a paragraph custom Character style
Style parastyle = doc.getStyles().add(StyleType.CHARACTER, "Num_Fig");
parastyle.getFont().setBold(false);
parastyle.getFont().setSize(8);
parastyle.getFont().setName("Arial");
parastyle.getFont().setColor(Color.RED);
.....
para.getParagraphFormat().setStyleName("Num_Fig");
.....
Thanks for your response tilal. Its working fine. I need one more clarification. After removing the image from source document, name should be give it as following format Num_Fig1 from the removed imaging place. please given the solution for this.
Here I have attached Input and expected output for your reference.
Thanks for your inquiry. Please check following code snippet, hopefully it will help you to accomplish the task. You can customize the code as per your requirements. Sample_out_1710.zip (224.7 KB)
Document doc = new Document("Sample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : (Iterable<Shape>) shapes) {
if (shape.hasChart() || shape.hasImage()) {
if (shape.getParentParagraph().getNextSibling() != null && shape.getParentParagraph().getNextSibling().getNodeType() == NodeType.PARAGRAPH)
{
if (shape.getParentParagraph().getNextSibling().toString(SaveFormat.TEXT).contains("Figure"))
{
String stylename = "Num_Fig"+ shape.getParentParagraph().getNextSibling().toString(SaveFormat.TEXT).charAt(7);
Style parastyle = doc.getStyles().add(StyleType.PARAGRAPH, stylename);
parastyle.getFont().setBold(false);
parastyle.getFont().setSize(12);
parastyle.getFont().setName("Arial");
parastyle.getFont().setColor(Color.RED);
Run run = new Run(doc);
run.setText("<Fig>" + stylename + "</Fig>");
builder.moveTo(shape.getParentNode().getPreviousSibling());
Paragraph para = builder.insertParagraph();
para.getParagraphFormat().setStyleName(stylename);
para.appendChild(run);
shape.remove();
}
}
}
}
doc.save("Sample_out_1710.docx");