Paragraph: space before and after

Hi,
Is it possible to update paragraph space before and after beetwen two specific bookmarks in the document?
Thanks,
PA

Hi Godson,

Thanks for your inquiry.

Sure, you can locate the position of the Paragraphs, you are requiring the spacing adjustment for, in the DOM hierarchy by opening your Word document with DocumentExplorer. Once you get the reference to those Paragraph nodes, you can then make use of ParagraphFormat.SpaceAfter and ParagraphFormat.SpaceBefore properties.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi Awais,
Could you please provide sample code of what you are talking about.
Thanks,
pa

Hi Godson,

Thanks for your inquiry. Could you please attach your input Word document along with the target output document here for testing? I will investigate your scenario on my side and provide you sample code.

Best Regards,

Hi Awais,
Please see attached test document.
It has two bookmarks b1 and b2.
I need programmatically update paragraph spacing between those two bookmarks.
p.ParagraphFormat.SpaceBefore = 6;
p.ParagraphFormat.SpaceAfter = 3;
Thanks,
pa

Hi
Godson,

Thanks for your inquiry. In the following screenshot, you can see the DOM structure of your test document when viewed with DocumentExplorer:

``

In order to programmatically update paragraph spacing between those two bookmarks, please use the following code snippet:

Document doc = new Document(@"c:\test\test.doc");
Bookmark b1 = doc.Range.Bookmarks["b1"];
Bookmark b2 = doc.Range.Bookmarks["b2"];
Paragraph paraOne = b1.BookmarkStart.ParentNode.NextSibling as Paragraph;
Paragraph paraTwo = b2.BookmarkStart.ParentNode.PreviousSibling as Paragraph;
paraOne.ParagraphFormat.SpaceAfter= 3;
paraOne.ParagraphFormat.SpaceBefore = 6;
paraTwo.ParagraphFormat.SpaceAfter = 3;
paraTwo.ParagraphFormat.SpaceBefore = 6; 
doc.Save(@"c:\test\out.docx");

I hope, this will help.

Best Regards,

Hi Awais,
I did run your code:

Aspose.Words.Bookmark b1 = docMain.Range.Bookmarks["b1"];
Aspose.Words.Bookmark b2 = docMain.Range.Bookmarks["b2"];
Paragraph paraOne = b1.BookmarkStart.ParentNode.NextSibling as Paragraph;
Paragraph paraTwo = b2.BookmarkStart.ParentNode.PreviousSibling as Paragraph;
paraOne.ParagraphFormat.SpaceAfter = 30;
paraOne.ParagraphFormat.SpaceBefore = 30;
paraTwo.ParagraphFormat.SpaceAfter = 30;
paraTwo.ParagraphFormat.SpaceBefore = 30; 

But it is only works for first and last paragraphs(see test2_result.doc).
I need for all paragraphs between bookmarks “b1” and “b2”
Please see two attached files: first test2.doc has two bookmarks and 6 paragraphs;
second is result after running code.
Code only update spaces of first and last paragraphs. I need for all.
Thanks,
pa

Hi Godson,

Thanks for your inquiry. I think, you can achieve this by using the following code snippet:

Document doc = new Document(@"c:\temp\test2.doc");
Bookmark b1 = doc.Range.Bookmarks["b1"];
Bookmark b2 = doc.Range.Bookmarks["b2"];
Paragraph paraOne = b1.BookmarkStart.ParentNode.NextSibling as Paragraph;
for (Node node = paraOne; node != b2.BookmarkStart.ParentNode; node = node.NextSibling)
{
    if (node.NodeType == NodeType.Paragraph)
    {
        Paragraph p = node as Paragraph;
        p.ParagraphFormat.SpaceAfter = 30;
        p.ParagraphFormat.SpaceBefore = 30;
    }
}
doc.Save(@"c:\temp\out.doc");

Please let me know if I can be of any further assistance.

Best Regards,

It works now.
Thank you,
Regards,
pa