2014-07-07 6 views
0

docx 파일을 병합하려고하는데이 파일은 C#을 통해 openxml 및 wordml을 사용하여 만들어졌습니다. 이 파일들은 heading 태그가 heading 1, heading 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; 
+0

두 개의 다른 소스 파일에 대해 제목 1의 스타일이 다른가요? – Steve

+0

두 개의 서로 다른 원본 파일에서 제목 1의 스타일이 다르지 않습니다. – user3812657

답변

0

일반적으로 스타일 정의가 styles.xml 부분에 없으면이 증상이 나타납니다. 병합 프로세스 중에 문서 내용이 넘겨졌지만 스타일 부분이 아닌 경우이 문제가 발생할 수 있습니다.

새 Word 문서에는 Normal과 같은 기본 스타일이 거의 없습니다. 제목 1과 같은 스타일 정의는 해당 스타일을 단락에 지정할 때까지 styles.xml에 추가되지 않습니다. 단락 요소에 패키지에없는 스타일에 대한 스타일 할당이 포함되어 있으면 스타일이 무시됩니다.

표 설정이 스타일을 무시하는 표 셀에서도 발생할 수 있습니다. 예를 들어 표에서 첫 번째 행 (제목과 같은)이 특정 글꼴 및 색상으로 나타나야하며 스타일 설정을 무시한다고 말할 수 있습니다.

둘 다 작동하지 않으면 생성 된 XML의 소량을 단락과 그 즉시 컨텍스트 중 하나에 게시하면 단서를 얻을 수 있습니다.

+0

안녕하세요 scanny, 제안을 주셔서 감사합니다, style.xml 실제로 Heading1 및 Heading2 스타일이 포함되어 있습니다. 개별 문서에는 실제로 그런 문제가 없습니다. Heading1 및 Heading2가 강조 표시되지만 문서를 병합 한 후에이 문제가 발생합니다. 그래서, 당신은 그 아이디어를 제안 할 수 있습니까? – user3812657

+0

응답에 몇 가지 가능성을 추가했습니다. – scanny

+0

안녕하세요 Scanny, 제안을 주셔서 감사합니다. 문제가 해결되었습니다. 다른 섹션을 병합하는 동안 style.xml을 추가했으며 OnOffValue에 false 값을 전달했습니다. 구문은 AltChunk입니다. altChunk = new AltChunk (new AltChunkProperties (New MatchSource {Val = new OnOffValue (false)})) {Id = altChunkId}; – user3812657