ssvel
November 9, 2017, 5:34am
1
Dear Team.,
I need solution for remove line number from word document using aspose.words java. And also add logo in first page header position.Here I have attached sample input and expected output.
Input: input.zip (9.9 KB)
Expected Output : Exp_Ouput.zip (179.5 KB)
Best Regards.,
Vadivel S S
@Vadivel_S_S ,
Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.
Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertImage(MyDir + "input.png");
//Remove empty paragraphs from the end of document.
while (doc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
{
if(doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.GROUP_SHAPE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FORM_FIELD, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FOOTNOTE, true).getCount() > 0
|| doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.COMMENT, true).getCount() > 0
)
break;
//Check if last paragraph contains the page break
if(doc.getLastSection().getBody().getLastParagraph().isEndOfDocument())
{
doc.getLastSection().getBody().getLastParagraph().getRange().replace(ControlChar.PAGE_BREAK, "", new FindReplaceOptions());
}
if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
(doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH))
break;
doc.getLastSection().getBody().getLastParagraph().remove();
// If the current section becomes empty, we should remove it.
if (!doc.getLastSection().getBody().hasChildNodes())
doc.getLastSection().remove();
// We should exit the loop if the document becomes empty.
if (!doc.hasChildNodes())
break;
}
//Remove list items
for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.isListItem())
{
paragraph.getListFormat().removeNumbers();
}
if(paragraph.toString(SaveFormat.TEXT).trim().endsWith(".") &&
isNumeric(paragraph.toString(SaveFormat.TEXT).trim() + "0"))
{
paragraph.remove();
}
}
doc.save(MyDir + "17.10.docx");
public static boolean isNumeric(String str)
{
return str.matches("-?\\d+(.\\d+)?");
}
ssvel
November 9, 2017, 8:44am
3
Dear Tahir.,
Thanks for your quick replying. Its working fine but below document line number is not removing.I have attached that document for your reference.
Document : Aspo.zip (34.1 KB)
Regards.,
S S Vadivel
@Vadivel_S_S ,
Thanks for your inquiry. You can remove line numbering from word document by setting the value of PageSetup.LineNumberCountBy property to 0 . Please check the following code example. Hope this helps you.
Document doc = new Document(MyDir + "Asp.docx");
PageSetup ps = doc.getFirstSection().getPageSetup();
ps.setLineNumberCountBy(0);
doc.save(MyDir + "17.10.docx");
ssvel
November 10, 2017, 3:44am
5
Dear Tahir,
Thank you so much.Its working fine.Its very useful us.
Regards.,
S S Vadivel