wordpress hit counter
OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Insert altchunk into documents

  • rated by 0 users
  • This post has 3 Replies |
  • 3 Followers
  • Hi! 

    I have some problems with inserting of altchunk into documents. I want get next structure: content from root->section break->altCunk->section break->content from root where altChunk is document content-> section break -> innerAltChunk ->section break -> document content and innerChunk is simple document that contains header, footer and some text. All documents have diffrent headers and footers. My code is as below::

    class Program
        {
            private const string RootDoc = "rootDocumentCopy.docx";
            private const string ChildDoc = "childDocumentCopy.docx";
            private const string ChildDoc1 = "childDocument1Copy.docx";
    
            static void Main(string[] args)
            {
                if (File.Exists(RootDoc))
                {
                    File.Delete(RootDoc);
                }
                if (File.Exists(ChildDoc))
                {
                    File.Delete(ChildDoc);
                }
                if (File.Exists(ChildDoc1))
                {
                    File.Delete(ChildDoc1);
                }
    
                File.Copy("documents\\rootDocument.docx", RootDoc);
                File.Copy("documents\\childDocument1.docx", ChildDoc);
                File.Copy("documents\\childDocument11.docx", ChildDoc1);
    
                Console.WriteLine("Start processing...");
    
                using (var rootDoc = WordprocessingDocument.Open(RootDoc, true))
                {
                    using (var doc = WordprocessingDocument.Open(ChildDoc, true))
                    {
                        var mainPart = doc.MainDocumentPart;
                        ReplaceContent(mainPart, ChildDoc1);
                    }
                    var rootMainPart = rootDoc.MainDocumentPart;
                    ReplaceContent(rootMainPart, ChildDoc);
                }
    
                Console.ReadKey();
            }
    
            private static void ReplaceContent(MainDocumentPart mainPart, string documentToInsert)
            {
                var paragraphToReplace = mainPart.Document.Body.Elements<Paragraph>().Skip(1).First();
                paragraphToReplace.InsertSectionBreakBefore(GetSectionPropertiesFromBody(mainPart.Document.Body));
                paragraphToReplace.InsertSectionBreakAfter();
                var innerChunk = CreateAltChunk(documentToInsert, mainPart, true);
                mainPart.Document.Body.ReplaceChild(innerChunk, paragraphToReplace);
            }
    
            private static AltChunk CreateAltChunk(string documentToInsert, MainDocumentPart mainPart, bool useOwnStyles)
            {
                var chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
                chunk.FeedData(File.OpenRead(documentToInsert));
                var altChunkId = mainPart.GetIdOfPart(chunk);
                var altChunk = new AltChunk { Id = altChunkId };
                var altChunkProperties = new AltChunkProperties();
                var matchSource = new MatchSource() { Val = useOwnStyles };
                altChunkProperties.AppendChild(matchSource);
                altChunk.AppendChild(altChunkProperties);
                return altChunk;
            }
    
            private static SectionProperties GetSectionPropertiesFromBody(Body body)
            {
                var sectionProperties = body.Elements<SectionProperties>();
                return sectionProperties.FirstOrDefault();
            }
        }
    
        public static class Extensions
        {
            public static Paragraph InsertSectionBreakBefore(this OpenXmlElement element, SectionProperties sectionPropertiesToCopy = null)
            {
                var rootParent = FindRootParent(element);
                var paragraph = CreateParagraphWithSectionBreak(sectionPropertiesToCopy);
                rootParent.InsertBeforeSelf(paragraph);
                return paragraph;
            }
    
            public static Paragraph InsertSectionBreakAfter(this OpenXmlElement element, SectionProperties sectionPropertiesToCopy = null)
            {
                var rootParent = FindRootParent(element);
                var paragraph = CreateParagraphWithSectionBreak(sectionPropertiesToCopy);
                rootParent.InsertAfterSelf(paragraph);
                return paragraph;
            }
    
            private static Paragraph CreateParagraphWithSectionBreak(SectionProperties sectionPropertiesToCopy = null)
            {
                SectionProperties sectionProperties = null;
                if (sectionPropertiesToCopy != null)
                {
                    sectionProperties = (SectionProperties)sectionPropertiesToCopy.CloneNode(true);
                }
                else
                {
                    sectionProperties = new SectionProperties();
                }
    
                var secType = sectionProperties.Elements<SectionType>().FirstOrDefault();
                if (secType == null)
                {
                    secType = new SectionType { Val = SectionMarkValues.NextPage };
                    sectionProperties.InsertAt(secType, 0);
                }
                else
                {
                    secType.Val = SectionMarkValues.NextPage;
                }
                var paragraph = new Paragraph();
                var paragraphProperties = new ParagraphProperties();
                paragraphProperties.AppendChild(sectionProperties);
                paragraph.AppendChild(paragraphProperties);
                return paragraph;
            }
    
            private static OpenXmlElement FindRootParent(OpenXmlElement element)
            {
                var rootParent = element;
                while (rootParent.Parent != null && rootParent.Parent as Body == null)
                {
                    rootParent = rootParent.Parent;
                }
                return rootParent;
            }
        }
    But I get result document that contains two or more headers and footers on the page. What am I doing wrong? How can I insert all altchunk  into  separate sections?

  • I

  • We can use software’s for our business and study purposes. But we can face issues in software’s while using. It is described that dissertation writer providing help to resolve the issues of software and we can get advantages from software’s.

  • Hello! Very interesting topic, thank you raised it.

    tellthebell.com

Page 1 of 1 (4 items)