Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Fri Apr 10, 2020 1:48 pm

The cross references in the example are set by labels. Can you create references by setting numbers?

Liyuanyuan
 
Posts: 13
Joined: Tue Mar 17, 2020 1:12 pm

Mon Apr 13, 2020 7:44 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
The following code demonstrates how to create cross-references based on paragraph number. If this is not what you want, please provide your desired output for further investigation.
Code: Select all
    //Create word document
    Document document = new Document();
    Section section = document.AddSection();
           
    //Create a list style
    ListStyle listStyle = new ListStyle(document, ListType.Numbered);
    listStyle.Name = "levelstyle";
    listStyle.Levels[0].PatternType = ListPatternType.Arabic;
    document.ListStyles.Add(listStyle);
   
    //Add the first paragarph
    Paragraph paragraph = section.AddParagraph();
    //Create a bookmark
    paragraph.AppendBookmarkStart("number_1");
    TextRange tr = paragraph.AppendText("Title 1");
    paragraph.AppendBookmarkEnd("number_1");
    //Apply list style
    paragraph.ListFormat.ApplyStyle("levelstyle");

    //Add the second paragarph
    paragraph = section.AddParagraph();
    //Create a bookmark
    paragraph.AppendBookmarkStart("number_2");
    tr = paragraph.AppendText("Title 2");
    paragraph.AppendBookmarkEnd("number_2");
    //Apply list style
    paragraph.ListFormat.ApplyStyle("levelstyle");

    section.AddParagraph();

    //Create cross-reference field to point to bookmark "number_1"
    Field field = new Field(document);
    field.Type = FieldType.FieldRef;
    field.Code = @"REF number_1 \r \h";
    //Insert field to paragraph
    Paragraph firstPara = section.AddParagraph();
    firstPara.ChildObjects.Add(field);
    FieldMark fieldSeparator = new FieldMark(document, FieldMarkType.FieldSeparator);
    firstPara.ChildObjects.Add(fieldSeparator);
    //Set the display text of the field
    tr = new TextRange(document);
    tr.Text = "1";
    firstPara.ChildObjects.Add(tr);
    FieldMark fieldEnd = new FieldMark(document, FieldMarkType.FieldEnd);
    firstPara.ChildObjects.Add(fieldEnd);

    //Create cross-reference field to point to bookmark "number_1"
    field = new Field(document);
    field.Type = FieldType.FieldRef;
    field.Code = @"REF number_2 \r \h";
    //Insert field to paragraph
    firstPara = section.AddParagraph();
    firstPara.ChildObjects.Add(field);
    fieldSeparator = new FieldMark(document, FieldMarkType.FieldSeparator);
    firstPara.ChildObjects.Add(fieldSeparator);
    //Set the display text of the field
    tr = new TextRange(document);
    tr.Text = "2";
    firstPara.ChildObjects.Add(tr);
    fieldEnd = new FieldMark(document, FieldMarkType.FieldEnd);
    firstPara.ChildObjects.Add(fieldEnd);

    //Update fields
    document.IsUpdateFields = true;

    //Save the file
    string output = "out.docx";
    document.SaveToFile(output, FileFormat.Docx);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1411
Joined: Tue Jul 09, 2019 2:22 am

Fri Apr 17, 2020 9:06 am

Hello,

Hope you are doing well.
Have you tested my code? Does it meet your needs?
Any feedback will be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1411
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc