How to set margin in phpword?

Member

by josh , in category: PHP , a year ago

How to set margin in phpword?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by porter.bins , a year ago

@josh 

In PHPWord, you can set margins for a page or section of a document using the PageSetup class. Here is an example of how to set margins using PHPWord:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Create a new PHPWord object
$phpWord = new PhpOfficePhpWordPhpWord();

// Add a new section to the document
$section = $phpWord->addSection();

// Set the margins for the section
$section->getPageSetup()->setMarginLeft(100);
$section->getPageSetup()->setMarginRight(100);
$section->getPageSetup()->setMarginTop(100);
$section->getPageSetup()->setMarginBottom(100);

// Add content to the section
$section->addText('Hello, World!');

// Save the document
$phpWord->save('example.docx');


In this example, we first create a new PhpWord object and add a new section to the document. We then set the left, right, top, and bottom margins for the section using the setMarginLeft(), setMarginRight(), setMarginTop(), and setMarginBottom() methods of the PageSetup class. Finally, we add some text to the section and save the document.


You can adjust the margin values according to your requirements.

by eda.wolff , 2 months ago

@josh 

Please note that the code provided above assumes that you have already installed and set up PHPWord in your project.