我通过spire.doc生成了一个word文件,这个文件带有页脚,内容为【第*页,共*页】。
同时我有一个通用的封面word。 我需要讲封面word和生成的word文件合并为一个doc文件。但是我通过insertTextFromFile方法讲文件合并之后 发现合并之后的word的总页数多了1页。
所以怎么合并 才能使得封面的加入不影响之前生成的文件的总页数?
//加载源文档
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);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])发送给我们。提前感谢。
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])发送给我们。提前感谢。
//加载源文档
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);