基本示例
以下是 PHPPresentation 库的一个基本示例。更多示例请参见 samples 文件夹。
require_once 'src/PhpPresentation/Autoloader.php';
\PhpOffice\PhpPresentation\Autoloader::register();
$objPHPPresentation = new PhpPresentation();
// Create slide
$currentSlide = $objPHPPresentation->getActiveSlide();
// Create a shape (drawing)
$shape = $currentSlide->createDrawingShape();
$shape->setName('PHPPresentation logo')
->setDescription('PHPPresentation logo')
->setPath('./resources/phppresentation_logo.gif')
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
$shape->getShadow()->setVisible(true)
->setDirection(45)
->setDistance(10);
// Create a shape (text)
$shape = $currentSlide->createRichTextShape()
->setHeight(300)
->setWidth(600)
->setOffsetX(170)
->setOffsetY(180);
$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
$textRun->getFont()->setBold(true)
->setSize(60)
->setColor( new Color( 'FFE06B20' ) );
$oWriterPPTX = IOFactory::createWriter($objPHPPresentation, 'PowerPoint2007');
$oWriterPPTX->save(__DIR__ . "/sample.pptx");
$oWriterODP = IOFactory::createWriter($objPHPPresentation, 'ODPresentation');
$oWriterODP->save(__DIR__ . "/sample.odp");
文档信息
您可以设置文档信息,如标题、创建者和公司名称。请使用以下函数:
$properties = $objPHPPresentation->getProperties();
$properties->setCreator('My name');
$properties->setCompany('My factory');
$properties->setTitle('My title');
$properties->setDescription('My description');
$properties->setCategory('My category');
$properties->setLastModifiedBy('My name');
$properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));
$properties->setModified(mktime(0, 0, 0, 3, 14, 2014));
$properties->setSubject('My subject');
$properties->setKeywords('my, key, word');
演示文稿属性
您可以定义一些与演示文稿相关的属性,如缩放或缩略图
注释
您可以通过setCommentVisible方法定义演示文稿是否显示注释。
$oPresentation = new PhpPresentation();
$oProperties = $oPresentation->getPresentationProperties();
// Get the display for comment
var_export($oProperties->isCommentVisible());
// Output : false
// Enable the display for comment
$oProperties->setCommentVisible(true);
// Get the display for comment
var_export($oProperties->isCommentVisible());
// Output : true
最后查看
您可以使用setLastView方法定义演示文稿的最后查看。
$oPresentation = new PhpPresentation();
$oProperties = $oPresentation->getPresentationProperties();
// Get the last view of the presentation
echo $oProperties->getZoom();
// Output : PresentationProperties::VIEW_SLIDE
// Set the last view of the presentation
$oProperties->setLastView(PresentationProperties::VIEW_NOTES);
// Get the last view of the presentation
echo $oProperties->getZoom();
// Output : PresentationProperties::VIEW_NOTES
缩略图
您可以使用setThumbnailPath方法定义演示文稿的缩略图。
$oPresentation = new PhpPresentation();
$oProperties = $oPresentation->getPresentationProperties();
// Set path of the thumbnail
$oProperties->setThumbnailPath(__DIR__.'\resources\phppowerpoint_logo.gif');
// Get path of the thumbnail
echo $oProperties->getThumbnailPath();
缩放
您可以使用setZoom方法定义演示文稿的缩放比例。
$oPresentation = new PhpPresentation();
$oProperties = $oPresentation->getPresentationProperties();
// Get zoom of the presentation
echo $oProperties->getZoom();
// Output : 1
// Set zoom of the presentation (3 = 300%)
$oProperties->setZoom(3);
// Get zoom of the presentation
echo $oProperties->getZoom();
// Output : 3
本文为翻译内容, 英文原文地址