为有中文需求的客户提供多渠道中文技术支持.

Mon Oct 17, 2022 2:22 am

您好,

我需要合并当前 文件夹Test 下的, 所有章节的docx文件
因为每一章都是从头开始的, 所以合并时:
需要后面的章不能直接放到前一章的正文末尾, 而是要在前一章正文后添加一分页符,
然后再接上后一章的标题和正文, 即: 后一章是从新页的开头开始的, 以此类推 ...

使用下面官方帮助网页中的代码, 合并的文件没有从新页开始

Code: Select all
string file1 = "Wd1.docx";
string file2 = "Wd2.docx";
Document doc = new Document(file1);
doc.InsertTextFromFile(file2, FileFormat.Docx2013);
doc.SaveToFile("合并.docx", FileFormat.Docx2013);


另外, 就是每个文件的章节内容都是设置文档网格的, 需要合并后的文件也要保持原有的位置和格式, 不知能否实现需求.
以前有询问过按章标题拆分文件, 因为spire.doc组件还不能获取和设置文档网格, 拆分后的章标题和正文的位置是有变化的, 暂不能达到需求

keLi1680
 
Posts: 30
Joined: Sun Oct 02, 2022 2:35 am

Mon Oct 17, 2022 11:12 am

您好,

感谢您的留言。
我使用最新版本的Spire.Doc10.9.6和下面的代码去测试合并文档场景,结果文档中每一章都是从新页开始的。我把结果文档放在附件中供您参考。另外,经过核对,结果文档中的内容的位置和格式和原始文件中的是一样的。
Code: Select all
  string filePath_1 = @"../../data/1.docx";
            string filePath_2 = @"../../data/2.docx";
            string filePath_3 = @"../../data/3.docx";
            string filePath_4 = @"../../data/4.docx";

            Document doc = new Document(filePath_1);
            doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013);
            doc.InsertTextFromFile(filePath_3, FileFormat.Docx2013);
            doc.InsertTextFromFile(filePath_4, FileFormat.Docx2013);

            doc.SaveToFile(@"../../output/合并文档.docx", FileFormat.Docx2013);


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Tue Oct 18, 2022 6:15 am

不好意思, 我测试时, 标题一 还没有设置好样式,

默认的情况下, 如果新文件开始部分是 标题一 样式, 合并后, 会从新的一页开始拼接, 这样可以直接满足我的要求

但如果合并的文件, 开始部分没有 标题一 的标题, 合并之后, 会拼接到上一文件的正文之下, 在这种情况下, 怎样也实现上面的效果, 即: 从新的一页开始拼接上?

Abel.He wrote:您好,

感谢您的留言。
我使用最新版本的Spire.Doc10.9.6和下面的代码去测试合并文档场景,结果文档中每一章都是从新页开始的。我把结果文档放在附件中供您参考。另外,经过核对,结果文档中的内容的位置和格式和原始文件中的是一样的。
Code: Select all
  string filePath_1 = @"../../data/1.docx";
            string filePath_2 = @"../../data/2.docx";
            string filePath_3 = @"../../data/3.docx";
            string filePath_4 = @"../../data/4.docx";

            Document doc = new Document(filePath_1);
            doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013);
            doc.InsertTextFromFile(filePath_3, FileFormat.Docx2013);
            doc.InsertTextFromFile(filePath_4, FileFormat.Docx2013);

            doc.SaveToFile(@"../../output/合并文档.docx", FileFormat.Docx2013);


Sincerely
Abel
E-iceblue support team

keLi1680
 
Posts: 30
Joined: Sun Oct 02, 2022 2:35 am

Tue Oct 18, 2022 7:04 am

您好,

感谢您的反馈。
我把您的提供的4个文档(1.docx、2.docx等等)开始部分的样式由“标题一”改为“普通”,再测试合并场景,得到的结果文档的每一章依然是从新页开始的。我把所有修改过的输入文档和结果文档放在附件中供您参考。
如果您有任何问题,请随时联系。

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Tue Oct 18, 2022 9:13 am

我使用下面的文件和代码,测试的结果: 第二个文件接到第一个文件的正文后面了

Code: Select all
string filePath_1 = @"1.docx";
string filePath_2 = @"2.docx";


Document doc = new Document(filePath_1);
doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013);


doc.SaveToFile(@"合并.docx", FileFormat.Docx2013);




Abel.He wrote:您好,

感谢您的反馈。
我把您的提供的4个文档(1.docx、2.docx等等)开始部分的样式由“标题一”改为“普通”,再测试合并场景,得到的结果文档的每一章依然是从新页开始的。我把所有修改过的输入文档和结果文档放在附件中供您参考。
如果您有任何问题,请随时联系。

Sincerely
Abel
E-iceblue support team

keLi1680
 
Posts: 30
Joined: Sun Oct 02, 2022 2:35 am

Tue Oct 18, 2022 10:37 am

您好,

感谢您的反馈。
您的这个问题是由文档1.docx开头添加的分节符(如下截图)导致的,您使用word打开1.docx然后将2.docx的内容插入,其效果和我们产品效果是一样。对于这两个文档,如果想要实现您期望的效果,请参考下面的代码。

Code: Select all
//加载文档1
            Document document1 = new Document();
            document1.LoadFromFile(@"../../data2/1.docx");
            //加载文档2
            Document document2 = new Document();
            document2.LoadFromFile(@"../../data2/2.docx");
           
            for (int i = 0; i < document2.Sections.Count; i++)
            { //在文档2最开始添加一个分页符
                if (i == 0)
                {
                    Paragraph paragraph = new Paragraph(document2);
                    paragraph.AppendBreak(BreakType.PageBreak);
                    document2.Sections[i].Body.ChildObjects.Insert(0, paragraph);
                    document1.Sections.Add(document2.Sections[i].Clone());
                }
                else
                {
                    //将document2其余section添加到document1中
                    document1.Sections.Add(document2.Sections[i].Clone());
                }

            }

            //保存文档
            document1.SaveToFile(@"../../output/合并3.docx", FileFormat.Docx2013);


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Return to 中文技术支持

cron