PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.29k stars 2.7k forks source link

Reset Numbering in Multilevel Lists In /PHPWord/TemplateProcessor cloneBlock #2436

Open ayDavidGitHere opened 1 year ago

ayDavidGitHere commented 1 year ago

I'm using the PHPWord library to clone a block containing a multilevel numbered list in a DOCX document. However, I've noticed that the numbering inner levels in the cloned block continues from the original block, which is not the behavior I'm looking for. I'd like to reset the numbering in lists inside the new block.

Steps to Reproduce:

Clone a block containing a multilevel numbered list using the cloneBlock method. Observe that the numbering in the cloned block continues from the original block. Expected Behavior: I would like to reset the numbering in the lists inside the cloned block so that they start from the beginning (e.g., 1, 2, 3...).

Current Implementation:

function cloneBlockInDocx($docx_path) {
    // Load the Word document
    $phpword = new \PhpOffice\PhpWord\PhpWord();
    $template = $phpword->loadTemplate( $docx_path );

    $template->cloneBlock('Container-block', 2, true, true); //block contains lists

    // Save the modified document
    $new_docx_path = str_ireplace( ".docx", "_cloned.docx", $docx_path);
    $template->saveAs($new_docx_path);

    return $new_docx_path;
}

Sample Docx Result

BLOCK
1. Item 1
   a) Subitem 1a
   b) Subitem 1b
2. Item 2
   a) Subitem 2a

BLOCK
1. Item 1
   c) Subitem 1a
   d) Subitem 1b
2. Item 2
   b) Subitem 2a

BLOCK
1. Item 1
   e) Subitem 1a
   f) Subitem 1b
2. Item 2
   c) Subitem 2a

Desired Solution: I'm looking for guidance on how to reset the numbering in the inner lists inside the cloned block so that they start from the beginning. Any suggestions or code examples would be greatly appreciated.

BLOCK
1. Item 1
   a) Subitem 1a
   b) Subitem 1b
2. Item 2
   a) Subitem 2a

BLOCK
1. Item 1
   a) Subitem 1a
   b) Subitem 1b
2. Item 2
   a) Subitem 2a

BLOCK
1. Item 1
   a) Subitem 1a
   b) Subitem 1b
2. Item 2
   a) Subitem 2a
xinjiawei commented 10 months ago

I'm also working on a solution to this problem. Do you have some idea now?

ayDavidGitHere commented 10 months ago

I concluded there is no /PHPWord way to do it, I implemented a custom and rough solution. I unfortunately don't have that code available at this moment.