Cursor Position in Paragraph

Hi
I am using Visual Studio 2013 and Aspose Words 16.10.

As part of the document being generated by my code, I need to generate a custom table of contents. This basically involves setting a style for each paragraph, inserting the title text, a tab with a right alignment and dots leader, and then the page number. Pretty simple.

There’s just one problem. The style (which is configurable) can contain a hanging indent, and if the title of the contents item is short enough, Word treats the hanging indent as a tab stop, causing the page number to be displayed in the wrong place (far to the left of where it should be).

What I want to be able to do, is detect when this happens, and insert an extra tab character, pushing the page number to the correct position on the page. Naturally, my first thought was to detect the location of the cursor at that moment, and act accordingly. But I’m unable to determine the cursor position.

Can anyone provide some assistance as to how to do this? Can anyone thing of another workaround to this problem? I can’t use a table because of the requirement for leading dots, and the removal of the hanging indent is not an option either.

Thanks
Chris.

Hi Chris,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document showing the undesired behavior.
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi
It’s not that the behaviour is incorrect, just that I need to detect a particular situation - the initial text being too short - and deal with it, and I wanted to know how to detect that situation reliably. There is no “input” Word document - I’m generating a document from data in a database.

Here is some code:

LoadLicence();
DocumentBuilder doc = new DocumentBuilder();
doc.PageSetup.PaperSize = PaperSize.A4;
doc.Font.Name = "Palatino Linotype";
doc.Font.Size = 11;
// Hanging indent…
doc.ParagraphFormat.LeftIndent = 31;
doc.ParagraphFormat.FirstLineIndent = -31;

double rightMargin = doc.PageSetup.PageWidth - doc.PageSetup.LeftMargin - doc.PageSetup.RightMargin;
doc.ParagraphFormat.TabStops.Add(rightMargin, TabAlignment.Right, TabLeader.Dots);

doc.Write("This title renders correctly");
doc.Write("\t");
doc.Writeln("CORRECT!");
// Because of the hanging indent, and the shortness of the text before the tab, the text after the
// tab is not aligning as desired. The behaviour is correct, just undesirable. To get around it,
// I need to detect the short text, and output an extra tab character before the text after.
doc.Write("Fees");
doc.Write("\t");
doc.Writeln("WRONG - SHOULD ALIGN TO THE RIGHT");

string fileName = "Sample.docx";
doc.Document.Save(fileName, SaveFormat.Docx);

This will generate a simple Word document demonstrating the difference. All I really need is a method to detect the shortness of the initial text (before the tab), so I can output an extra tab to push the cursor to the correct tab stop.

Chris

Hi Chris,

I have just added a new paragraph at the end that shows the desired behavior (see attachment).

Document doc = new Document(MyDir + @"Samples\Sample(Generated).docx");
Paragraph newPara = new Paragraph(doc);
TabStop tab = new TabStop(6.27 * 72, TabAlignment.Right, TabLeader.Dots);
newPara.ParagraphFormat.TabStops.Add(tab);
newPara.AppendChild(new Run(doc, "Fees " + ControlChar.Tab + "WRONG - SHOULD ALIGN TO THE RIGHT"));
doc.LastSection.Body.InsertAfter(newPara, doc.LastSection.Body.LastParagraph);
doc.Save(MyDir + @"Samples\16.11.0.docx");

Hope, this helps.

Best regards,

Hi Awais

Thanks for your reply.
However, this does not fix my problem, because it does not include the hanging indent, which is a requirement for this document.

I have managed to put together a suitable solution to the problem now, using classes from the Aspose.Words.Layout namespace:

public RectangleF GetLayoutRectangle(Node node)
{
    LayoutCollector collector = new LayoutCollector(Document);
    LayoutEnumerator enumerator = new LayoutEnumerator(Document);

    object renderObject = collector.GetEntity(node);
    enumerator.Current = renderObject;
    return enumerator.Rectangle;
}

I then compare the location of the rectangle to the desired location of the tab stop, and if it comes up short, I output another tab:

RectangleF location = GetLayoutRectangle(documentBuilder.CurrentParagraph);
if (location.X < RightMargin + documentBuilder.CurrentSection.PageSetup.LeftMargin - 10)
    documentBuilder.Write("\t");

This seems to have the desired effect.

Thanks for your time.
Chris

Hi Chris,

It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,