C# 使用openxml从最后一页中排除页眉、页脚和水印
我正在使用C# 使用openxml从最后一页中排除页眉、页脚和水印,c#,c#-4.0,openxml,docx,openxml-sdk,C#,C# 4.0,Openxml,Docx,Openxml Sdk,我正在使用OpenXml(DocumentFormat.OpenXmlnuget包)生成docx文件。我的做法如下: 我有一个文件,名为template.docx。在这个文件中,我有一个封面和一个空白页,其中有页眉,页脚,还有一个背景图像。无论如何,我首先打开文档,然后在文档中添加一些文本,然后关闭它 另一方面,我有一个名为template back.docx的文件,我想把它附加到上面修改过的文档(template.docx)的末尾 通过使用以下代码片段,我可以做到这一点: public
OpenXml
(DocumentFormat.OpenXml
nuget包)生成docx文件。我的做法如下:
我有一个文件,名为template.docx
。在这个文件中,我有一个封面
和一个空白页,其中有页眉
,页脚
,还有一个背景图像。无论如何,我首先打开文档,然后在文档中添加一些文本,然后关闭它
另一方面,我有一个名为template back.docx
的文件,我想把它附加到上面修改过的文档(template.docx
)的末尾
通过使用以下代码片段,我可以做到这一点:
public static void MergeDocumentWithPagebreak(string sourceFile, string destinationFile, string altChunkID) {
using (var myDoc = WordprocessingDocument.Open(sourceFile, true)) {
var mainPart = myDoc.MainDocumentPart;
//Append page break
var para = new Paragraph(new Run((new Break() { Type = BreakValues.Page })));
mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild);
//Append file
var chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkID);
using (var fileStream = File.Open(destinationFile, FileMode.Open))
chunk.FeedData(fileStream);
var altChunk = new AltChunk{
Id = altChunkID
};
mainPart.Document
.Body
.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
}
}
public static void MergeDocumentWithPagebreak(字符串sourceFile、字符串destinationFile、字符串altChunkID){
使用(var myDoc=WordprocessingDocument.Open(sourceFile,true)){
var mainPart=myDoc.MainDocumentPart;
//附加分页符
var para=新段落(新运行((new Break(){Type=BreakValues.Page}));
mainPart.Document.Body.InsertAfter(段落mainPart.Document.Body.LastChild);
//附加文件
var chunk=mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML,altChunkID);
使用(var fileStream=File.Open(destinationFile,FileMode.Open))
FeedData(fileStream);
var altChunk=新altChunk{
Id=altChunkID
};
主要部分.文件
.身体
.InsertAfter(altChunk,mainPart.Document.Body.Elements().Last());
mainPart.Document.Save();
}
}
但是,当我这样做时,页眉
、页脚
和背景图像将应用于最后一页。我想能够排除从这些设计的最后一页。我希望它是干净的,简单的和白色的。但谷歌搜索这个问题,却毫无帮助。你有什么想法吗?提前谢谢
附言
关于合并文档的原始文章:
这有点棘手,但没那么复杂 首先,您必须了解word的工作原理:
- 默认情况下,word文档是一个节,这一节共享页眉和页脚。如果需要不同的页眉/页脚,则必须在页面末尾创建一个分隔符,以指示“下一页是新的部分”
- 创建新节后,必须指明“新节不共享相同的页眉/页脚”
- 创建剖面分隔符
- 在此节中插入新页眉/页脚(空页眉/页脚)
- 在新部分中插入新文档
信息:我试图删除
或将r:id设置为0,但无效。创建一个空标题是最快的方法这有点棘手,但并不复杂
首先,您必须了解word的工作原理:
- 默认情况下,word文档是一个节,这一节共享页眉和页脚。如果需要不同的页眉/页脚,则必须在页面末尾创建一个分隔符,以指示“下一页是新的部分”
- 创建新节后,必须指明“新节不共享相同的页眉/页脚”
- 创建剖面分隔符
- 在此节中插入新页眉/页脚(空页眉/页脚)
- 在新部分中插入新文档
信息:我试图删除
或将r:id设置为0,但无效。创建空页眉是最快的方法用以下代码替换分页符
Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page }));
我还看到你是在最后一个孩子之后插入的,这与附加不一样,但对你来说效果很好!用这个代替
wordprocessingDocument.MainDocumentPart.Document.Body.Append(PageBreakParagraph)
您需要将剖面分隔符添加到剖面属性中。然后需要将节属性附加到段落属性。然后将段落属性附加到段落
Paragraph paragraph232 = new Paragraph();
ParagraphProperties paragraphProperties220 = new ParagraphProperties();
SectionProperties sectionProperties1 = new SectionProperties();
SectionType sectionType1 = new SectionType(){ Val = SectionMarkValues.NextPage };
sectionProperties1.Append(sectionType1);
paragraphProperties220.Append(sectionProperties1);
paragraph232.Append(paragraphProperties220);
//Replace your last but one line with this one.
mainPart.Document
.Body
.Append(altChunk);
由此产生的开放XML是:
最简单的方法是在word中实际创建文档,然后在中打开,您可以反映代码并查看C#code将生成您试图实现的各种开放XML元素。希望这有帮助 用以下代码替换分页符
Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page }));
我还看到你是在最后一个孩子之后插入的,这与附加不一样,但对你来说效果很好!用这个代替
wordprocessingDocument.MainDocumentPart.Document.Body.Append(PageBreakParagraph)
您需要将剖面分隔符添加到剖面属性中。然后需要将节属性附加到段落属性。然后将段落属性附加到段落
Paragraph paragraph232 = new Paragraph();
ParagraphProperties paragraphProperties220 = new ParagraphProperties();
SectionProperties sectionProperties1 = new SectionProperties();
SectionType sectionType1 = new SectionType(){ Val = SectionMarkValues.NextPage };
sectionProperties1.Append(sectionType1);
paragraphProperties220.Append(sectionProperties1);
paragraph232.Append(paragraphProperties220);
//Replace your last but one line with this one.
mainPart.Document
.Body
.Append(altChunk);
由此产生的开放XML是:
最简单的方法是在word中实际创建文档,然后在中打开,您可以反映代码并查看C#code将生成您试图实现的各种开放XML元素。希望这有帮助 你找到解决办法了吗?@G.Mich我5年多前问过这个问题。其实我