合并 2 个文件 docx 使用 tbszip [英] merge 2 file docx use tbszip

查看:69
本文介绍了合并 2 个文件 docx 使用 tbszip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OpenTBS 合并 2 个文件 docx.

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');

file-1.docx 中的内容包括图像+文本,file-2:仅文本.但是生成文件merge1.docx时,不能从file-1.docx生成图像请给我一个解决方案,谢谢.P/s:对不起我的英语.

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.

当我颠倒顺序打开文件时,文件merge1.docx的全部内容.为什么?

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');

推荐答案

由于图片、图表、...等内部元素,合并两个DOCX相当困难

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

  • 存档中,图片必须保存在word/media/目录下.
  • 它们必须在文件 /[Content_Types].xml
  • 中声明
  • 它们还必须分配给文件 <​​code>/word/_rels/document.xml.rels 中的唯一 ID.
  • 然后必须在与word/document.xml文件中的图片对应的XML元素中使用唯一的Id.
  • 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.

所以为了合并两个 DCX 文件,你必须应用你的代码片段,然后将图片从 DOCX 获取到另一个,然后执行上面的操作.

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.

您正在使用 OpenTBS 使用的 TbsZip,但它不是同一个工具.OpenTBS 不会帮助您将两个 DOCX 合并在一起.

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.

这篇关于合并 2 个文件 docx 使用 tbszip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆