douhuiyan2772 2013-11-17 09:16
浏览 70

合并2文件docx使用tbszip

I' using OpenTBS to merge 2 file docx.

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the first document

$zip->Open('file-1.docx');
$content1 = $zip->FileRead('word/document.xml');
$zip->Close();

// Extract the content of the first document
$p = strpos($content1, '<w:body'); 
if ($p===false) exit("Tag <w:body> not found in document 1.");
$p = strpos($content1, '>', $p);
$content1 = substr($content1, $p+1);
$p = strpos($content1, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document 1.");
$content1 = substr($content1, 0, $p);

// Insert into the second document
$zip->Open('file-2.docx');
$content2 = $zip->FileRead('word/document.xml');
$p = strpos($content2, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document 2.");
$content2 = substr_replace($content2, $content1, $p, 0);


$zip->FileReplace('word/document.xml', $content2, TBSZIP_STRING);

// Save the merge into a third file
$zip->Flush(TBSZIP_DOWNLOAD, 'merge1.docx');

content in file-1.docx include image+text, file-2: only text. But when gen file merge1.docx, can not gen image from file-1.docx Please for me a solution, thanks. P/s: sorry for my english.


when I reversed the order to open the file, file merge1.docx full content. why?

// Open the first document

$zip->Open('file-2.docx');
$content1 = $zip->FileRead('word/document.xml');
$zip->Close();


..........
// Insert into the second document
$zip->Open('file-1.docx');
  • 写回答

2条回答 默认 最新

  • dongzhiji0814 2013-11-18 00:42
    关注

    It is quite difficult to merge two DOCX because of internal elements such as pictures, charts, ...

    • In the archive, pictures must be saved in the word/media/ directory.
    • They must be declared in the file /[Content_Types].xml
    • They also must be assigned to a unique Id in the file /word/_rels/document.xml.rels.
    • And then the unique Id must be used in a XML element corresponding to the picture in the word/document.xml file.

    So in order to merge two DCX files you have to apply your snippet, then get the pictures from DOCX to the other, and then perform the operation above.

    You're using TbsZip, which is used by OpenTBS but it is not the same tool. OpenTBS won't help your to merge two DOCX together.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog