我合併了很少的docx文件,這些文件是通過C#使用openxml和wordml創建的。那些標題爲標題1,標題2等的文件以及帶有這些標籤的一些文本。當單獨創建這些文件時,如果我們點擊或選擇那些標題爲1和標題2的文本,那麼標題1,標題2等將變得突出顯示,並且導航欄也顯示在標題1,標題2標籤上,但是在我們單擊或選擇這些文本時合併這些文檔後,標題1和標題2未突出顯示。在風格的功能區。這種合併的代碼在這裏給出,標題1,標題2沒有在合併docx文件後的文檔樣式功能區中突出顯示
MemoryStream ms = new MemoryStream();
using (WordprocessingDocument myDoc =
WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
mainPart.Document = new Document { Body = new Body() };
int counter = 1;
foreach (var sectionOutput in sectionOutputs)
{
foreach (var outputFile in sectionOutput.Files)
{
Paragraph sectionBreakPara = null;
if (!sectionOutput.SectionType.Equals(sectionOutputs[sectionOutputs.Count - 1].SectionType))
{
if (outputFile == sectionOutput.Files.Last())
//check whether this is the last file in this section
{
using (
WordprocessingDocument pkgSourceDoc =
WordprocessingDocument.Open(outputFile.OutputStream, true))
{
var sourceBody = pkgSourceDoc.MainDocumentPart.Document.Body;
SectionProperties docSectionBreak =
sourceBody.Descendants<SectionProperties>().LastOrDefault();
if (docSectionBreak != null)
{
var clonedSectionBreak = (SectionProperties)docSectionBreak.CloneNode(true);
clonedSectionBreak.RemoveAllChildren<FooterReference>();
clonedSectionBreak.RemoveAllChildren<HeaderReference>();
sectionBreakPara = new Paragraph();
ParagraphProperties sectionParaProp = new ParagraphProperties();
sectionParaProp.AppendChild(clonedSectionBreak);
sectionBreakPara.AppendChild(sectionParaProp);
}
}
}
}
string altChunkId = string.Format("altchunkId{0}", counter);
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
outputFile.OutputStream.Seek(0, SeekOrigin.Begin);
chunk.FeedData(outputFile.OutputStream);
AltChunk altChunk = new AltChunk(new AltChunkProperties(new MatchSource { Val = new OnOffValue(true) })) { Id = altChunkId };
mainPart.Document.Body.AppendChild(altChunk);
if (sectionBreakPara != null)
{
mainPart.Document
.Body
.AppendChild(sectionBreakPara);
}
counter++;
}
}
mainPart.Document.Save();
}
return ms;
對於兩個不同的源文件,標題1的樣式是否不同? – Steve
對於兩個不同的源文件,標題1的樣式不同。 – user3812657