How do I implement cross-referencing of custom heading styles in code?

In word, I customized the style based on the heading, but when I want to use cross-reference in the body, I can’t see the heading, and it’s
empty. How can I implement my requirements with the code?
My operation steps are as shown in the figure
, and the file is a test document

@lovecomputer Cross-reference in MS Word document is inserted as REF field to a hidden bookmark. So to insert cross-reference, you should first wrap the required content into a bookmark. For example see the following code:

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

// Bookmarks with name that starts with underscore are hidden in MS Word. 
string bkName = "_someBookmark";

builder.StartBookmark(bkName);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Write("Some heading");
builder.EndBookmark(bkName);
builder.Writeln();
builder.ParagraphFormat.ClearFormatting();
// Insert REF field to the inserted bookmark.
builder.InsertField("REF " + bkName);

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

Go ahead, if it is in Word, how can I implement a cross-reference custom heading style?Don’t see a custom heading in the cross-reference dialog,See only basic heading styles

@lovecomputer I am afraid, I do not see any way to add paragraphs with custom style into the cross-reference dialog. The only possible way is adding a bookmarks to these paragraphs and then adding reference to these bookmarks.

I have 2 word files and want to copy the style of one file to another file, but after copying, I find that there is an extra serial number?

@lovecomputer Unfortunately, it is not quite clear what the problem is. Could you please elaborate it in more details? As I can see styles are properly copied into the destination document.
Please note, when styles are copied from a template to a document, like-named styles in the document are redefined to match the style descriptions in the template. Unique styles from the template are copied to the document. Unique styles in the document remain intact.

The reminder is correct, fixed because the style base of the original text is also caused by the title

1 Like

I have a list of unilateral parentheses in my file,
test.docx (71.1 KB)

when I want to get the list string with this line of code, only the first line is correct,paragraph.ListLabel.LabelString=“1)”, the next 3 lines are custom multi-level numbering forms, and the string obtained is bilateral parentheses,paragraph.ListLabel.LabelString="(1), I don’t know why? How can it be solved?

@lovecomputer Unfortunately, I cannot reproduce the problem on my side. I used the following code for testing:

Document doc = new Document(@"C:\Temp\in.docx");
doc.UpdateListLabels();
foreach (Paragraph p in doc.FirstSection.Body.Paragraphs)
{
    if (p.IsListItem)
        Console.WriteLine(p.ListLabel.LabelString);
}

The output is correct:

1)
1)
2)
3)
4)

There are Bulleted list, Numbered List and Multi-level list in word, my questions are:

  1. I want to create a new number through the code, and the numbering form is (1, (2, (3… Form;
  2. How to get the existing numbering format in the word numbering library, such as the existing numbering format is a), b), c), etc,I would like to get the number of the red circle 1、2、3
    Note: I want to create a new Numbered List, which is not a multi-level list

@lovecomputer Please see our documentation to learn how to work with lists:
https://docs.aspose.com/words/net/working-with-lists/

  1. You can create a list from predefines template using Lists.Add method and specifying the appropriate ListTemplate.

  2. You can use ListLevel.NumberFormat property to get or set number format of the list level.

I used the following code to create a new single-level number(Numbered List), but it was still in the form of a multi-level list after completion.

 ListLevel listLevel = listdemo.ListLevels[0];
 listLevel.NumberFormat = "(\x0000)ab";
 listLevel.NumberStyle = NumberStyle.Arabic;
 listLevel.StartAt = 8;

 builder.Writeln("List 1 starts below:");
 builder.ListFormat.List = listdemo;
 builder.Writeln("Item 1");
 builder.Writeln("Item 2");
 builder.ListFormat.RemoveNumbers();
 doc.Save(@"out.docx");

In the output file, when you right-click on the numbered line, select the adjustment list indent function, and the multi-level categories will appear, But I want to create a single-level list(Numbered List)

@lovecomputer I am afraid there is no way to create a single level list through public API.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26149

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

word can be customized multi-level numbering, multi-level numbering has a “TAB position to add” option, how to prohibit this option, such as the red line of the option part

@lovecomputer You can adjust tab positions using ListLevel.TabPosition property. Also, you can use ListLevel.TrailingCharacter to use another character between the number and list item text instead of tab.

I’m not trying to change another character between the number and list item text, but I want to do not select this option(TAB position to add)

@lovecomputer The mentioned option clears the list level’s tab. I am afraid there is no public API to clear tab for ListLevel. Currently you can only clear tab stop of a particular paragraph.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26150

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

There is a test document, in which the paragraph structure is as follows: paragraph text, figure, paragraph text, 2 blank lines, figure, software(DocumentExplorer.exe) test found that the obtained paragraph structure is not in the same order as in the document. The paragraph structure obtained by the software is as follows: paragraph text, paragraph text, figure, figure, and the 2 blank lines in the middle of the text and figure are not found, what is the reason?

@lovecomputer Shapes in your document are floating, so their visual position can differ from the position in the document structure. You can unzip DOCX document and explore document.xml, you will see the structure is the same as in Aspose.Words.

I want to implement the judgment of a paragraph with only line breaks(return character), and at the same time require that the front neighbor of the paragraph is not tabular form, and the back neighbor is not tabular form, the paragraph that meets these 3 conditions, I use this function PreviousSibling and NextSibling, if the hierarchy order is not right, can not judge the paragraph that meets the above conditions, whether there are other scientific methods, to achieve my needs