Forum


Replies: 3   Views: 2775
Is ti possible to insert new text on a document at the end ?
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 mike0898  · 23-04-2018 - 17:23

I'm trying to add some text on every page at the end of document which i have loaded

But, as far as i know the text is only added in a specific position with options contains where document has a word which matched
        $referenceNode = array(
            'type' => 'paragraph',
            'contains' => 'text',
        );

Can you help me?

Regards

Posted by admin  · 24-04-2018 - 07:24

Hello,

A DOCX is not 'painted' as a PDF, it's more similar to HTML. So there's no option to know the exact position of a content until the DOCX is open using MS Word, LibreOffice, Google Docs or any other program, and the position and layout may vary depending on the program used.

The only way to accomplish what you need is using footers or sections. DOCXPath can query by section tags as well.

Regards.

Posted by mike0898  · 30-04-2018 - 17:02

Could you explain an example with sections how you say?

I've tried to do with docxpath without success

Posted by admin  · 01-05-2018 - 08:42

Hello,

As there's no way to get the end of a page by its contents, the only solution is adding sections or page breaks (https://support.office.com/en-us/article/insert-a-section-break-eef20fd8-e38c-4ba6-a027-e503bdf8375c) to each page when you create the DOCX (from scratch or a template); each page must have a page break or a section. Adding one of these breaks (section or page break) and using phpdocx 6.5 or newer (Advanced or Premium licenses) you can query by the section tag (w:sectPr , 'section' type value) to add an element before each section:

$content->addText('New text.', array('fontSize' => 20, 'color' => '#0000ff'));

// as there's no occurrence value, iterate all sections
$referenceNode = array(
    'type' => 'section',
);

$docx->insertWordFragment($content, $referenceNode, 'before');

or use a customQuery to query by the page break tag.

Anyway, we recommend you adding headers or footers to add contents to each page, as it's the easiest solution.

Regards.