Skip to content

Support Multiple headers #15

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

*.docx
*.xml
/.settings
/.buildpath
/.project
/docs
/docs
31 changes: 30 additions & 1 deletion src/Examples/HeaderFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
// New portrait section
$section = $PHPWord->createSection();

// Add header
// Add first page header
$header = $section->createHeader();
$header->firstPage();
$table = $header->addTable();
$table->addRow();
$table->addCell(4500)->addText('This is the header.');
$table->addCell(4500)->addImage('_earth.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));

// Add header for all other pages
$subsequent = $section->createHeader();
$subsequent->addText("Subsequent pages in Section 1 will Have this!");

// Add footer
$footer = $section->createFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'));
Expand All @@ -22,6 +27,30 @@
$section->addTextBreak();
$section->addText('Some text...');

// Create a second page
$section->addPageBreak();

// Write some text
$section->addTextBreak();
$section->addText('Some text...');

// Create a third page
$section->addPageBreak();

// Write some text
$section->addTextBreak();
$section->addText('Some text...');

// New portrait section
$section2 = $PHPWord->createSection();

$sec2Header = $section2->createHeader();
$sec2Header->addText("All pages in Section 2 will Have this!");

// Write some text
$section2->addTextBreak();
$section2->addText('Some text...');

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('HeaderFooter.docx');
Expand Down
File renamed without changes
32 changes: 24 additions & 8 deletions src/PHPWord/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class PHPWord_Section {
private $_elementCollection = array();

/**
* Section Header
* Section Headers
*
* @var PHPWord_Section_Header
* @var array
*/
private $_header = null;
private $_headers = array();

/**
* Section Footer
Expand Down Expand Up @@ -335,17 +335,33 @@ public function getElements() {
*/
public function createHeader() {
$header = new PHPWord_Section_Header($this->_sectionCount);
$this->_header = $header;
$this->_headers[] = $header;
return $header;
}

/**
* Get Header
* Get Headers
*
* @return PHPWord_Section_Header
* @return array
*/
public function getHeaders() {
return $this->_headers;
}

/**
* Is there a header for this section that is for the first page only?
*
* If any of the PHPWord_Section_Header instances have a type of
* PHPWord_Section_Header::FIRST then this method returns true. False
* otherwise.
*
* @return Boolean
*/
public function getHeader() {
return $this->_header;
public function hasDifferentFirstPage() {
$value = array_filter($this->_headers, function(PHPWord_Section_Header &$header) {
return $header->getType() == PHPWord_Section_Header::FIRST;
});
return count($value) > 0;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions src/PHPWord/Section/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ class PHPWord_Section_Header {
* @var int
*/
private $_rId;

/**
* Header type
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
private $_type = PHPWord_Section_Header::AUTO;

/**
* Even Numbered Pages Only
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const EVEN = 'even';

/**
* Default Header or Footer
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword

/**
* First Page Only
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const FIRST = 'first';

/**
* Header Element Collection
Expand Down Expand Up @@ -218,5 +247,34 @@ public function getElements() {
public function getHeaderCount() {
return $this->_headerCount;
}

/**
* Get Header Type
*/
public function getType() {
return $this->_type;
}

/**
* Reset back to default
*/
public function resetType() {
return $this->_type = PHPWord_Section_Header::AUTO;
}

/**
* First page only header
*/
public function firstPage() {
return $this->_type = PHPWord_Section_Header::FIRST;
}

/**
* Even numbered Pages only
*/
public function evenPage() {
return $this->_type = PHPWord_Section_Header::EVEN;
}

}
?>
7 changes: 3 additions & 4 deletions src/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,11 @@ public function save($pFilename = null) {
$_sections = $this->_document->getSections();

foreach($_sections as $section) {
$_header = $section->getHeader();
if(!is_null($_header)) {
$_headers = $section->getHeaders();
foreach ($_headers as $index => &$_header) {
$_cHdrs++;
$_header->setRelationId(++$rID);
$_headerCount = $_header->getHeaderCount();
$_headerFile = 'header'.$_headerCount.'.xml';
$_headerFile = 'header'.$_cHdrs.'.xml';
$sectionElements[] = array('target'=>$_headerFile, 'type'=>'header', 'rID'=>$rID);
$objZip->addFromString('word/'.$_headerFile, $this->getWriterPart('header')->writeHeader($_header));
}
Expand Down
11 changes: 8 additions & 3 deletions src/PHPWord/Writer/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo

private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) {
$_settings = $section->getSettings();
$_header = $section->getHeader();
$_headers = $section->getHeaders();
$_footer = $section->getFooter();
$pgSzW = $_settings->getPageSizeW();
$pgSzH = $_settings->getPageSizeH();
Expand All @@ -132,14 +132,19 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH

$objWriter->startElement('w:sectPr');

if(!is_null($_header)) {
foreach ($_headers as &$_header) {
$rId = $_header->getRelationId();
$objWriter->startElement('w:headerReference');
$objWriter->writeAttribute('w:type', 'default');
$objWriter->writeAttribute('w:type', $_header->getType());
$objWriter->writeAttribute('r:id', 'rId'.$rId);
$objWriter->endElement();
}

if($section->hasDifferentFirstPage()) {
$objWriter->startElement('w:titlePg');
$objWriter->endElement();
}

if(!is_null($_footer)) {
$rId = $_footer->getRelationId();
$objWriter->startElement('w:footerReference');
Expand Down