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

Mon May 29, 2023 11:26 am

我通过spire.doc生成了一个word文件,这个文件带有页脚,内容为【第*页,共*页】。
同时我有一个通用的封面word。 我需要讲封面word和生成的word文件合并为一个doc文件。但是我通过insertTextFromFile方法讲文件合并之后 发现合并之后的word的总页数多了1页。
所以怎么合并 才能使得封面的加入不影响之前生成的文件的总页数?

asdasdasdasdas
 
Posts: 4
Joined: Thu Apr 27, 2023 9:35 am

Tue May 30, 2023 8:35 am

您好,

感谢您的咨询。
经过我的测试,复现了您的问题。使用insertTextFromFile时会出现总页数多一页的情况。我们推荐你使用如下代码进行合并:
Code: Select all
 //加载源文档
            Document document = new Document();
            document.LoadFromFile("Word文档1.docx");

            Document document1 = new Document();

            //加载封面文档
            document1.LoadFromFile("cover.docx");
            Section section1 = document1.Sections[0];
            //合并
            document.Sections.Insert(0, section1.Clone());

            for (int i = 1; i < document.Sections.Count - 1; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                Paragraph paragraph = footer.Paragraphs[0];
                foreach (DocumentObject documentObject in paragraph.ChildObjects)
                {
                    if (documentObject.DocumentObjectType == DocumentObjectType.Field)
                    {
                        Field field = (Field)documentObject;
                        if (field.Type == FieldType.FieldNumPages)
                        {
                            field.Type = FieldType.FieldSectionPages;
                        }
                    }
                }
                section.PageSetup.RestartPageNumbering = true;
                section.PageSetup.PageStartingNumber = 1;
            }

            document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);

如果使用上述代码测试之后问题还是存在,请提供你的输入文件。您可以直接将它们附在这里或通过邮件([email protected])发送给我们。提前感谢。
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Tue May 30, 2023 10:05 am

Wenly.Zhang wrote:您好,

感谢您的咨询。
经过我的测试,复现了您的问题。使用insertTextFromFile时会出现总页数多一页的情况。我们推荐你使用如下代码进行合并:
Code: Select all
 //加载源文档
            Document document = new Document();
            document.LoadFromFile("Word文档1.docx");

            Document document1 = new Document();

            //加载封面文档
            document1.LoadFromFile("cover.docx");
            Section section1 = document1.Sections[0];
            //合并
            document.Sections.Insert(0, section1.Clone());

            for (int i = 1; i < document.Sections.Count - 1; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                Paragraph paragraph = footer.Paragraphs[0];
                foreach (DocumentObject documentObject in paragraph.ChildObjects)
                {
                    if (documentObject.DocumentObjectType == DocumentObjectType.Field)
                    {
                        Field field = (Field)documentObject;
                        if (field.Type == FieldType.FieldNumPages)
                        {
                            field.Type = FieldType.FieldSectionPages;
                        }
                    }
                }
                section.PageSetup.RestartPageNumbering = true;
                section.PageSetup.PageStartingNumber = 1;
            }

            document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);

如果使用上述代码测试之后问题还是存在,请提供你的输入文件。您可以直接将它们附在这里或通过邮件([email protected])发送给我们。提前感谢。

非常感谢 上面的代码可以解决我的问题

asdasdasdasdas
 
Posts: 4
Joined: Thu Apr 27, 2023 9:35 am

Tue May 30, 2023 10:51 am

Wenly.Zhang wrote:您好,

感谢您的咨询。
经过我的测试,复现了您的问题。使用insertTextFromFile时会出现总页数多一页的情况。我们推荐你使用如下代码进行合并:
Code: Select all
 //加载源文档
            Document document = new Document();
            document.LoadFromFile("Word文档1.docx");

            Document document1 = new Document();

            //加载封面文档
            document1.LoadFromFile("cover.docx");
            Section section1 = document1.Sections[0];
            //合并
            document.Sections.Insert(0, section1.Clone());

            for (int i = 1; i < document.Sections.Count - 1; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                Paragraph paragraph = footer.Paragraphs[0];
                foreach (DocumentObject documentObject in paragraph.ChildObjects)
                {
                    if (documentObject.DocumentObjectType == DocumentObjectType.Field)
                    {
                        Field field = (Field)documentObject;
                        if (field.Type == FieldType.FieldNumPages)
                        {
                            field.Type = FieldType.FieldSectionPages;
                        }
                    }
                }
                section.PageSetup.RestartPageNumbering = true;
                section.PageSetup.PageStartingNumber = 1;
            }

            document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);

如果使用上述代码测试之后问题还是存在,请提供你的输入文件。您可以直接将它们附在这里或通过邮件([email protected])发送给我们。提前感谢。


你好,这段代码解决了我的问题,但是我是在你提供的代码基础上进行了修改。修改如下:
Code: Select all
 //加载源文档
            Document document = new Document();
            document.LoadFromFile("Word文档1.docx");

            Document document1 = new Document();

            //加载封面文档
            document1.LoadFromFile("cover.docx");
            Section section1 = document1.Sections[0];
            //合并
            document.Sections.Insert(0, section1.Clone());

            for (int i = 0; i < document.Sections.Count ; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                if (footer.Paragraphs.Count <= 0) continue;
                Paragraph paragraph = footer.Paragraphs[0];
                foreach (DocumentObject documentObject in paragraph.ChildObjects)
                {
                    if (documentObject.DocumentObjectType == DocumentObjectType.Field)
                    {
                        Field field = (Field)documentObject;
                        if (field.Type == FieldType.FieldNumPages)
                        {
                            field.Type = FieldType.FieldSectionPages;
                        }
                    }
                }
                section.PageSetup.RestartPageNumbering = true;
                section.PageSetup.PageStartingNumber = 1;
            }

            document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);

跟您提供的代码相比 我只是改了循环的起点 并且需要把footer没有paragraph的section跳过。 我理解 两套代码应该得到的结果一致。 但是事实上,您提供的代码 还是会多一页。 我下面的代码可以正常得到想要的效果。 但是我不清楚他是如何生效的。 全量的代码我通过邮箱发送过去了

asdasdasdasdas
 
Posts: 4
Joined: Thu Apr 27, 2023 9:35 am

Wed May 31, 2023 7:06 am

你好,

感谢您的反馈。我很高兴听到你的问题已经解决了。对于您的问题,是因为FieldType.Field_Num_Pages是获取文档总页,field.setType(FieldType.Field_Section_Pages)是获取当前section页数。
如果您有任何问题,请随时与我们联系。

Sincerely,
Wenly
E-iceblue support team
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Return to 中文技术支持