Getting margin for the paragraph

Hi,

I want to take the margin(left, right, top and bottom) set to each paragraph using aspose words for java.

here is my code,

ParagraphFormat para = ((Paragraph) this.node).getParagraphFormat();
System.out.println("Margin = " + para.getSpaceAfter() + "...Text = " + this.node.getText());

I am getting the output as

Text: Title => Margin Top = 0.0 and Margin Bottom = 15.0
Text: Subtitle => Margin Top = 0.0 and Margin Bottom = 0.0
Text: SubtleEmphasis => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Emphasis => Margin Top = 0.0 and Margin Bottom = 0.0
Text: IntenseEmphasis => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Strong => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Quote => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Intense Quote => Margin Top = 10.0 and Margin Bottom = 14.0
Text: SubtleReference => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Intense Reference => Margin Top = 0.0 and Margin Bottom = 0.0
Text: Book Title => Margin Top = 0.0 and Margin Bottom = 0.0
Text: List Paragraph => Margin Top = 0.0 and Margin Bottom = 0.0
Text: No Spacing => Margin Top = 0.0 and Margin Bottom = 0.0

But the original document in MSWord shows margin top and bottom for each paragraph.Why couldn’t i get it? Clarify me if i am doing anything wrong

Hello
Thanks for your request. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. We will let you know once it is resolved.
But, I think, I can suggest you a workaround of this issue. You can save your DOCX document as DOC, and then get Paragraph Formatting, please see the following code:

// Open document
Document doc = new Document("C:\\Temp\\Title.docx");
// Convert DOCX to DOC to get access to formating.
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
doc.save(byteStream, SaveFormat.DOC);
InputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());
// Create com.aspose.words.Document from InputStream.
doc = new Document(inputStream);
// Get all paragraphs
NodeCollection nodeColl = doc.getChildNodes(NodeType.PARAGRAPH, true);
// Loop through all paragraphs
for (int i = 0; i <nodeColl.getCount(); i++)
{
    Paragraph paragraph = (Paragraph) nodeColl.get(i);
    System.out.println(paragraph.getParagraphFormat().getLeftIndent());
    System.out.println(paragraph.getParagraphFormat().getRightIndent());
    System.out.println(paragraph.getParagraphFormat().getSpaceAfter());
    System.out.println(paragraph.getParagraphFormat().getSpaceBefore());
}
// Using this code you can get default Paragraph formating
System.out.println(doc.getStyles().get(StyleIdentifier.NORMAL).getParagraphFormat().getLeftIndent());
System.out.println(doc.getStyles().get(StyleIdentifier.NORMAL).getParagraphFormat().getRightIndent());
System.out.println(doc.getStyles().get(StyleIdentifier.NORMAL).getParagraphFormat().getSpaceAfter());
System.out.println(doc.getStyles().get(StyleIdentifier.NORMAL).getParagraphFormat().getSpaceBefore());

Best refgards,

Hi,
Thanks for your reply. I have tried your code like.

System.out.println("Margin-Bottom = " + doc.getStyles().get(StyleIdentifier.NORMAL).getParagraphFormat().getSpaceAfter());

But still its producing the output “0.0” instead of “18pt” as in document.The Code works for doc format but how to get the result in docx format(as i need to handle both formats).

Hello
Thanks for your inquiry. You will be notified as soon as this problem is resolved. As a workaround you can try using the following code:

// Open document
Document doc = new Document("C:\\Temp\\margin.docx");
// Convert DOCX to DOC to fix the problem.
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
doc.save(byteStream, SaveFormat.DOC);
InputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());
// Create com.aspose.words.Document from InputStream.
doc = new Document(inputStream);
// Get all paragraphs
NodeCollection nodeColl = doc.getChildNodes(NodeType.PARAGRAPH, true);
// Loop through all paragraphs
for (int i = 0; i <nodeColl.getCount(); i++)
{
    Paragraph paragraph = (Paragraph) nodeColl.get(i);
    System.out.println(paragraph.getParagraphFormat().getLeftIndent());
    System.out.println(paragraph.getParagraphFormat().getRightIndent());
    System.out.println(paragraph.getParagraphFormat().getSpaceAfter());
    System.out.println(paragraph.getParagraphFormat().getSpaceBefore());
}

Best regards,

The issues you have found earlier (filed as WORDSNET-4782) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.