Minimum working sample for text, textrun, and textbreak

This commit is contained in:
Ivan Lanin 2014-03-11 02:43:04 +07:00
parent c1b4b2e4a5
commit cf790b9f98
4 changed files with 169 additions and 1914 deletions

View File

@ -143,4 +143,15 @@ class PHPWord_IOFactory
throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType"); throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
} }
/**
* Loads PHPWord from file
*
* @param string $pFilename The name of the file
* @return PHPWord
*/
public static function load($pFilename, $readerType = 'Word2007') {
$reader = self::createReader($readerType);
return $reader->load($pFilename);
}
} }

View File

@ -36,9 +36,9 @@ abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
* *
* @var boolean * @var boolean
*/ */
protected $_readDataOnly = FALSE; protected $readDataOnly = TRUE;
protected $_fileHandle = NULL; protected $fileHandle = NULL;
/** /**
@ -47,7 +47,8 @@ abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
* @return boolean * @return boolean
*/ */
public function getReadDataOnly() { public function getReadDataOnly() {
return $this->_readDataOnly; // return $this->readDataOnly;
return TRUE;
} }
/** /**
@ -56,8 +57,8 @@ abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
* @param boolean $pValue * @param boolean $pValue
* @return PHPWord_Reader_IReader * @return PHPWord_Reader_IReader
*/ */
public function setReadDataOnly($pValue = FALSE) { public function setReadDataOnly($pValue = TRUE) {
$this->_readDataOnly = $pValue; $this->readDataOnly = $pValue;
return $this; return $this;
} }
@ -65,20 +66,20 @@ abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
* Open file for reading * Open file for reading
* *
* @param string $pFilename * @param string $pFilename
* @throws PHPWord_Reader_Exception * @throws PHPWord_Exception
* @return resource * @return resource
*/ */
protected function _openFile($pFilename) protected function openFile($pFilename)
{ {
// Check if file exists // Check if file exists
if (!file_exists($pFilename) || !is_readable($pFilename)) { if (!file_exists($pFilename) || !is_readable($pFilename)) {
throw new PHPWord_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist."); throw new PHPWord_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
} }
// Open file // Open file
$this->_fileHandle = fopen($pFilename, 'r'); $this->fileHandle = fopen($pFilename, 'r');
if ($this->_fileHandle === FALSE) { if ($this->fileHandle === FALSE) {
throw new PHPWord_Reader_Exception("Could not open file " . $pFilename . " for reading."); throw new PHPWord_Exception("Could not open file " . $pFilename . " for reading.");
} }
} }
@ -87,19 +88,17 @@ abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
* *
* @param string $pFilename * @param string $pFilename
* @return boolean * @return boolean
* @throws PHPWord_Reader_Exception * @throws PHPWord_Exception
*/ */
public function canRead($pFilename) public function canRead($pFilename)
{ {
// Check if file exists // Check if file exists
try { try {
$this->_openFile($pFilename); $this->openFile($pFilename);
} catch (Exception $e) { } catch (Exception $e) {
return FALSE; return FALSE;
} }
fclose ($this->fileHandle);
$readable = $this->_isValidFormat();
fclose ($this->_fileHandle);
return $readable; return $readable;
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,78 @@
<?php
// error_reporting(E_ALL );
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';
$files = array(
"Sample_01_SimpleText.docx",
"Sample_02_TabStops.docx",
"Sample_03_Sections.docx",
"Sample_04_Textrun.docx",
"Sample_05_Multicolumn.docx",
"Sample_06_Footnote.docx",
"Sample_07_TemplateCloneRow.docx",
"Sample_08_ParagraphPagination.docx",
"Sample_09_Tables.docx",
);
foreach ($files as $file) {
echo '<hr />';
echo '<p><strong>', date('H:i:s'), " Load from {$file} with contents:</strong></p>";
unset($PHPWord);
try {
$PHPWord = PHPWord_IOFactory::load($file);
} catch (Exception $e) {
echo '<p style="color: red;">Caught exception: ', $e->getMessage(), '</p>';
continue;
}
$sections = $PHPWord->getSections();
$countSections = count($sections);
$pSection = 0;
if ($countSections > 0) {
foreach ($sections as $section) {
$pSection++;
echo "<p><strong>Section {$pSection}:</strong></p>";
$elements = $section->getElements();
foreach ($elements as $element) {
if ($element instanceof PHPWord_Section_Text) {
echo '<p>' . htmlspecialchars($element->getText()) . '</p>';
} elseif ($element instanceof PHPWord_Section_TextRun) {
$subelements = $element->getElements();
echo '<p>';
if (count($subelements) > 0) {
foreach ($subelements as $subelement) {
if ($subelement instanceof PHPWord_Section_Text) {
echo htmlspecialchars($subelement->getText());
}
}
}
echo '</p>';
} elseif ($element instanceof PHPWord_Section_Link) {
echo '<p style="color: red;">Link not yet supported.</p>';
} elseif ($element instanceof PHPWord_Section_Title) {
echo '<p style="color: red;">Title not yet supported.</p>';
} elseif ($element instanceof PHPWord_Section_TextBreak) {
echo '<br />';
} elseif ($element instanceof PHPWord_Section_PageBreak) {
echo '<p style="color: red;">Page break not yet supported.</p>';
} elseif ($element instanceof PHPWord_Section_Table) {
echo '<p style="color: red;">Table not yet supported.</p>';
} elseif ($element instanceof PHPWord_Section_ListItem) {
echo '<p style="color: red;">List item not yet supported.</p>';
} elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage
) {
echo '<p style="color: red;">Image not yet supported.</p>';
} elseif ($element instanceof PHPWord_TOC) {
echo '<p style="color: red;">TOC not yet supported.</p>';
} elseif($element instanceof PHPWord_Section_Footnote) {
echo '<p style="color: red;">Footnote not yet supported.</p>';
}
}
}
}
}