Forum


Replies: 6   Views: 819
Setdefaultfont method on existing document text
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by usman12  · 27-05-2022 - 10:40

Hi,

I am using setDefaultFont method to change the font family of the document by opening the template using CreateDocxFromTemplate method, but it does not work.

However, when I add new text and apply font it works fine but does not change the default font.

I am using the premium license.

Here is my whole script:



$docx = new \Phpdocx\Create\CreateDocxFromTemplate($tempFilePath);

$docx->setDefaultFont('Arial');

$docx->createDocx($new_file_path);

Thanks & Regards 

Posted by admin  · 27-05-2022 - 11:03

Hello,

The setDefaultFont method sets the default font in the theme scope of a DOCX document. This is an internal setting that sets the default font to be used in a DOCX unless other font is applied to the content; this method is useful when creating a DOCX from scratch. Default styles, custom styles and internal styles overwrite this internal setting.

This method doesn't change the font used by contents if some other style is applied, only that specific internal theme setting. If you need to change styles in a DOCX template you need to use DOCXCustomizer, available in Premium licenses.

The setDocumentDefaultStyles method changes default styles of a DOCX document.

Which method to be used depends on the style type to be changed.

Regards.

Posted by usman12  · 27-05-2022 - 12:33

Thanks for your quick response.

I have tried to use setDocumentDefaultStyles but it didn't work.

my updated code:

$docx = new \Phpdocx\Create\CreateDocxFromTemplate($tempFilePath);

$style = array(
    'color' => 'ff0000',
    'fontSize' => 14,
    'font'=>'Arial'
);

$docx->setDocumentDefaultStyles($style);

$docx->createDocx($new_file_path);

I have opened the CreateDocxFromTemplate.php class and could not find the method setDocumentDefaultStyles but I found it in CreateDocx which is not helpful in my case because I have to modify the font of the template. 

Thanks & Regards

 

Posted by usman12  · 27-05-2022 - 12:39

ok, I can see CreateDocxFromTemplate is being extended from CreateDocx so please check my code and let me know what is wrong?

Posted by admin  · 27-05-2022 - 12:41

Hello,

Please note that as explained in our previous reply, the setDocumentDefaultStyles method changes the default styles of a DOCX document (w:pPrDefault and rPrDefault tags in docDefaults style [http://officeopenxml.com/WPstyleDefaults.php]). This method doesn't  change custom styles or inline styles.

If the styles to be changed are applied using a custom/type style or as inline styles, DOCXCustomizer must be used.

In a DOCX document the styles work following a hierarchy: theme -> default styles -> type styles -> custom styles -> inline styles. As you are working with a DOCX template you need to use the proper method to update the needed style. In the package you can find many samples that illlustrate how to use DOCXCustomizer.

Also note that CreateDocxFromTemplate extends CreateDocx, so all methods available in CreateDocx are also available using CreateDocxFromTemplate.

Regards.

Posted by usman12  · 27-05-2022 - 13:32

ok, I have used the following code which works fine when I put 'type' => 'paragraph' but when I put 'type' =>  '*' then it does not work so how I can apply the font to all content? Please paste any code example if you can.

        $docx = new \Phpdocx\Create\CreateDocxFromTemplate($tempFilePath);

        $referenceNode = array(
            'type' => 'paragraph'
        );

        $docx->customizeWordContent($referenceNode,
            array(
                'font' => 'Times New Roman'
            )
        );


        $docx->createDocx($new_file_path);

Thanks for your patience and for helping me.

Regards,

Posted by admin  · 27-05-2022 - 14:51

Hello,

On https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP you can read the supported styles for each element. The font style is supported in paragraph and run elements.

Also, note this method includes the target option:

target string document (default), style, lastSection, header, footer.

By default, the target is document, but you can also set the following targets: style, header, footer.

Please check the samples included in the package (examples/DOCXCustimizer folder), that illustrate how to use DOCXCustomizer.

Regards.