Complete Fill Paragraphs in Word DOCX Document with Dashes or Lines using C# .NET

Hi
I am trying to create a doc with several paragraphs and at the of each paragraph I want to complete the line with dashes if there’s a space like the following example:
This is paragraph this is paragraph
This is paragraph this is paragraph this
Is paragraph------------------------------------------

@Tato_tia,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Your expected document showing the desired output. You can create expected document by using MS Word.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Hi , thank you for your response
i created a program that tells the user to fill some forms and then the program will generates a word based on the filled informations and arrange them to pragraphs , but some paragraphs have an empty space at the last line , and because the documents have sensitive data , each empty space should filled so no one can add external data or change some infos , that’s why the empty space at the last line of each paragraph should be filled with dashes
i hope things are clear
thank again
res.zip (15.3 KB)

@Tato_tia,

Please tell if the following solution is acceptable for you?

Document document = new Document("E:\\Temp\\res\\INPUT.docx");
DocumentBuilder builder = new DocumentBuilder(document);

LayoutCollector collector = new LayoutCollector(document);
LayoutEnumerator enumerator = new LayoutEnumerator(document);

foreach (Paragraph paragraph in document.FirstSection.Body.GetChildNodes(NodeType.Paragraph, true))
{
    if (!string.IsNullOrEmpty(paragraph.ToString(SaveFormat.Text).Trim()) &&
        paragraph.ParagraphFormat.Alignment == ParagraphAlignment.Justify)
    {
        PageSetup ps = paragraph.ParentSection.PageSetup;

        enumerator.Current = collector.GetEntity(paragraph);

        double pageBody = ps.PageWidth - ps.RightMargin;
        AddLine(paragraph,
            enumerator.Rectangle.Bottom - ((enumerator.Rectangle.Bottom - enumerator.Rectangle.Top) / 2),
            enumerator.Rectangle.Left,
            pageBody - enumerator.Rectangle.Left);
    }
}

document.UpdatePageLayout();
document.Save("E:\\Temp\\res\\19.8.docx");

public static void AddLine(Paragraph para, double top, double left, double width)
{
    DocumentBuilder builder = new DocumentBuilder((Document)para.Document);
    builder.MoveTo(para);

    Shape line = new Shape((Document)para.Document, ShapeType.Line);
    line.StrokeColor = Color.Black;
    line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    line.WrapType = WrapType.None;

    builder.InsertNode(line);

    line.Width = width;
    line.Left = left;
    line.Top = top;
}

amazing
finally got a solution , but ther still a little probleme with paragraphs that have orientation right to left

@Tato_tia,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document containing paragraphs with orientation from right to left
  • Your expected document showing the desired output. You can create expected document by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Hi thank you for response
res.zip (15.2 KB)

@Tato_tia,

Please try specifying the following values in AddLine method:

AddLine(paragraph,
    enumerator.Rectangle.Bottom - ((enumerator.Rectangle.Bottom - enumerator.Rectangle.Top) / 2),
    ps.LeftMargin,
    enumerator.Rectangle.Left - ps.LeftMargin);

hi again
the code works fine but there is a little issue (please be patient with me) which is : some added lines have lot of space between them and the text i will give the output to see it .
please look at the text line before the two last lines.
thanks
have a good daytemp3.zip (15.7 KB)

@Tato_tia,

We have logged your problem in our issue tracking system. Your ticket number is WORDSNET-19105. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19105) have been fixed in this Aspose.Words for .NET 20.5 update and this Aspose.Words for Java 20.5 update.

hi
thank you for your update
is there any code to add because i tried it but it still the same issue

have a good day

hi again
after several tries i figured out that the problem is in the font i use which is simplified arabic , the code works great when i use times new roman or arial
have a nice day.
stay @ home

@Tato_tia,

You also need to enable the support for OpenType features. Please check the following article:

Hope, this helps.