Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOC style inconsistent before/after updating field in Word #1768

Open
mdupont opened this issue Nov 29, 2019 · 0 comments
Open

TOC style inconsistent before/after updating field in Word #1768

mdupont opened this issue Nov 29, 2019 · 0 comments

Comments

@mdupont
Copy link
Contributor

mdupont commented Nov 29, 2019

Describe the Bug

  • Add a TOC with some styling to a document with $section->addTOC($fontStyle)
  • Generate a Word document
  • Open the document in Word
  • See $fontStyle being applied to the TOC
  • Right-click on the TOC, select "Update Field" then "Update entire table"
  • $fontStyle is no longer applied to the TOC. Instead, a style depending on the TOC level is applied: toc 1 for 1st level items, toc 2 for second level, etc

It seems the TOC $fontStyle is not very useful and the OOXML generation should assign toc 1, toc 2 styles right away.

Please note that in many cases a user will want to run the "Update field" operation on the generated document. In my case, it is needed to append page numbers, fix the rendering in the TOC of titles containing a line break, and display the automatic numbering of some titles in the TOC.

Tested with Word 16.29 on macOS.

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
require __DIR__ . '/vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setUpdateFields(true);

// New section
$section = $phpWord->addSection();

// Define styles
$fontStyle15 = array('size' => 15);
$fontStyle12 = array('size' => 12);
$fontStyle10 = array('size' => 10);
$phpWord->addFontStyle('toc 1', $fontStyle15);
$phpWord->addFontStyle('toc 2', $fontStyle10);

// Add text elements
$section->addTitle('Table of contents', 0);

// Add TOC
$toc = $section->addTOC($fontStyle12);
$section->addTextBreak(2);

// Add Titles
$section->addTitle('Title1', 1);
$section->addTitle('I am a Subtitle of Title 1', 2);
$section->addTitle('Title 2', 1);
$section->addTitle('I am a Subtitle of Title 2', 2);

// Generate a Word file
$phpWord->save("TocStylingTest.docx", 'Word2007');

// Open the file without updating the fields: the TOC will be in font size 12.
// Right-click on the TOC, choose "Update field" then "Update the entire table":
// The TOC will then have 1st level titles in font size 15
// and second level titles in font size 10

Expected Behavior

The appearance of the TOC remains the same before and after we update the TOC field with the "update entire table" operation.

Current Behavior

Before updating the TOC, the style defined in addTOC() is applied. After the field update, the style for the TOC level of each line (TOC1, TOC2...) is applied.

Context

Please fill in your environment information:

  • PHP Version: 7.3
  • PHPWord Version: 0.17.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant