Merge pull request #84 from bskrtich/textrunimage
Ability to use images in a text run
This commit is contained in:
commit
4ad5cf004a
@ -109,6 +109,27 @@ class PHPWord_Section_TextRun
|
|||||||
return $link;
|
return $link;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a Image Element
|
||||||
|
*
|
||||||
|
* @param string $imageSrc
|
||||||
|
* @param mixed $styleFont
|
||||||
|
* @return PHPWord_Section_Image
|
||||||
|
*/
|
||||||
|
public function addImage($imageSrc, $style = null) {
|
||||||
|
$image = new PHPWord_Section_Image($imageSrc, $style);
|
||||||
|
|
||||||
|
if (!is_null($image->getSource())) {
|
||||||
|
$rID = PHPWord_Media::addSectionMediaElement($imageSrc, 'image');
|
||||||
|
$image->setRelationId($rID);
|
||||||
|
|
||||||
|
$this->_elementCollection[] = $image;
|
||||||
|
return $image;
|
||||||
|
} else {
|
||||||
|
trigger_error('Source does not exist or unsupported image type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get TextRun content
|
* Get TextRun content
|
||||||
*
|
*
|
||||||
|
|||||||
@ -106,6 +106,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
$this->_writeText($objWriter, $element, true);
|
$this->_writeText($objWriter, $element, true);
|
||||||
} elseif ($element instanceof PHPWord_Section_Link) {
|
} elseif ($element instanceof PHPWord_Section_Link) {
|
||||||
$this->_writeLink($objWriter, $element, true);
|
$this->_writeLink($objWriter, $element, true);
|
||||||
|
} elseif ($element instanceof PHPWord_Section_Image) {
|
||||||
|
$this->_writeImage($objWriter, $element, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -627,7 +629,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
* @param \PHPWord_Shared_XMLWriter $objWriter
|
* @param \PHPWord_Shared_XMLWriter $objWriter
|
||||||
* @param \PHPWord_Section_Image $image
|
* @param \PHPWord_Section_Image $image
|
||||||
*/
|
*/
|
||||||
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image)
|
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
|
||||||
{
|
{
|
||||||
$rId = $image->getRelationId();
|
$rId = $image->getRelationId();
|
||||||
|
|
||||||
@ -639,6 +641,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
$marginLeft = $style->getMarginLeft();
|
$marginLeft = $style->getMarginLeft();
|
||||||
$wrappingStyle = $style->getWrappingStyle();
|
$wrappingStyle = $style->getWrappingStyle();
|
||||||
|
|
||||||
|
if (!$withoutP) {
|
||||||
$objWriter->startElement('w:p');
|
$objWriter->startElement('w:p');
|
||||||
|
|
||||||
if (!is_null($align)) {
|
if (!is_null($align)) {
|
||||||
@ -648,6 +651,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
$objWriter->endElement();
|
$objWriter->endElement();
|
||||||
$objWriter->endElement();
|
$objWriter->endElement();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$objWriter->startElement('w:r');
|
$objWriter->startElement('w:r');
|
||||||
|
|
||||||
@ -697,7 +701,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
|
|
||||||
$objWriter->endElement();
|
$objWriter->endElement();
|
||||||
|
|
||||||
$objWriter->endElement();
|
if (!$withoutP) {
|
||||||
|
$objWriter->endElement(); // w:p
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
|
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
|
||||||
|
|||||||
61
samples/Sample_04_Textrun.php
Normal file
61
samples/Sample_04_Textrun.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
|
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
|
||||||
|
define('EOL', PHP_EOL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
define('EOL', '<br />');
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once '../Classes/PHPWord.php';
|
||||||
|
|
||||||
|
// New Word Document
|
||||||
|
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
|
||||||
|
$PHPWord = new PHPWord();
|
||||||
|
|
||||||
|
|
||||||
|
// Ads styles
|
||||||
|
$PHPWord->addParagraphStyle('pStyle', array('spacing'=>100));
|
||||||
|
$PHPWord->addFontStyle('BoldText', array('bold'=>true));
|
||||||
|
$PHPWord->addFontStyle('ColoredText', array('color'=>'FF8080'));
|
||||||
|
$PHPWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
|
||||||
|
|
||||||
|
// New portrait section
|
||||||
|
$section = $PHPWord->createSection();
|
||||||
|
|
||||||
|
// Add text run
|
||||||
|
$textrun = $section->createTextRun('pStyle');
|
||||||
|
|
||||||
|
$textrun->addText('Each textrun can contain native text, link elements or an image.');
|
||||||
|
$textrun->addText(' No break is placed after adding an element.', 'BoldText');
|
||||||
|
$textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText');
|
||||||
|
$textrun->addText(' Sample Link: ');
|
||||||
|
$textrun->addLink('http://www.google.com', null, 'NLink');
|
||||||
|
$textrun->addText(' Sample Image: ');
|
||||||
|
$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
|
||||||
|
$textrun->addText(' Here is some more text. ');
|
||||||
|
|
||||||
|
// Save File
|
||||||
|
echo date('H:i:s') , ' Write to Word2007 format' , EOL;
|
||||||
|
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
|
||||||
|
$objWriter->save(str_replace('.php', '.docx', __FILE__));
|
||||||
|
|
||||||
|
/* Text Run is not currently supported for ODText
|
||||||
|
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
|
||||||
|
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
|
||||||
|
$objWriter->save(str_replace('.php', '.odt', __FILE__));
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Text Run is not currently supported for RTF
|
||||||
|
echo date('H:i:s') , ' Write to RTF format' , EOL;
|
||||||
|
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
|
||||||
|
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Echo memory peak usage
|
||||||
|
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
|
||||||
|
|
||||||
|
// Echo done
|
||||||
|
echo date('H:i:s') , ' Done writing file' , EOL;
|
||||||
Loading…
x
Reference in New Issue
Block a user