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.

I saw the ticket status changed to closed. is it already fixed?

@maik2,

WORDSNET-21131 is closed now. It is actually not possible to set a custom page separator and still have the page numbers right aligned. However, about your last question:

It is as simple as (for new document):

var doc = new Document();
var builder = new DocumentBuilder(doc);

builder.InsertField("INDEX \\e \"\t\" \\z \"1033\"");

var tabStop = new TabStop();
tabStop.Alignment = TabAlignment.Right;
tabStop.Leader = TabLeader.Dots;    // Or any other leader type.

builder.ParagraphFormat.TabStops.Add(tabStop);

or, for an existing field:

field.PageNumberSeparator = "\t";

and set a tab stop for the parent paragraph similarly to above.

To avoid setting tab stop formatting for every containing paragraph, you can also set tab stops for the Index1 - Index9 styles.

As to the second part of the question (use Word templates), you might be interested in the API that we recently implemented:

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

Hi @awais.hafeez I tested the solution proposed using the TabStops but I was not able to achieve the desired result. Is there any issue on the code?

Thanks, regards

@maik2,

Did 20.11 version of Aspose.Words not resolve your issue? Can you please summarize what problem(s) are you still getting on your end? Please provide 1) source word document, 2) Aspose.Words generated output document, 3) the expected document showing the desired result (create this manually using ms word etc) and also 4) create a standalone simple console application (source code without compilation errors) that helps us reproduce your current problem on our end and attach these resources here for testing. We’ll then investigate your issue further and provide you more information.

Hi @awais.hafeez please find https://1drv.ms/u/s!AltXSY5rsFdWgr0uwsMmjh4x4zxfxw?e=aN9dHG what you ask me to reproduce the issue.
input.html is the source file I use to generate the word document.
current.docx is the current output generated by aspose.words 20.12
expected.docx is the expected result created using MS Word

Index is on the first page, the expected is a leader separator with dashes not dots

Please let me know if you are able to reproduce it, thanks.

Hi @awais.hafeez also, having in consideration this thread is it possible to create multiple indexes with different title? For example:
People:
person1…1-4-6
person2…3

Places:
place1…1-6
place2…1

and so on. Please let me know, thanks!

@maik2,

We have logged these details in our issue tracking system and will keep you posted here on further updates.