基本示例
以下是 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->setKeywords('my, key, word');
文檔資訊
您可以設定文檔資訊,如标題、建立者和公司名稱。請使用以下函數:
$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');
示範文稿屬性
您可以定義一些與示範文稿相關的屬性,如縮放或縮略圖
注釋
您可以通過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
本文為翻譯内容, 英文原文位址