How to add a relative width line to the end of a paragraph

Hello,
I need to process an existing Word file immediately after a simple mail merge has been performed. The purpose is to “close out” all paragraphs as soon as all the fields have been replaced in the mail merge.
To be more precise, I have to iterate each paragraph in each section after the mail merge and then modify it in order to ensure that the space between the last character in that paragraph and the right margin is filled. This can be done either with a horizontal line or a sequence of “-” characters of the appropriate length (considering the free space between the current horizontal position at the end of the paragraph’s text and the right margin). The line would have to be vertically centered relative to that text at the end of the paragraph, as indicated by the “----” fake dashed line possibility I mentioned.
I am struggling to find a way to do this with Aspose, and it is a decisive factor in whether to use your product, as it otherwise seems to fulfill all of our requirements. Any help would be greatly appreciated.
Thanks in advance.

Hi

Thanks for your request. I think you can achieve this using TabStops. Please try using the following code:

// Open document
Document doc = new Document(@"Test135\in.doc");
// Loop through all section in the document
foreach(Section sect in doc.Sections)
{
    // Calculate width of page in the current section
    double pageWidth = sect.PageSetup.PageWidth - sect.PageSetup.LeftMargin - sect.PageSetup.RightMargin - sect.PageSetup.Gutter;
    // Loop through all paragraphs in the current section
    foreach(Paragraph par in sect.Body.Paragraphs)
    {
        // Remove all tab stops in the paragraph
        par.ParagraphFormat.TabStops.Clear();
        // add tab stop to the end of the page
        par.ParagraphFormat.TabStops.Add(pageWidth, TabAlignment.Right, TabLeader.Dashes);
        // Insert tab at the end of the paragraph
        Run run = new Run(doc, ControlChar.Tab);
        par.AppendChild(run);
    }
}
// Save output docuemnt
doc.Save(@"Test135\out.doc");

Hope this helps.
Best regards.

Excellent. I ran a quick test with your code, and the only thing I had to add to suit our needs was a simple check to skip empty paragraphs (i.e. those with zero runs). As far as I can tell, your reply solves our problem. Thank you very much!

Turns out there is a problem with this approach, albeit perhaps not caused by Aspose.Words. Our ultimate goal is to produce a PDF, so we’re using Aspose.Pdf to convert the modified Doc via XML. Unfortunately instead of “----” what comes out in the PDF is something like “_ _ _ _ " or "___”. Whichever kind of tab leader I try works fine in Words, but I can’t seem to get it vertically centered in the resulting PDF. Any ideas? Thanks again.

Hi

Thanks for your request. Currently Aspose.Words allows converting to PDF without using Aspose.Pdf. Please see the following links for more information:
https://docs.aspose.com/words/net/convert-a-document-to-pdf/
So you can just try using this new method to convert your Word documents to PDF. This feature is available starting from Aspose.Words 6.0.0.
Best regards.

That solved it. Thank you.