Merge pull request #42 from RLovelett/multiple-headers

Support Multiple headers
This commit is contained in:
Progi1984 2013-12-15 06:55:03 -08:00
commit 72977545bd
5 changed files with 123 additions and 16 deletions

View File

@ -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
@ -345,17 +345,33 @@ class PHPWord_Section {
*/
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 getHeader() {
return $this->_header;
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 hasDifferentFirstPage() {
$value = array_filter($this->_headers, function(PHPWord_Section_Header &$header) {
return $header->getType() == PHPWord_Section_Header::FIRST;
});
return count($value) > 0;
}
/**

View File

@ -49,6 +49,35 @@ class PHPWord_Section_Header {
*/
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
*
@ -222,5 +251,34 @@ class PHPWord_Section_Header {
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;
}
}
?>

View File

@ -114,12 +114,11 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter {
$_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));
}

View File

@ -117,7 +117,7 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base {
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();
@ -132,14 +132,19 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base {
$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');

View File

@ -7,13 +7,18 @@ $PHPWord = new PHPWord();
// 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'));
@ -22,6 +27,30 @@ $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'))
$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');