Forum


Replies: 9   Views: 331
How to reduce space between one line break
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 fairshareitservices  · 25-07-2024 - 10:31

I have a variable of type text and which has <br> tag inside it, so when code finds <br> tags it breaks the sentence using below code
 

$break = new WordFragment($docx);

    $break->addBreak();
 

if (!empty($lineBreakText)) {

                    $text[] = array(

                        'text' => $lineBreakText

                    );

                }

               $text[] = $break;
But this line break gets added by default with font 'Microsoft Sans Serif' with size 11. I want it to make it as 'Arial' with size 6.

Posted by admin  · 25-07-2024 - 11:33

Hello,

You can add a text (for example an empty text) with the font family and size you want to apply immediately before adding the soft break.

Please note that MS Word ignores this specific case in most cases, so the best approach would be adding a paragraph (hard) break with the styles (font family, font size, spacing...) you want to use instead of a soft break.

Regards.

Posted by fairshareitservices  · 26-07-2024 - 06:58

Okay it would be great if you could provide an example for both adding text with the font family and size you want to apply immediately before adding the soft break and also for adding a paragraph (hard) with the styles (font family, font size, spacing...) 

Posted by admin  · 26-07-2024 - 10:23

Hello,

Please check the samples included in the package, that illustrate how to use all methods available in phpdocx. You need to use the addText method with the styles you want to apply.

For example, to add an empty paragraph with custom spacing, font family, and font size styles:

$docx->addText('Content 1', array('spacingBottom' => 0));
$docx->addText('', array('fontSize' => 6, 'font' => 'Arial', 'spacingBottom' => 4, 'spacingTop' => 4));
$docx->addText('Content 2');

Or to add a specific text style before a soft break:

$breakFragment = new WordFragment($docx);
$breakFragment->addBreak();

$runs = array();
$runs[] = array('text' => 'Content 1');
$runs[] = array('text' => ' ', 'fontSize' => 6, 'font' => 'Arial');
$runs[] = $breakFragment;
$runs[] = array('text' => 'Content 2');

$docx->addText($runs);

Regards.

Posted by fairshareitservices  · 30-07-2024 - 10:08

$docx->addText('', array('fontSize' => 6, 'font' => 'Arial', 'spacingBottom' => 4, 'spacingTop' => 4)); is not working as expected. 

$breakFragment = new WordFragment($docx);
$breakFragment->addBreak();

$runs = array();
$runs[] = array('text' => 'Content 1');
$runs[] = array('text' => ' ', 'fontSize' => 6, 'font' => 'Arial');
$runs[] = $breakFragment;
$runs[] = array('text' => 'Content 2');

$docx->addText($runs);
Also adds Microsoft Sanserif 11 font size. So how to reduce this size?

Posted by admin  · 30-07-2024 - 10:14

Hello,

You can also set the styles before and after the break:

$runs = array();
$runs[] = array('text' => 'Content 1');
$runs[] = array('text' => ' ', 'fontSize' => 6, 'font' => 'Arial');
$runs[] = $breakFragment;
$runs[] = array('text' => ' ', 'fontSize' => 6, 'font' => 'Arial');
$runs[] = array('text' => 'Content 2');

$docx->addText($runs);

Also please note as explained in a previous reply:

You can add a text (for example an empty text) with the font family and size you want to apply immediately before adding the soft break.

Please note that MS Word ignores this specific case in most cases

You can check the same if you generate the DOCX manually using MS Word. MS Word includes some default behaviors that can't be changed.

There's no other approach than the detailed ones from our previous reply to handle break styles and spacings.

Regards.

Posted by fairshareitservices  · 30-07-2024 - 10:45

Okay. When I use \n with instead of break it works with the replaceVariableByText() method. For whne there is a bold tag in the sentence we use below code
 

$docx = new CreateDocx();

    $wf = new WordFragment($docx);
 

 $text[] = array(

                    'text' => $partialInputExploded[$i],

                    'bold' => true,

                    'fontSize' => 5.5,

                    'font' => 'Arial',

                );
$wf->addText($text, array('font' => 'Arial', 'fontSize' => 7)); and then use replaceVariableByWordFragment(array($placeHolderName => $fomattedWordFragment), array('type' => 'inline')); So for this we \n add the sentence to new line but \n remains same in the generated output

Posted by admin  · 30-07-2024 - 10:52

Hello,

To replace the supported line breaks:

'\n\r', '\r\n', '\n', '\r', "\n\r", "\r\n", "\n", "\r"

when adding contents in the supported methods, you need to enable the parseLineBreaks option (the default value of this option is false). This option replaces the line breaks with <w:br/> tags in the document.

Regards.

Posted by fairshareitservices  · 30-07-2024 - 11:55

Okay. So how to handle when text has both <b></b> tags and <br>? This means if it has bold text should be bold and the <br> tag is present then need to add a statement after the <br> tag to the new line. 

Posted by admin  · 30-07-2024 - 12:12

Hello,

You can add line breaks and styles when adding text content (the same approach can be used with WordFragments):

$text = "A text with styles and \nbreaks \nand more contents.";
$paragraphOptions = array(
    'bold' => true,
    'font' => 'Arial',
    'parseLineBreaks' => true,
);
$docx->addText($text, $paragraphOptions);

The sample Contents/addText/sample_6.php included in the package illustrates a similar code using parseLineBreaks and parseTabs options.

The parseLineBreaks option is available in many methods: https://www.phpdocx.com/documentation/api-documentation

HTML can also be used to add content with styles and line breaks.

Regards.