Free Support Forum - aspose.com

Add \e Switch to INDEX Field to Define Separator for Index Entry (XE Field) & Page Number or Cross References C# .NET | \z switch

Hi, I am creating field index XE and creating an index at the end of my document, but I am trying to align page numbers to the right of the page using dots or any character, but I cannot achieve, is it possible to do that?

Thanks, regards

@maik2,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

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 for .NET.

@awais.hafeez Thanks for your answer. I am attaching the zip file with the evidence you asked me for.It contains an input.html file that is the source file to convert to docx, output.docx is the html file already converted where you may see on last page, the index page numbers cannot be aligned to the right, expected.docx is how the index should look in the last page.

Please let me know if any further information is required.
Here is the attachment: aspose.zip (21.3 KB)

@maik2,

Please try running the following code:

Document doc = new Document("C:\\temp\\Aspose\\input.html");
DocumentBuilder builder = new DocumentBuilder(doc);
// first insert a few index enteries i.e. XE fields at appropriate places
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
// Then in the end insert INDEX field using the following field code
builder.InsertField("INDEX \\e \"	\" \\z \"1033\"");
doc.Save("C:\\temp\\Aspose\\20.9.docx");

@awais.hafeez that did it!! Thanks. I am just wondering what do all those parameteres in the index creation mean? Also, is it possible to give some formatting to the index? I mean font size, font name, if it is bold, italic or underlined?

Thanks in advance.

@maik2,

You can add the \e switch to your index field to indicate how you want MS Word to separate the index entry (or subentry) from the page number references or cross-references when it actually creates the index. So, this is used to define a separator. Secondly, I would say that the \z switch in INDEX field has something to do with localization or character sets. You may remove \\z \"1033\" from INDEX field.

Please ZIP and attach your input and expected DOCX files showing the desired output here for our reference. You can create this expected document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end. We will then investigate this particular scenario and provide you C# code to achieve the same by using Aspose.Words for .NET.

hi @awais.hafeez , here is the attachment with the expected result which is to apply a font formatting to the index created in the last page, the output example shows how the index is generated with a default font formatting so please let me know what should I do to achieve the expected result (font formatting or any formatting that can be applied to the created index)?

attachment : expected.zip (20.9 KB)

Thanks!

@maik2,

You can change Font formatting (Name, Size, Color etc) of text contained within INDEX field by using the following C# code:

Document doc = new Document("C:\\Temp\\expected\\output1.docx");

FieldIndex fieldIndex = null;
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldIndex)
    {
        fieldIndex = (FieldIndex)field;
        break;
    }
}

if (fieldIndex != null)
{
    Node currentNode = fieldIndex.Start;
    while (currentNode != null && currentNode != fieldIndex.End)
    {
        if (currentNode.NodeType == NodeType.Run)
        {
            Run run = (Run)currentNode;
            run.Font.Name = "Verdana";
            run.Font.Italic = true;
            run.Font.Size = 18;
            run.Font.Color = Color.Red;
        }
        currentNode = currentNode.NextPreOrder(currentNode.Document); ;
    }
}

doc.Save("C:\\temp\\expected\\20.9.docx");

Hi @awais.hafeez,
is it possible to customize the separator between item name and page number? for example in the code you shared "INDEX \e " " \z “1033"” the page separator are a sequence of dots until page numbers are to the right, can I set a different separator? for example I would like to use . . . . (dot space dot space and so on) or what are the separators available for this scenario?
Thanks, regards

@maik2,

I am afraid, this does not seem possible. A reliable way to right align numbers is setting a Tab but MS Word allows only four built-in Tab Leader types (none, dots, dashes, solid lines). You can also explicitly specify the separator of a certain length, like \e “. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .” to push the number to the right and thus avoid using a Tab.

Can you meet this requirement by using MS Word? If yes then please create your expected document by using MS Word and share it here for our reference. Please also list the complete steps that you performed in MS Word to create the expected document on your end. We will then further investigate into your issue and provide you more information.

Thanks @awais.hafeez, could you please share the code I should write to set the tab leader types ( none, dots, dashes, solid lines ) as MS Word? I have been trying to achieve that but no luck so far. Is it possible to also use the templates that MS Word has?

Thanks in advance, regards.

@maik2,

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