Hi
$docx->importContents($secondDocx) also overrides the existing header/footer in $docx with those in $secondDocx.
Is there any way to only import the body content?
Regards
Hi
$docx->importContents($secondDocx) also overrides the existing header/footer in $docx with those in $secondDocx.
Is there any way to only import the body content?
Regards
Hello,
The importContents method imports contents from an external DOCX. If no referenceNode is set, all contents are imported. This method is useful when importing specific contents (or exclude specific contents if needed using a custom XPath query).
If you neeed to import all contents, we recommend you use mergeDocx (https://www.phpdocx.com/api-documentation/docxutilities/merge-Word-documents-with-PHP). Setting the mergeType option you can preserve or ignore the sections (that also include the headers and footer):
0: preserves the sections of the merged document with their respective headers and footers.
1: merges the contents at the end of the last section of the first document.
If you set mergeType as 1, sections from the merged DOCX are ignored, and contents are added at the end of the last section of the first document. Maybe this method is the best approach for your needs.
If you send the DOCX samples to contact[at]phpdocx.com we'll check them to recommend the best approack for your task.
Regards.
Thanks. Not too keen on using mergeDocx because it operates on the file level, I'd much rather work on the content level.
My use case is a tool that allows a user to combine various blocks of content to a final word document. Some of these blocks are defined static, but one of them is "add content from a word document".
I am just going through all the blocks and append it to the document (basically a large for loop over all blocks appending content to the docx). This works well so far and creating a docx, merging, opening the resulting docx and continue appending is not a great fit instead of just appending content (as kind of possible with importContents).
Can you think of combination of 'parent' or 'customQuery' combination on importContents that would allow me to just extract the content? All the styles are defined in the main document, i really just need the body content.
If not i'll probably keep on using importDocuments and then override the header/footer again at the end with the original file, but its not a clean solution.
Hello,
Please note that importContents uses MultiMerge internally using in-memory documents (https://www.phpdocx.com/documentation/cookbook/in-memory-docx-documents) to avoid generating extra files.
Using importContents, you can avoid importing specific elements using a custom XPath query. For example internal sections:
$referenceNode = array(
'customQuery' => '//w:body/*[not(self::w:p/w:pPr/w:sectPr)]',
);
$docx->importContents('document.docx', $referenceNode);
But we think the easiest approach would be importing all contents to the DOCX and then use importHeadersAndFooters (https://www.phpdocx.com/api-documentation/layout-and-general/import-headers-and-footers-Word-document-with-PHP) to set headers and footers as the first DOCX.
If the contents to be imported doesn't include external relationships (such as images or charts), an alternative approach can be done getting the XML of the contents with getDocxPathQuery (https://www.phpdocx.com/api-documentation/docx-path/get-docx-path-query-info) and adding them using addWordML (https://www.phpdocx.com/api-documentation/word-content/add-raw-wordml-Word-document).
If you send to contact[at]phpdocx.com two or three sample DOCX documents we'll check them to generate a custom script.
Regards.
Thanks, I think I'll be able to get it working with one of these ways.
I just found another option which seems to be doing what I need (further testing needed...). If I add variable placeholders and then call replaceVariableByExternalFile with the second docx as argument i seem to be able to get all the content and none of the extra stuff I want to avoid.
Hello,
Although using replaceVariableByExternalFile is a good approach in some cases, please note that this method uses altChunk tags that are not supported by some DOCX readers. MS Word supports it, but other such as Google Docs or old LibreOffice versions don't. And this method doesn't allow to customize the imported contents.
We recommend you use importContents, mergeDocx (using in-memory DOCX documents if you don't want to generate extra external files) or overwrite the headers/footers using importHeadersAndFooters.
Regards.