How can i have correct 'SEQ' if i use my own headingStyle?

@alexey.noskov
here’s my code :

RunCollection runs = paragraph.getRuns();
runs.add(new Run(document, "图"));

paragraph.appendField("STYLEREF \"customHeading_1\" \\s");

runs.add(new Run(document, seperator));
paragraph.appendField("SEQ 图 \\* ARABIC \\s 1");

How to reset the numbering of image and table questions for each chapter. one of my friend have asked the same question yesterday, and you replied : Use different SEQ segment identifiers for each section to measure the new start number

{SEQ firstsection \* ARABIC \S 1}
{SEQ secondsection \* ARABIC \S 1 }
{SEQ thirdsection \* ARABIC \S 1}

but, can we just like before

paragraph.appendField("STYLEREF 1 \\s");
runs.add(new Run(document, seperator));
paragraph.appendField("SEQ 图 \\* ARABIC \\s 1");

because it seems like just use ‘"customHeading_1"’ rather than ‘1’, and the sequence becomes consistent for the whole document, rather than restart counting on a new chapter. I really wish to solve this problem

so the problem comes from ‘SEQ’ right ? then, is there a way, that i can restart the SEQ with the current chapter ? for example, the second image on chapter four, it shows ‘4-2’ and the third image on chapter 5, shows ‘5-3’


then i deleted the {SEQ …}

so, the only problem is , how can i restart the “SEQ 图 \* ARABIC \s 1” at a new chapter
“STYLEREF "customHeading_1" \s” is totally correct. it shows the correct chapter number

Here’s the code that i asked chatgpt. is it applicable ? is it possible ? sorry, it’s just so urgent to solve this problem

Document doc = new Document("input.docx");
for (Section section : doc.getSections())
{
    for (Paragraph para : section.getBody().getParagraphs())
    {
        if (para.getParagraphFormat().getStyleName().equals("customHeading_1"))
        {
            // Reset SEQ for the new chapter.
            Paragraph chapterMarker = new Paragraph(doc);
            chapterMarker.appendChild(new FieldStart(doc, FieldType.FIELD_SEQUENCE));
            chapterMarker.appendChild(new Run(doc, "图 \\r 1"));
            chapterMarker.appendChild(new FieldEnd(doc, FieldType.FIELD_SEQUENCE));
            section.getBody().insertBefore(chapterMarker, para);
        }
        if (/* 判断是否是图例标题 */)
        {
            para.appendField("SEQ 图 \\* ARABIC \\s 1");
        }
    }
}

@Madecho You can use STYLEREF field value as SEQ field identifier to restart numbering for each chapter. For example see the following code:

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

for (int i = 0; i < 5; i++)
{
    // Insert some heading that indicates start of chapter.
    builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
    builder.writeln("My chapter " + i);

    builder.getParagraphFormat().clearFormatting();
    // Insert style ref and SEQ fields.
    builder.insertField("STYLEREF \"Heading 1\"");
    builder.writeln();

    // Use SEQ field for each chapter. Use STYLEREF to construct SEQ field identifier.
    for (int j = 0; j < 5; j++)
    {
        Field seq = builder.insertField("SEQ");
        // Move DocumentBuilder inside SEQ field code
        builder.moveTo(seq.getSeparator());
        // Use STYLEREF as identifier
        builder.write(" \"");
        builder.insertField("STYLEREF \"Heading 1\"");
        builder.write("\" \\* Arabic \\s 1");
        builder.moveToField(seq, true);
        builder.writeln();
    }

    builder.writeln();
}

doc.updateFields();
doc.save("C:\\Temp\\out.docx");

out.docx (7.8 KB)

what if i could only use the custom headingStyle, is it still possible to achive that ?

@Madecho Sure, you can use any style in STYLEREF field. I used Heading 1 style for demonstration purposes.

1 Like

sorry alexey, I tried, but don’t know how to apply your code to my code , here’s my code , how can i adjust my code to achive your result

StringBuilder text = new StringBuilder();
for (Run run : paragraph.getRuns())
{
    text.append(run.getText());
}

String textRevised = text.toString().replaceFirst("图\\s*\\d*([-.]\\d*)*([::])?", "").trim();
paragraph.getRuns().clear();
StyleUtils.merge(paragraph.getParagraphFormat(), StyleIdentifier.INDEX_3, true);

Field seq = builder.insertField("SEQ");
builder.moveTo(seq.getSeparator());
builder.write(" \"");
builder.insertField("STYLEREF \"customHeading_1\"");
builder.write("\" \\* Arabic \\s 1");
builder.moveToField(seq, true);
builder.writeln();

RunCollection runs = paragraph.getRuns();

runs.add(new Run(document, "图"));

paragraph.appendField("STYLEREF \"customHeading_1\" \\s");

runs.add(new Run(document, seperator));
paragraph.appendField("SEQ 图 \\* ARABIC \\s 1");

Run spaceBar = new Run(document, "  "); // 两个空格
runs.add(spaceBar);
runs.add(new Run(document, textRevised));

because i haven’t used any DocumentBuilder before, so…

@Madecho You can use the following code:

builder.moveTo(paragraph);
builder.write("图");
builder.insertField("STYLEREF \"customHeading_1\" \\s");
        
Field seq = builder.insertField("SEQ");
builder.moveTo(seq.getSeparator());
builder.write(" \"");
builder.insertField("STYLEREF \"customHeading_1\"");
builder.write("\" \\* Arabic \\s 1");
builder.moveToField(seq, true);
builder.writeln();
    
builder.write(textRevised);

thanks @alexey.noskov , there’s another blackbox i want to know. your code works well, but in the previous code, mine is

  paragraph.appendField("STYLEREF \"customHeading_1\" \\s");
  runs.add(new Run(document, seperator));
  paragraph.appendField("SEQ 图 \\* ARABIC \\s 1");

so when i use

it could show all the images;
so how could i adjust the code now, to show all the code.

i have tired

    Field seq = builder.insertField("SEQ 图");
    builder.moveTo(seq.getSeparator());
    builder.write(" \"");
    builder.insertField("STYLEREF \"customHeading_1\"");
    builder.write("\" \\* Arabic \\s 1");

however the output is

how can i fix it ?

i can fix the toc field code ’ paragraph.appendField(“TOC \h \z \c "图"”);’ or fix the code above, i just don’t know which one to fix.

@Madecho In this case you should create a separate TOC for each SEQ field identifier, i.e. for each chapter. You should have something like this:

{ TOC \h \z \c "firstChaptertSeqId"}
{ TOC \h \z \c "secondChaptertSeqId"}
{ TOC \h \z \c "thirdChaptertSeqId"}
....

but i think the code

  builder.moveTo(paragraph);
  builder.write("图");
  builder.insertField("STYLEREF \"customHeading_1\" \\s");
          
  Field seq = builder.insertField("SEQ");
  builder.moveTo(seq.getSeparator());
  builder.write(" \"");
  builder.insertField("STYLEREF \"customHeading_1\"");
  builder.write("\" \\* Arabic \\s 1");
  builder.moveToField(seq, true);
  builder.writeln();
      
  builder.write(textRevised);

is applying one seq field identifier right ? then ? how can i create separate toc for each SEQ field identifier ?

or are these two things are completely conflict ? so i can only choose one to implement

@Madecho Currently you are using STYLEREF field value as SEQ field identifier. but with TOC you can not use it since TOC field location is not inside the chapter, i.e. under the style. So you can generate your own SEQ field identifiers for each chapter, field example, id1, id2… etc. Then you can use these identifiers in the TOC fields.

is there any other way that, i can use to keep the

   builder.moveTo(paragraph);
  builder.write("图");
  builder.insertField("STYLEREF \"customHeading_1\" \\s");
          
  Field seq = builder.insertField("SEQ");
  builder.moveTo(seq.getSeparator());
  builder.write(" \"");
  builder.insertField("STYLEREF \"customHeading_1\"");
  builder.write("\" \\* Arabic \\s 1");
  builder.moveToField(seq, true);
  builder.writeln();
      
  builder.write(textRevised);

unchanged, and don’t create many SEQ identifier, but display all the images ? like

cause we don’t want to create many seq identifier, then when we updatealltheField of document, it would pop up as much times as we create seq identifier

@Madecho The code to restart numbering for SEQ fields per chapter anyways creates a separate identifier for SEQ fields in each chapter, because value of STYLEREF field is used as an identifier and it’s value is different for each chapter. So if you generate your own SEQ identifiers for each chapter, there will not be any problems.